-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
108 lines (77 loc) · 2.88 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import logging
import os
import sys
from flask import Flask, request, abort
from linebot.exceptions import InvalidSignatureError
from linebot.models.events import FollowEvent, UnfollowEvent, MessageEvent, TextMessage, PostbackEvent, ImageMessage, \
AudioMessage, VideoMessage
from pyngrok import ngrok
from controllers import LineBotController
from utils import handler, ngrok_auth_token
# Append the root path
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
# Initiate google logging (https://googleapis.dev/python/logging/latest/stdlib-usage.html)
# client = google.cloud.logging.Client()
# Initiate line event log
# bot_event_handler = CloudLoggingHandler(client, name="ncu_bot_event")
# bot_event_logger = logging.getLogger('ncu_bot_event')
# bot_event_logger.setLevel(logging.INFO)
# bot_event_logger.addHandler(bot_event_handler)
# Initiate the app
app = Flask(__name__)
# CORS(app)
@app.route('/test')
def hello_world():
# bot_event_logger.info("test")
return 'Hello, World!'
@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)
# bot_event_logger.info(body)
# print(body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@app.route("/user", methods=['GET'])
def get_user():
# result = UserController.get_user(request)
# return result
pass
@handler.add(FollowEvent)
def handle_line_follow(event):
return LineBotController.follow_event(event)
@handler.add(UnfollowEvent)
def handle_line_unfollow(event):
return LineBotController.unfollow_event(event)
@handler.add(MessageEvent, message=TextMessage)
def handle_line_text(event):
return LineBotController.handle_text_message(event)
@handler.add(MessageEvent, message=ImageMessage)
def handle_line_image(event):
# return LineBotController.handle_image_message(event)
pass
@handler.add(MessageEvent, message=VideoMessage)
def handle_line_video(event):
# return LineBotController.handle_video_message(event)
pass
@handler.add(MessageEvent, message=AudioMessage)
def handle_line_audio(event):
# return LineBotController.handle_audio_message(event)
pass
@handler.add(PostbackEvent)
def handle_postback_event(event):
# return LineBotController.handle_postback_event(event)
pass
if __name__ == "__main__":
ngrok.set_auth_token(ngrok_auth_token)
print(ngrok.connect(5000).public_url)
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))