forked from kercos/ExamplesPythonTelegramBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_03_states.py
More file actions
104 lines (93 loc) · 3.73 KB
/
ex_03_states.py
File metadata and controls
104 lines (93 loc) · 3.73 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
# -*- coding: utf-8 -*-
import bot_manager
from bot_manager import (send_message, send_photo, send_location,
direct_user_to_state, repeatState, set_user_var_value, get_user_var_value)
##############################
# STATES FUNCTIONS
# * each function's name has to start with 'state_X' where X is the name of the state
# * each function is split in two parts:
# - if bot_turn: bot's turn to say something
# - else: the user said something
##############################
def state_0(user, message):
"""
state 0 is the initial state by default (see bot_manager) and must be defined
This is where the bot sends the user after s/he starts the bot (or types the command /start)
"""
bot_turn = message is None
if bot_turn:
reply_text = 'You are in the initial state.'
keyboard = [['State1'], ['State2']]
send_message(user, reply_text, keyboard)
else:
if message.text:
input_text = message.text
if input_text == 'State1':
reply_text = "I'm sending you to state 1."
send_message(user, reply_text)
direct_user_to_state(user, '1')
elif input_text == 'State2':
reply_text = "I'm sending you to state 2."
send_message(user, reply_text)
direct_user_to_state(user, '2')
else:
reply_text = "Please use the keyboard below."
send_message(user, reply_text)
else:
reply_text = 'Only text is allowed here.'
send_message(user, reply_text)
def state_1(user, message):
bot_turn = message is None
if bot_turn:
reply_text = 'You are in state 1'
keyboard = [['Option1', 'Option2'], ['🔙 Back']]
send_message(user, reply_text, keyboard)
else:
if message.text:
input_text = message.text
if input_text == 'Option1':
reply_text = "Thanks, you selected option 1"
send_message(user, reply_text)
elif input_text == 'Option2':
reply_text = "Thanks, you selected option 2"
send_message(user, reply_text)
elif input_text == '🔙 Back':
reply_text = "I'm sending back to the initial state."
send_message(user, reply_text)
direct_user_to_state(user, '0')
else:
reply_text = "Please use the keyboard below."
send_message(user, reply_text)
else:
reply_text = 'Only text is allowed here.'
send_message(user, reply_text)
def state_2(user, message):
bot_turn = message is None
if bot_turn:
reply_text = 'You are in state 2'
keyboard = [['Option1', 'Option2'], ['🔙 Back']]
send_message(user, reply_text, keyboard)
else:
if message.text:
input_text = message.text
if input_text == 'Option1':
reply_text = "Thanks, you selected option 1"
send_message(user, reply_text)
elif input_text == 'Option2':
reply_text = "Thanks, you selected option 2"
send_message(user, reply_text)
elif input_text == '🔙 Back':
reply_text = "I'm sending back to the initial state."
send_message(user, reply_text)
direct_user_to_state(user, '0')
else:
reply_text = "Please use the keyboard below."
send_message(user, reply_text)
else:
reply_text = 'Only text is allowed here.'
send_message(user, reply_text)
##############################
# MAIN
##############################
if __name__ == '__main__':
bot_manager.startBot()