-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (45 loc) · 1.32 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
import os
import logging
import shutil
from flask import Flask, request, abort
from linebot.v3.exceptions import (
InvalidSignatureError
)
import routes
from routes.iot.receive import iot_receive_app
from utils import (
line_webhook
)
logging.basicConfig(
level = logging.DEBUG,
filename = "runtime.log",
filemode = "w",
format = "%(asctime)s %(levelname)s: %(message)s"
)
app = Flask(__name__)
app.register_blueprint(iot_receive_app)
def initialize():
tmp_folder_path = os.path.join(os.getcwd(), "tmp")
if os.path.exists(tmp_folder_path):
shutil.rmtree(tmp_folder_path)
os.makedirs(tmp_folder_path)
@app.route("/", methods=["GET"])
def hello_world():
return "<h1>hello world</h1>"
@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)
logging.debug(f"Request body: {body}")
# handle webhook body
try:
line_webhook.handle(body, signature)
except InvalidSignatureError:
logging.error("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return "OK"
if __name__ == "__main__":
initialize()
app.run(host="0.0.0.0", port=8080, debug=True)