-
Notifications
You must be signed in to change notification settings - Fork 113
/
app.py
75 lines (61 loc) · 1.68 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
from bottle import route, run, request, abort, static_file
from fsm import TocMachine
VERIFY_TOKEN = "Your Webhook Verify Token"
machine = TocMachine(
states=[
'user',
'state1',
'state2'
],
transitions=[
{
'trigger': 'advance',
'source': 'user',
'dest': 'state1',
'conditions': 'is_going_to_state1'
},
{
'trigger': 'advance',
'source': 'user',
'dest': 'state2',
'conditions': 'is_going_to_state2'
},
{
'trigger': 'go_back',
'source': [
'state1',
'state2'
],
'dest': '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=5000, debug=True)