-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
162 lines (148 loc) · 3.92 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
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
import os
from bottle import route, run, request, abort, static_file
from fsm import TocMachine
VERIFY_TOKEN = os.environ.get('VERIFY_TOKEN')
PORT = os.environ.get('PORT')
machine = TocMachine(
states=[
'user',
'save',
'load',
'delete',
'readdate',
'readnumber',
'check',
'getdata',
'deletedata',
'other',
'domemo',
'memosave',
'memoload'
],
transitions=[
{
'trigger': 'advance',
'source': 'user',
'dest': 'save',
'conditions': 'is_going_to_save'
},
{
'trigger': 'advance',
'source': 'user',
'dest': 'load',
'conditions': 'is_going_to_load'
},
{
'trigger': 'advance',
'source': 'user',
'dest': 'delete',
'conditions': 'is_going_to_delete'
},
{
'trigger': 'advance',
'source': 'user',
'dest': 'other',
'conditions': 'is_going_to_other'
},
{
'trigger': 'advance',
'source': 'other',
'dest': 'domemo',
'conditions': 'is_going_to_save'
},
{
'trigger': 'advance',
'source': 'domemo',
'dest': 'memosave',
'conditions': 'is_going_to_read'
},
{
'trigger': 'advance',
'source': 'other',
'dest': 'memoload',
'conditions': 'is_going_to_load'
},
{
'trigger': 'advance',
'source': 'save',
'dest': 'readdate',
'conditions': 'is_going_to_read'
},
{
'trigger': 'advance',
'source': 'readdate',
'dest': 'readnumber',
'conditions': 'is_going_to_read'
},
{
'trigger': 'advance',
'source': 'readnumber',
'dest': 'check',
'conditions': 'is_going_to_check'
},
{
'trigger': 'advance',
'source': 'readnumber',
'dest': 'save',
'conditions': 'is_going_to_retrun'
},
{
'trigger': 'advance',
'source': 'load',
'dest': 'getdata',
'conditions': 'is_going_to_data'
},
{
'trigger': 'advance',
'source': 'delete',
'dest': 'deletedata',
'conditions': 'is_going_to_data'
},
{
'trigger': 'go_to_initial',
'source': [
'deletedata',
'getdata',
'check',
'memosave',
'memoload'
],
'dest': 'user'
},
{
'trigger': 'advance',
'source': '*',
'dest': 'user',
'conditions': 'is_going_to_user'
}
],
initial='user',
auto_transitions=False,
show_conditions=True,
)
@route("/webhook", method="GET")
def setup_webhook():
mode = request.GET.get("hub.mode")
token = request.GET.get("hub.verify_token")
challenge = request.GET.get("hub.challenge")
if mode == "subscribe" and token == VERIFY_TOKEN:
print("WEBHOOK_VERIFIED")
return challenge
else:
abort(403)
@route("/webhook", method="POST")
def webhook_handler():
body = request.json
print('\nFSM STATE: ' + machine.state)
print('REQUEST BODY: ')
print(body)
if body['object'] == "page":
event = body['entry'][0]['messaging'][0]
machine.advance(event)
return 'OK'
@route('/show-fsm', methods=['GET'])
def show_fsm():
machine.get_graph().draw('fsm.png', prog='dot', format='png')
return static_file('fsm.png', root='./', mimetype='image/png')
if __name__ == "__main__":
run(host="0.0.0.0", port=PORT, debug=True, reloader=True)