/
app.py
92 lines (73 loc) · 2.75 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
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage
import configparser
import random
import json
import os
app = Flask(__name__)
if not os.path.isfile("answer.json"):
with open("answer.json", "w") as out_file:
json.dump(dict(), out_file, indent=4)
config = configparser.ConfigParser()
config.read('config.ini')
line_bot_api = LineBotApi(config.get('line-bot', 'channel_access_token'))
handler = WebhookHandler(config.get('line-bot', 'channel_secret'))
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
with open("answer.json", "r") as in_file:
user_dict = json.load(in_file)
user_ID = event.source.user_id
print(user_dict)
if user_ID not in user_dict:
user_dict[user_ID] = random.sample('1234567890', 4)
y = event.message.text
if (y.isdigit() == False):
message = TextSendMessage(text= "Wrong Input!")
line_bot_api.reply_message(event.reply_token, message)
return
if (len(y) != 4):
message = TextSendMessage(text= "Wrong Input!")
line_bot_api.reply_message(event.reply_token, message)
return
if (len(y) != len(set(y))):
message = TextSendMessage(text= "Wrong Input!")
line_bot_api.reply_message(event.reply_token, message)
return
a = 0
b = 0
for i in range (len(y)):
if(y[i] in user_dict[user_ID]):
if(y[i] == user_dict[user_ID][i]):
a += 1
else:
b += 1
if (a == 4):
user_dict[user_ID] = random.sample('1234567890', 4)
if (a == 4):
message = [TextSendMessage(text= "%d A %d B\nCorrect!!" % (a, b)),
TextSendMessage(text= "You are excellent!!")]
line_bot_api.reply_message(event.reply_token, message)
else:
message = [TextSendMessage(text= "%d A %d B" % (a, b)),
TextSendMessage(text= "Try again!!")]
line_bot_api.reply_message(event.reply_token, message)
with open("answer.json", "w") as output:
json.dump(user_dict, output, indent=4)
if __name__ == "__main__":
app.run()