-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
49 lines (39 loc) · 1.47 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
#載入LineBot所需要的套件
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
app = Flask(__name__)
# 必須放上自己的Channel Access Token
line_bot_api = LineBotApi('m5f8ukwTOpBEyg7ORPLdLp968qGCQ+6SqnlOLEi31Y68vjSqBPIO0lGQEENOhqEwAI8TQ4rPRo+oaiZBwBb2q+2axOfW3FRQZHMmGvm+i5W2xa+YyLUGx1boW6GjTwi9MYLet/7SeljqZiLlM88y8gdB04t89/1O/w1cDnyilFU=')
# 必須放上自己的Channel Secret
handler = WebhookHandler('357a99751b0951f15d1825543576df63')
line_bot_api.push_message('U53f8a9041a345af60fe290efb6e1e018', TextSendMessage(text='你可以開始了'))
# 監聽所有來自 /callback 的 Post Request
@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)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
#訊息傳遞區塊
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
message = TextSendMessage(text=event.message.text)
line_bot_api.reply_message(event.reply_token,message)
#主程式
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)