-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
60 lines (44 loc) · 1.6 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
from bot_service import BotService
from flask import Flask, abort, request, send_from_directory, Response, current_app
from linebot import WebhookHandler
from linebot.exceptions import InvalidSignatureError, LineBotApiError
from linebot.models import (
MessageEvent,
TextMessage,
)
import logging
from config import config
app = Flask(__name__)
app.debug = True
BotService.init_bot(config.get('access_token'))
handler = WebhookHandler(config.get('secret'))
@app.route("/line-webhook", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
try:
handler.handle(body, signature)
except LineBotApiError as e:
errors = ''
for m in e.error.details:
errors += f' {m.property}: {m.message}'
current_app.logger.error("Got exception from LINE Messaging API: %s\n%s" % e.message,
errors)
except InvalidSignatureError:
abort(400)
return 'OK'
@app.route('/images/<path:path>')
def send_images(path):
return send_from_directory('images', path)
@app.route('/status', methods=['GET'])
def status():
return Response('{"good": "momentos"}', status=200, mimetype='application/json')
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
message: str = event.message.text
message = message.lower().strip()
return BotService(event, message).reply()
if __name__ == "__main__":
logging.basicConfig(format='[%(levelname)s] %(asctime)s $(filename)s:%(lineno)s : ',
level=logging.INFO)
app.run()