-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
113 lines (103 loc) · 3.87 KB
/
main.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
import yaml,sys,os,json
sys.path.append('subsys/')
data_folder = str(os.getcwd()) + '/data/'
now_folder = str(os.getcwd())
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
from logger import logger
from get_mask import get_mask
from update import update
app = Flask(__name__)
# LINE BOT info
config_data = open('config.yml','r',newline='')
config = yaml.load(config_data)
line_bot_api = LineBotApi(config['ACCESS_TOKEN'])
handler = WebhookHandler(config['SECRET'])
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
print(body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
# Message event
@handler.add(MessageEvent)
def handle_message(event):
message_type = event.message.type
user_id = event.source.user_id
reply_token = event.reply_token
logger.info('message_type: ' + str(message_type))
logger.info('user_id: ' + str(user_id))
if(message_type == 'location'):
update()
result = get_mask(event)
Card = json.load(open(data_folder + 'json/Card.json','r',encoding='utf-8'))
for i in range(len(result)):
bubble = json.load(open(data_folder + 'json/Bubble.json','r',encoding='utf-8'))
# 醫事機構代碼
Code = result[i][0]
# 藥局名稱
Name = result[i][1]
# 地址
Address = result[i][2]
# 電話
Phone = result[i][3]
# 成人口罩剩餘
AdultRemain = result[i][4]
# 兒童口罩剩餘
KidRemain = result[i][5]
# 最後更新時間
LastUpdate = result[i][6]
# 緯度
Latitude = result[i][7]
# 經度
Longitude = result[i][8]
# 標題
bubble['body']['contents'][0]['text'] = Name
# 電話
bubble['body']['contents'][1]['contents'][1]['text'] = Phone
# 地址
bubble['body']['contents'][2]['contents'][1]['text'] = Address
# 成人口罩剩餘
bubble['body']['contents'][3]['contents'][1]['text'] = AdultRemain
# 兒童口罩剩餘
bubble['body']['contents'][4]['contents'][1]['text'] = KidRemain
# 最後更新時間
bubble['body']['contents'][5]['contents'][1]['text'] = LastUpdate[5:-3]
# PostBack Action
bubble['footer']['contents'][0]['action']['data'] = 'GetMap {}'.format(Code)
Card['contents'].append(bubble)
line_bot_api.reply_message(reply_token, FlexSendMessage('查詢結果出爐~',Card))
elif(message_type == 'text'):
user_text = event.message.text
logger.info('user_text: ' + user_text)
line_bot_api.reply_message(reply_token, TextSendMessage(text = '早安^^'))
@handler.add(PostbackEvent)
def handle_postback(event):
if(event.postback.data.startswith('GetMap')):
px = event.postback.data[7:]
positions = json.load(open(data_folder + 'json/positions.json','r'))
name = positions[px]['name']
lat = positions[px]['lat']
lng = positions[px]['lng']
address = positions[px]['address']
line_bot_api.reply_message(event.reply_token, LocationSendMessage(
title = name,
address = address,
latitude = lat,
longitude = lng
))
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 80))
app.run(host='0.0.0.0', port=port)