-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
229 lines (218 loc) · 8.15 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
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
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
app = Flask(__name__)
line_bot_api = LineBotApi('YOUR_Channel_access_token')
handler = WebhookHandler('YOUR_Channel_secret')
@app.route("/", methods=['GET'])
def hello():
return "Hello World!"
@app.route("/", 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)
print("Request body: " + body, "Signature: " + signature)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
msg = event.message.text
#print(type(msg))
msg = msg.encode('utf-8')
if event.message.text == "文字":
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=event.message.text))
elif event.message.text == "貼圖":
line_bot_api.reply_message(event.reply_token,StickerSendMessage(package_id=1, sticker_id=2))
elif event.message.text == "圖片":
line_bot_api.reply_message(event.reply_token,ImageSendMessage(original_content_url='圖片網址', preview_image_url='圖片網址'))
elif event.message.text == "影片":
line_bot_api.reply_message(event.reply_token,VideoSendMessage(original_content_url='影片網址', preview_image_url='預覽圖片網址'))
elif event.message.text == "音訊":
line_bot_api.reply_message(event.reply_token,AudioSendMessage(original_content_url='音訊網址', duration=100000))
elif event.message.text == "位置":
line_bot_api.reply_message(event.reply_token,LocationSendMessage(title='my location', address='Tainan', latitude=22.994821, longitude=120.196452))
elif event.message.text == "位置2":
imagemap_message = ImagemapSendMessage(
base_url='',
alt_text='this is an imagemap',
base_size=BaseSize(height=520, width=520),
actions=[
URIImagemapAction(
link_uri='',
area=ImagemapArea(
x=174, y=65, width=707, height=416
)
),
MessageImagemapAction(
text='hello',
area=ImagemapArea(
x=520, y=0, width=520, height=520
)
)
]
)
line_bot_api.reply_message(event.reply_token,imagemap_message)
elif event.message.text == "樣板":
buttons_template = TemplateSendMessage(
alt_text='目錄 template',
template=ButtonsTemplate(
title='Template-樣板介紹',
text='Template分為四種,也就是以下四種:',
thumbnail_image_url='圖片網址',
actions=[
MessageTemplateAction(
label='Buttons Template',
text='Buttons Template'
),
MessageTemplateAction(
label='Confirm template',
text='Confirm template'
),
MessageTemplateAction(
label='Carousel template',
text='Carousel template'
),
MessageTemplateAction(
label='Image Carousel',
text='Image Carousel'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
elif event.message.text == "Buttons Template":
buttons_template = TemplateSendMessage(
alt_text='Buttons Template',
template=ButtonsTemplate(
title='這是ButtonsTemplate',
text='ButtonsTemplate可以傳送text,uri',
thumbnail_image_url='圖片網址',
actions=[
MessageTemplateAction(
label='ButtonsTemplate',
text='ButtonsTemplate'
),
URITemplateAction(
label='VIDEO1',
uri='影片網址'
),
PostbackTemplateAction(
label='postback',
text='postback text',
data='postback1'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
elif event.message.text == "Carousel template":
print("Carousel template")
Carousel_template = TemplateSendMessage(
alt_text='目錄 template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url='圖片網址',
title='this is menu1',
text='description1',
actions=[
PostbackTemplateAction(
label='postback1',
text='postback text1',
data='action=buy&itemid=1'
),
MessageTemplateAction(
label='message1',
text='message text1'
),
URITemplateAction(
label='uri1',
uri='網址'
)
]
),
CarouselColumn(
thumbnail_image_url='圖片網址',
title='this is menu2',
text='description2',
actions=[
PostbackTemplateAction(
label='postback2',
text='postback text2',
data='action=buy&itemid=2'
),
MessageTemplateAction(
label='message2',
text='message text2'
),
URITemplateAction(
label='連結2',
uri='網址'
)
]
)
]
)
)
line_bot_api.reply_message(event.reply_token,Carousel_template)
elif event.message.text == "Confirm template":
print("Confirm template")
Confirm_template = TemplateSendMessage(
alt_text='目錄 template',
template=ConfirmTemplate(
title='這是ConfirmTemplate',
text='這就是ConfirmTemplate,用於兩種按鈕選擇',
actions=[
PostbackTemplateAction(
label='Y',
text='Y',
data='action=buy&itemid=1'
),
MessageTemplateAction(
label='N',
text='N'
)
]
)
)
line_bot_api.reply_message(event.reply_token,Confirm_template)
elif event.message.text == "Image Carousel":
print("Image Carousel")
Image_Carousel = TemplateSendMessage(
alt_text='Image Carousel template',
template=ImageCarouselTemplate(
columns=[
ImageCarouselColumn(
image_url='圖片網址',
action=PostbackTemplateAction(
label='postback1',
text='postback text1',
data='action=buy&itemid=1'
)
),
ImageCarouselColumn(
image_url='圖片網址',
action=PostbackTemplateAction(
label='postback2',
text='postback text2',
data='action=buy&itemid=2'
)
)
]
)
)
line_bot_api.reply_message(event.reply_token,Image_Carousel)
return 'OK2'
if __name__ == "__main__":
app.run(debug=True,port=80)