forked from NCKU-CCS/TOC-Project-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
174 lines (150 loc) · 4.77 KB
/
Copy pathapp.py
File metadata and controls
174 lines (150 loc) · 4.77 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import sys
from flask import Flask, jsonify, request, abort, send_file
from dotenv import load_dotenv
from linebot import LineBotApi, WebhookParser
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage
from fsm import TocMachine
from utils import send_text_message
load_dotenv()
machine = TocMachine(
states=["user", "q1", "q2","q3","q4","q5","a","b","c","d"],
transitions=[
{
"trigger": "advance",
"source": "user",
"dest": "q1",
"conditions": "is_going_to_q1",
},
{
"trigger": "advance",
"source": "q1",
"dest": "q2",
"conditions": "is_going_to_q2_yes",
},
{
"trigger": "advance",
"source": "q1",
"dest": "q3",
"conditions": "is_going_to_q3_no",
},
{
"trigger": "advance",
"source": "q2",
"dest": "q3",
"conditions": "is_going_to_q3_yes",
},
{
"trigger": "advance",
"source": "q2",
"dest": "q4",
"conditions": "is_going_to_q4_no",
},
{
"trigger": "advance",
"source": "q3",
"dest": "q4",
"conditions": "is_going_to_q4_yes",
},
{
"trigger": "advance",
"source": "q3",
"dest": "q5",
"conditions": "is_going_to_q5_no",
},
{
"trigger": "advance",
"source": "q4",
"dest": "a",
"conditions": "is_going_to_a_yes",
},
{
"trigger": "advance",
"source": "q4",
"dest": "b",
"conditions": "is_going_to_b_no",
},
{
"trigger": "advance",
"source": "q5",
"dest": "c",
"conditions": "is_going_to_c_yes",
},
{
"trigger": "advance",
"source": "q5",
"dest": "d",
"conditions": "is_going_to_d_no",
},
{"trigger": "go_back", "source": ["a","b","c","d"], "dest": "user"},
],
initial="user",
auto_transitions=False,
show_conditions=True,
)
app = Flask(__name__, static_url_path="")
# get channel_secret and channel_access_token from your environment variable
channel_secret = os.getenv("LINE_CHANNEL_SECRET", None)
channel_access_token = os.getenv("LINE_CHANNEL_ACCESS_TOKEN", None)
if channel_secret is None:
print("Specify LINE_CHANNEL_SECRET as environment variable.")
sys.exit(1)
if channel_access_token is None:
print("Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.")
sys.exit(1)
line_bot_api = LineBotApi(channel_access_token)
parser = WebhookParser(channel_secret)
@app.route("/callback", methods=["POST"])
def callback():
signature = request.headers["X-Line-Signature"]
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# parse webhook body
try:
events = parser.parse(body, signature)
except InvalidSignatureError:
abort(400)
# if event is MessageEvent and message is TextMessage, then echo text
for event in events:
if not isinstance(event, MessageEvent):
continue
if not isinstance(event.message, TextMessage):
continue
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text=event.message.text)
)
return "OK"
@app.route("/webhook", methods=["POST"])
def webhook_handler():
signature = request.headers["X-Line-Signature"]
# get request body as text
body = request.get_data(as_text=True)
app.logger.info(f"Request body: {body}")
# parse webhook body
try:
events = parser.parse(body, signature)
except InvalidSignatureError:
abort(400)
# if event is MessageEvent and message is TextMessage, then echo text
for event in events:
if not isinstance(event, MessageEvent):
continue
if not isinstance(event.message, TextMessage):
continue
if not isinstance(event.message.text, str):
continue
print(f"\nFSM STATE: {machine.state}")
print(f"REQUEST BODY: \n{body}")
response = machine.advance(event)
if response == False:
send_text_message(event.reply_token, "Not Entering any State")
return "OK"
@app.route("/show-fsm", methods=["GET"])
def show_fsm():
machine.get_graph().draw("fsm.png", prog="dot", format="png")
return send_file("fsm.png", mimetype="image/png")
if __name__ == "__main__":
port = os.environ.get("PORT", 8000)
app.run(host="0.0.0.0", port=port, debug=True)