-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.py
40 lines (35 loc) · 1.03 KB
/
template.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
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 info
line_bot_api = LineBotApi('Channel Access token')
handler = WebhookHandler('Channel Secret')
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
print(body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
# Message event
@handler.add(MessageEvent)
def handle_message(event):
message_type = event.message.type
user_id = event.source.user_id
reply_token = event.reply_token
message = event.message.text
line_bot_api.reply_message(reply_token, TextSendMessage(text = message))
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 80))
app.run(host='0.0.0.0', port=port)