-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (61 loc) · 2.13 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
from flask import Flask, request, abort
import os
from Checker import Receipt_Numbers
from Utilities_Functions import filter_inputs, parse_results
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
line_bot_api = LineBotApi(os.environ['Channel_Access_Token'])
handler = WebhookHandler(os.environ['Channel_Secret'])
rn = Receipt_Numbers()
@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:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
print(event) #debug
if event.source.type == "room":
r_id = event.source.room_id # should've named room id, which is unique
else:
r_id = event.source.user_id
print('Received', event.message.text)
filtered_text = list(map(filter_inputs, event.message.text.split()))
sets_of_digits_count = len(filtered_text)
batch = [single_check(t) for t in filtered_text if t] # if t isn't ''
batch = '\n\n'.join(batch)
if batch == '':
batch = '請輸入數字'
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=batch))
# sends an additional notification message if won
# and when the batch of numbers is greater than 5 sets
# if 'congratulations' is in the message and sets count >= 5:
if '恭喜' in batch and sets_of_digits_count >= 5:
line_bot_api.push_message(
r_id,
TextSendMessage(text='有中獎喔~'))
def single_check(input_text):
result = rn.check(input_text)
msg = list(map(parse_results, result))
msg = '\n'.join(msg)
return '號碼{}:\n{}'.format(input_text[:8], msg)
if __name__ == "__main__":
app.run()