-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
65 lines (51 loc) · 1.81 KB
/
api.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
from http.client import HTTPException
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import TextSendMessage
import json
import os
import sentry_sdk
import logging
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
def strip_sensitive_data(event, hint):
if "exc_info" in hint:
instance = hint['exc_info'][1]
if isinstance(instance, HTTPException) and instance.code < 500:
return None
return event
sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.WARNING # Send errors as events
)
sentry_sdk.init(
dsn=os.environ.get('SENTRY_DSN'),
before_send=strip_sensitive_data,
integrations=[FlaskIntegration(), sentry_logging]
)
app = Flask(__name__)
@app.route("/webhook", methods=['POST'])
def webhook():
line_bot_api = LineBotApi(os.environ.get('LINE_CHANNEL_TOKEN'))
handler = WebhookHandler(os.environ.get('LINE_CHANNEL_SECRET_KEY'))
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
event = json.loads(body)
print(event)
try:
handler.handle(body, signature)
except InvalidSignatureError:
print(
"Invalid signature. Please check your channel access token/channel secret.")
abort(400)
token = event['events'][0]['replyToken']
if token == "00000000000000000000000000000000":
pass
else:
line_bot_api.reply_message(token, TextSendMessage(
text=event['events'][0]['message']['text']))
return 'OK'
if __name__ == '__main__':
app.run(debug=True)