forked from eternnoir/pyTelegramBotAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eternnoir#1324 from coder2020official/master
Update of state handlers
- Loading branch information
Showing
4 changed files
with
70 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,48 @@ | ||
import telebot | ||
|
||
from telebot.handler_backends import State | ||
from telebot import custom_filters | ||
|
||
bot = telebot.TeleBot("") | ||
|
||
|
||
|
||
@bot.message_handler(commands=['start']) | ||
def start_ex(message): | ||
bot.set_state(message.chat.id, 1) | ||
bot.send_message(message.chat.id, 'Hi, write me a name') | ||
|
||
|
||
|
||
@bot.state_handler(state=1) | ||
def name_get(message, state:State): | ||
@bot.message_handler(state="*", commands='cancel') | ||
def any_state(message): | ||
bot.send_message(message.chat.id, "Your state was cancelled.") | ||
bot.delete_state(message.chat.id) | ||
|
||
@bot.message_handler(state=1) | ||
def name_get(message): | ||
bot.send_message(message.chat.id, f'Now write me a surname') | ||
state.set(message.chat.id, 2) | ||
with state.retrieve_data(message.chat.id) as data: | ||
bot.set_state(message.chat.id, 2) | ||
with bot.retrieve_data(message.chat.id) as data: | ||
data['name'] = message.text | ||
|
||
|
||
@bot.state_handler(state=2) | ||
def ask_age(message, state:State): | ||
@bot.message_handler(state=2) | ||
def ask_age(message): | ||
bot.send_message(message.chat.id, "What is your age?") | ||
state.set(message.chat.id, 3) | ||
with state.retrieve_data(message.chat.id) as data: | ||
bot.set_state(message.chat.id, 3) | ||
with bot.retrieve_data(message.chat.id) as data: | ||
data['surname'] = message.text | ||
|
||
@bot.state_handler(state=3) | ||
def ready_for_answer(message, state: State): | ||
with state.retrieve_data(message.chat.id) as data: | ||
@bot.message_handler(state=3, is_digit=True) | ||
def ready_for_answer(message): | ||
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") | ||
state.finish(message.chat.id) | ||
bot.delete_state(message.chat.id) | ||
|
||
@bot.message_handler(state=3, is_digit=False) | ||
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.infinity_polling() | ||
bot.add_custom_filter(custom_filters.StateFilter(bot)) | ||
bot.add_custom_filter(custom_filters.IsDigitFilter()) | ||
bot.infinity_polling(skip_pending=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters