1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Little fix for states

This commit is contained in:
_run 2021-11-06 12:52:41 +05:00
parent 6808ab3ebe
commit 8dcfa0c282
3 changed files with 12 additions and 4 deletions

View File

@ -5,6 +5,12 @@ from telebot import custom_filters
bot = telebot.TeleBot("") bot = telebot.TeleBot("")
class MyStates:
name = 1
surname = 2
age = 3
@bot.message_handler(commands=['start']) @bot.message_handler(commands=['start'])
def start_ex(message): def start_ex(message):
@ -24,7 +30,7 @@ def any_state(message):
bot.send_message(message.chat.id, "Your state was cancelled.") bot.send_message(message.chat.id, "Your state was cancelled.")
bot.delete_state(message.chat.id) bot.delete_state(message.chat.id)
@bot.message_handler(state=1) @bot.message_handler(state=MyStates.name)
def name_get(message): def name_get(message):
""" """
State 1. Will process when user's state is 1. State 1. Will process when user's state is 1.
@ -35,7 +41,7 @@ def name_get(message):
data['name'] = message.text data['name'] = message.text
@bot.message_handler(state=2) @bot.message_handler(state=MyStates.surname)
def ask_age(message): def ask_age(message):
""" """
State 2. Will process when user's state is 2. State 2. Will process when user's state is 2.
@ -46,14 +52,14 @@ def ask_age(message):
data['surname'] = message.text data['surname'] = message.text
# result # result
@bot.message_handler(state=3, is_digit=True) @bot.message_handler(state=MyStates.age, is_digit=True)
def ready_for_answer(message): def ready_for_answer(message):
with bot.retrieve_data(message.chat.id) as data: with bot.retrieve_data(message.chat.id) as data:
bot.send_message(message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html") bot.send_message(message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html")
bot.delete_state(message.chat.id) bot.delete_state(message.chat.id)
#incorrect number #incorrect number
@bot.message_handler(state=3, is_digit=False) @bot.message_handler(state=MyStates.age, is_digit=False)
def age_incorrect(message): def age_incorrect(message):
bot.send_message(message.chat.id, 'Looks like you are submitting a string in the field age. Please enter a number') bot.send_message(message.chat.id, 'Looks like you are submitting a string in the field age. Please enter a number')

View File

@ -0,0 +1 @@
<EFBFBD>}<7D>.

View File

@ -235,6 +235,7 @@ def test_json_chat_invite_link():
assert invite_link.pending_join_request_count == 1 assert invite_link.pending_join_request_count == 1
assert invite_link.creates_join_request assert invite_link.creates_join_request
def test_chat_member_updated(): def test_chat_member_updated():
json_string = r'{"chat": {"id": -1234567890123, "type": "supergroup", "title": "No Real Group", "username": "NoRealGroup"}, "from": {"id": 133869498, "is_bot": false, "first_name": "Vincent"}, "date": 1624119999, "old_chat_member": {"user": {"id": 77777777, "is_bot": false, "first_name": "Pepe"}, "status": "member"}, "new_chat_member": {"user": {"id": 77777777, "is_bot": false, "first_name": "Pepe"}, "status": "administrator"}}' json_string = r'{"chat": {"id": -1234567890123, "type": "supergroup", "title": "No Real Group", "username": "NoRealGroup"}, "from": {"id": 133869498, "is_bot": false, "first_name": "Vincent"}, "date": 1624119999, "old_chat_member": {"user": {"id": 77777777, "is_bot": false, "first_name": "Pepe"}, "status": "member"}, "new_chat_member": {"user": {"id": 77777777, "is_bot": false, "first_name": "Pepe"}, "status": "administrator"}}'
cm_updated = types.ChatMemberUpdated.de_json(json_string) cm_updated = types.ChatMemberUpdated.de_json(json_string)