-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinechatBOT.py
119 lines (105 loc) · 4.18 KB
/
linechatBOT.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from flask import Flask
app = Flask(__name__)
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, StickerSendMessage, LocationSendMessage, QuickReply, QuickReplyButton, MessageAction
line_bot_api = LineBotApi('PUT_YOUR_LINE_TOKEN')
handler = WebhookHandler('PUT_YOUR_HEROKU_TOKEN')
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
mtext = event.message.text
if mtext == '@傳送文字':
try:
message = TextSendMessage(
text = "1073513 王辰豪"
)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤'))
elif mtext == "@傳送圖片":
try:
message = ImageSendMessage(
original_content_url = "https://i.imgur.com/kYDpOEQ.jpg",
preview_image_url = "https://i.imgur.com/kYDpOEQ.jpg"
)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤'))
elif mtext == "@傳送貼圖":
try:
message = StickerSendMessage(
package_id='1',
sticker_id='2'
)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤'))
elif mtext == "@多項傳送":
try:
message = [
StickerSendMessage(
package_id='1',
sticker_id='2'
),
ImageSendMessage(
original_content_url = "https://i.imgur.com/kYDpOEQ.jpg",
preview_image_url = "https://i.imgur.com/kYDpOEQ.jpg"
),
TextSendMessage(
text = "1073513 王辰豪"
)
]
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤'))
elif mtext == "@傳送位置":
try:
message = LocationSendMessage(
title='元智大學',
address='桃園市中壢區遠東路135號',
latitude=24.970152,
longitude=121.267498
)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤'))
elif mtext == "@快速選單":
try:
message = TextSendMessage(
text='請選擇你最喜歡的課程',
quick_reply=QuickReply(
items=[
QuickReplyButton(
action=MessageAction(label="雲端運算", text="雲端運算")
),
QuickReplyButton(
action=MessageAction(label="計算機網路概論", text="計算機網路概論")
),
QuickReplyButton(
action=MessageAction(label="經典導讀", text="經典導讀")
),
QuickReplyButton(
action=MessageAction(label="木球", text="木球")
),
]
)
)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤'))
if __name__ == '__main__':
app.run()
# In[ ]: