forked from Lee-W/TOC-Project-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (62 loc) · 1.62 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
import sys
from io import BytesIO
import telegram
from flask import Flask, request, send_file
from fsm import TocMachine
API_TOKEN = 'Your Telegram API Token'
WEBHOOK_URL = 'Your Webhook URL'
app = Flask(__name__)
bot = telegram.Bot(token=API_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,
)
def _set_webhook():
status = bot.set_webhook(WEBHOOK_URL)
if not status:
print('Webhook setup failed')
sys.exit(1)
else:
print('Your webhook URL has been set to "{}"'.format(WEBHOOK_URL))
@app.route('/hook', methods=['POST'])
def webhook_handler():
update = telegram.Update.de_json(request.get_json(force=True), bot)
machine.advance(update)
return 'ok'
@app.route('/show-fsm', methods=['GET'])
def show_fsm():
byte_io = BytesIO()
machine.graph.draw(byte_io, prog='dot', format='png')
byte_io.seek(0)
return send_file(byte_io, attachment_filename='fsm.png', mimetype='image/png')
if __name__ == "__main__":
_set_webhook()
app.run()