-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathline_chatbot_join.py
More file actions
61 lines (45 loc) · 1.38 KB
/
line_chatbot_join.py
File metadata and controls
61 lines (45 loc) · 1.38 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
import json
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, JoinEvent, LeaveEvent, TextMessage, TextSendMessage
)
app = Flask(__name__)
line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
@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)
print(body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(JoinEvent)
def handle_join(event):
newcoming_text = "謝謝邀請我這個機器來至此群組!!我會盡力為大家服務的~"
line_bot_api.reply_message(
event.reply_token,
TextMessage(text=newcoming_text)
)
print("JoinEvent =", JoinEvent)
@handler.add(LeaveEvent)
def handle_leave(event):
print("leave Event =", event)
print("我被踢掉了QQ 相關資訊", event.source)
@app.route('/')
def homepage():
return 'Hello, World!'
if __name__ == "__main__":
app.run()