forked from datademofun/heroku-basic-flask
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathapp.py
More file actions
263 lines (231 loc) · 8.11 KB
/
app.py
File metadata and controls
263 lines (231 loc) · 8.11 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import json
import requests
from flask import Flask, request, abort
from linebot import LineBotApi
from linebot.models import TextSendMessage
from linebot.exceptions import LineBotApiError
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
line_channel_access_token = 'FT9zaPscYQJbAQtWq4bHl1iM4Kpx7tHthvQZn4KcCj4eta1Nfq0T+jBpYxGtxPtrRrj3/GD6QLDCQxThjS52itajTMpnvsnJzRwuMj9xivIQIN1fkIEslMimIRNnyXF/heI7MCcCtpCNcbD2Y4EDewdB04t89/1O/w1cDnyilFU='
line_bot_api = LineBotApi(line_channel_access_token)
handler = WebhookHandler('96363b03e615241b7d68c6ef2ba8dcf2')
Authorization = "Bearer {}".format(line_channel_access_token)
@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)
body = json.loads(body)
print (body)
reply_token = body['events'][0]["replyToken"]
print("reply_token: {}".format(reply_token))
event_type = body['events'][0]['type']
print("event_type: {}".format(event_type))
if event_type == "message":
message_type = body['events'][0]['message']['type']
# print("message_type: {}".format(message_type))
if message_type == "text":
text = body['events'][0]['message']['text']
print("text: {}".format(text))
if "home" in text or "Home" in text:
print("replying text:{}".format(text))
reply_menu3(reply_token)
elif text == "weather":
line_bot_api.reply_message(reply_token, TextSendMessage(text='ตอนนี้อุณหภูมิ ที่บ้าน 30 C '))
elif text == "energy":
line_bot_api.reply_message(reply_token, TextSendMessage(text='การใช้ไไฟ้าที่บ้านวันนี้ 3.4 หน่วย คิดเป็นเงิน 12 บาท'))
return '',200
def reply_menu(reply_token):
print('from reply menu')
print("Authorization:{}".format(Authorization))
print("reply_token:{}".format((reply_token)))
# line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World222!'))
response = requests.post(
url="https://api.line.me/v2/bot/message/reply",
headers={
"Content-Type": "application/json",
"Authorization": Authorization,
},
data=json.dumps({
"replyToken": str(reply_token),
"messages": [
{
"type": "text",
"text": "เชิญเลือกเมนูได้เลยครับ",
"quickReply": {
"items": [
{
"type": "action",
"imageUrl": 'https://drive.google.com/uc?export=download&id=1Hsd2w0YF1ThUIQEiXXm-6s67fmV7azh2',
"action": {
"type": "message",
"label": "home",
"text": "Home"
}
},
{
"type": "action",
"imageUrl": 'https://drive.google.com/uc?export=download&id=1Hsd2w0YF1ThUIQEiXXm-6s67fmV7azh2',
"action": {
"type": "message",
"label": "weather",
"text": "weather"
}
},
{
"type": "action",
"imageUrl": 'https://drive.google.com/uc?export=download&id=1Hsd2w0YF1ThUIQEiXXm-6s67fmV7azh2',
"action": {
"type": "message",
"label": "energy",
"text": "energy"
}
}
]
}
}
]
})
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
def reply_menu2(reply_token):
print('from reply menu')
print("Authorization:{}".format(Authorization))
print("reply_token:{}".format((reply_token)))
# line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World222!'))
response = requests.post(
url="https://api.line.me/v2/bot/message/reply",
headers={
"Content-Type": "application/json",
"Authorization": Authorization,
},
data=json.dumps({
"replyToken": str(reply_token),
"messages": [{
"type": "template",
"altText": "this is a carousel template",
"template": {
"type": "carousel",
"actions": [],
"columns": [
{
"thumbnailImageUrl": "https://d3n8a8pro7vhmx.cloudfront.net/edonsw/pages/995/attachments/original/1386210667/green_energy_320.jpg",
"text": "Energy",
"actions": [
{
"type": "message",
"label": "click",
"text": "energy"
}
]
},
{
"thumbnailImageUrl": "https://d3n8a8pro7vhmx.cloudfront.net/edonsw/pages/995/attachments/original/1386210667/green_energy_320.jpg",
"text": "Weather",
"actions": [
{
"type": "message",
"label": "click",
"text": "weather"
}
]
}
]
}
}]
})
)
def reply_menu3(reply_token):
response = requests.post(
url="https://api.line.me/v2/bot/message/reply",
headers={
"Content-Type": "application/json",
"Authorization": Authorization,
},
data=json.dumps({
"replyToken": str(reply_token),
"messages": [{
"type": "template",
"altText": "this is a carousel template",
"template": {
"type": "carousel",
"actions": [],
"columns": [
{
"thumbnailImageUrl": "https://sv1.picz.in.th/images/2019/06/27/1CCpqZ.th.jpg",
"text": "weather",
"actions": [
{
"type": "message",
"label": "weather",
"text": "weather"
}
]
},
{
"thumbnailImageUrl": "https://d3n8a8pro7vhmx.cloudfront.net/edonsw/pages/995/attachments/original/1386210667/green_energy_320.jpg",
"text": "energy",
"actions": [
{
"type": "message",
"label": "energy",
"text": "energy"
}
]
}
]
}
}]
})
)
def reply_menu4(reply_token):
response = requests.post(
url="https://api.line.me/v2/bot/message/reply",
headers={
"Content-Type": "application/json",
"Authorization": Authorization,
},
data=json.dumps({
"replyToken": str(reply_token),
"messages": []
})
)
def reply_menu5(reply_token):
response = requests.post(
url="https://api.line.me/v2/bot/message/reply",
headers={
"Content-Type": "application/json",
"Authorization": Authorization,
},
data=json.dumps({
"replyToken": str(reply_token),
"messages": []
})
)
def reply_menu6(reply_token):
response = requests.post(
url="https://api.line.me/v2/bot/message/reply",
headers={
"Content-Type": "application/json",
"Authorization": Authorization,
},
data=json.dumps({
"replyToken": str(reply_token),
"messages": []
})
)
if __name__ == "__main__":
app.run()