-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstates.py
More file actions
75 lines (52 loc) · 2.07 KB
/
states.py
File metadata and controls
75 lines (52 loc) · 2.07 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
from whatsapp_chatbot_python import BaseStates, GreenAPIBot, Notification
from whatsapp_chatbot_python.filters import TEXT_TYPES
bot = GreenAPIBot(
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
)
class States(BaseStates):
USERNAME = "username"
PASSWORD = "password"
@bot.router.message(state=None)
def message_handler(notification: Notification) -> None:
sender = notification.sender
notification.state_manager.set_state(sender, States.USERNAME.value)
notification.answer("Hello. Tell me your username.")
@bot.router.message(command="cancel")
def cancel_handler(notification: Notification) -> None:
sender = notification.sender
state = notification.state_manager.get_state(sender)
if not state:
return None
else:
notification.state_manager.delete_state(sender)
notification.answer("Bye")
@bot.router.message(state=States.USERNAME.value, type_message=TEXT_TYPES)
def username_handler(notification: Notification) -> None:
sender = notification.sender
username = notification.message_text
if not 5 <= len(username) <= 20:
notification.answer("Invalid username.")
else:
notification.state_manager.update_state(sender, States.PASSWORD.value)
notification.state_manager.set_state_data(
sender, {"username": username}
)
notification.answer("Tell me your password.")
@bot.router.message(state=States.PASSWORD.value, type_message=TEXT_TYPES)
def password_handler(notification: Notification) -> None:
sender = notification.sender
password = notification.message_text
if not 8 <= len(password) <= 20:
notification.answer("Invalid password.")
else:
data = notification.state_manager.get_state_data(sender)
username = data["username"]
notification.answer(
(
"Successful account creation.\n\n"
f"Your username: {username}.\n"
f"Your password: {password}."
)
)
notification.state_manager.delete_state(sender)
bot.run_forever()