-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
140 lines (111 loc) · 3.76 KB
/
run.py
File metadata and controls
140 lines (111 loc) · 3.76 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
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
from pyrogram import Client
from decouple import config # pip install python-decouple
from pyrogram.types import ReplyKeyboardMarkup as Markup
from re import match
app = Client(
name="db",
api_id=config('api_id'),
api_hash=config('api_hash'),
bot_token=config('token')
)
{
123456: {'step': "home", 'name': '', "email": ""},
789456: {'step': "home", 'name': '', "email": ""},
654321: {'step': "home", 'name': '', "email": ""}
}
{
1904997544: {'step': 'home', 'name': 'alireza', 'email': 'alireza@gmail.com'},
317817082: {'step': 'home', 'name': 'Hamidreza', 'email': 'hamid@gmail.com'}
}
STEP = {}
@app.on_message()
def messages(app, message):
global STEP
print(f'New Message from {message.chat.first_name}')
chat_id = message.chat.id
if chat_id not in STEP:
STEP[chat_id] = {'step': "home", 'name': '', "email": ""}
text = message.text
# text - step - callback
commands = [
[r"/start", r".+", start], # text - step - callback
[r"cancel", r".+", start],
[r"signup", r"home", signup],
[r"Info", r"home", info],
[r".+", r"enter-full-name", enter_full_name],
[r".+", r"enter-email-address", enter_email_address],
]
for command in commands:
pattern, step, callback = command
if match(pattern, text) and match(step, STEP[chat_id]["step"]):
callback(message)
break
else:
message.reply_text(text='bad request.')
# if text in ['/start', 'cancel']:
# start(message)
# elif text == 'signup' and STEP[chat_id]["step"] == 'home':
# signup(message)
# elif text == "Info" and STEP[chat_id]["step"] == 'home':
# info(message)
# elif STEP[chat_id]["step"] == 'enter-full-name':
# enter_full_name(message)
# elif STEP[chat_id]["step"] == 'enter-email-address':
# enter_email_address(message)
# else:
# message.reply_text(text='bad request.')
def start(message):
chat_id = message.chat.id
STEP[chat_id]["step"] = 'home'
message.reply_text(
text="wellcome to pysoft tutorial.",
reply_markup=Markup(
keyboard=[
["signup"],
["Info"]
],
resize_keyboard=True
)
)
def signup(message):
chat_id = message.chat.id
STEP[chat_id]["step"] = 'enter-full-name'
message.reply_text(
text="enter your full-name: ",
reply_markup=Markup(
keyboard=[["cancel"]],
resize_keyboard=True
)
)
def enter_full_name(message):
chat_id, text = message.chat.id, message.text
if text.isdigit():
return message.reply_text(text='please enter your full-name correctly:')
STEP[chat_id]["step"] = 'enter-email-address'
STEP[chat_id]['name'] = text
message.reply_text(
text="all right, enter your email-address: ",
reply_markup=Markup(
keyboard=[["cancel"]],
resize_keyboard=True
)
)
def enter_email_address(message):
chat_id, text = message.chat.id, message.text
STEP[chat_id]["step"] = 'home'
STEP[chat_id]['email'] = text
message.reply_text(
text="Ok, your informations are saved successfully.\n\nYou have returned to the home page.",
reply_markup=Markup(
keyboard=[["signup"], ["Info"]],
resize_keyboard=True
)
)
def info(message):
chat_id = message.chat.id
if STEP[chat_id]["name"] and STEP[chat_id]["email"]:
print(STEP)
message.reply_text(text=f"Your name is **{STEP[chat_id]['name']}** and your email address is **{STEP[chat_id]['email']}**")
else:
message.reply_text(text=f"Your registration is incomplete, please register first.")
app.run()