-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
132 lines (108 loc) · 4.22 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
import json
import time
import traceback
from flask import Flask, request, abort
import configparser
from game import *
from linebot import WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage
config = configparser.ConfigParser()
configpath = os.path.dirname(os.path.realpath(__file__)) + '/config.ini'
config.read(configpath)
handler = WebhookHandler(config.get('line', 'secret'))
facebooK_hub_verify_token = config.get('facebook', 'hub_verify_token')
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/facebook", methods=['GET'])
def facebookget():
if request.args.get('hub.mode') == 'subscribe' and request.args.get('hub.verify_token') == facebooK_hub_verify_token:
return request.args.get('hub.challenge')
return "OK"
@app.route("/facebook", methods=['POST'])
def facebookpost():
data = json.loads(request.data.decode("utf8"))
if "messaging" in data["entry"][0]:
if "message" in data["entry"][0]["messaging"][0]:
if "text" in data["entry"][0]["messaging"][0]["message"]:
userid = data["entry"][0]["messaging"][0]["sender"]["id"]
game = FacebookGame(userid)
message = data["entry"][0]["messaging"][0]["message"]["text"]
response = game.response(message)
game.sendmessage(response)
return "OK"
@app.route("/guess")
def log1():
game = Game("unknown", "unknown")
game.cur.execute("""SELECT * FROM `guess`""")
rows = game.cur.fetchall()
text = ""
for row in rows:
text += row[0] + " " + row[1] + " " + row[2] + " " + row[3] + " " + row[4] + "\n<hr>\n"
return text
@app.route("/log")
def log2():
game = Game("unknown", "unknown")
game.cur.execute("""SELECT `message` FROM `log` ORDER BY `time` DESC LIMIT 20""")
rows = game.cur.fetchall()
text = ""
for row in rows:
text += row[0] + "\n<hr>\n"
return text
@app.route("/telegram", methods=['POST'])
def telegram():
data = json.loads(request.data.decode("utf8"))
if "message" in data:
if "text" in data["message"]:
date = data["message"]["date"]
if date < time.time() - 600:
return "OK"
userid = data["message"]["chat"]["id"]
fromid = data["message"]["from"]["id"]
text = data["message"]["text"]
game = TelegramGame(userid, fromid, date)
if "reply_to_message" in data["message"] and data["message"]["reply_to_message"]["from"]["id"] != game.botid:
return "OK"
if "reply_to_message" not in data["message"] and not text.startswith("/") and game.isgroup:
return "OK"
first_name = data["message"]["from"]["first_name"]
chat_name = first_name
if "title" in data["message"]["chat"]:
chat_name = data["message"]["chat"]["title"]
if "username" in data["message"]["chat"]:
chat_name += " @" + data["message"]["chat"]["username"]
response = game.response(text)
if response != "":
if game.isdelusermsg:
game.addmessage(data["message"]["message_id"])
game.sendmessage(response, data["message"]["message_id"])
if game.isgroup:
game.managemessage()
if game.isdelanswer:
game.addmessage(game.botmsgid)
return "OK"
return "OK"
@app.route("/line", methods=['POST'])
def line():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def line_message_handler(event):
userid = event.source.user_id
message = event.message.text
reply_token = event.reply_token
game = LineGame(userid, reply_token)
game.log('get ' + str(userid) + ' ' + str(message))
response = game.response(message)
game.sendmessage(response)
if __name__ == "__main__":
app.run()