mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
States, and some minor improvements
This commit is contained in:
34
examples/custom_states.py
Normal file
34
examples/custom_states.py
Normal file
@ -0,0 +1,34 @@
|
||||
import telebot
|
||||
|
||||
from telebot.handler_backends import State
|
||||
|
||||
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.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:
|
||||
data['name'] = message.text
|
||||
|
||||
|
||||
@bot.state_handler(state=2)
|
||||
def ask_age(message, state:State):
|
||||
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:
|
||||
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.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.polling()
|
@ -1,11 +1,13 @@
|
||||
import telebot
|
||||
from telebot import custom_filters
|
||||
import config
|
||||
bot = telebot.TeleBot(config.api_token)
|
||||
|
||||
import handlers
|
||||
api_token = '1297441208:AAH5THRzLXvY5breGFzkrEOIj7zwCGzbQ-Y'
|
||||
|
||||
bot.register_message_handler(handlers.start_executor, commands=['start']) # Start command executor
|
||||
bot = telebot.TeleBot(api_token)
|
||||
|
||||
def start_executor(message):
|
||||
bot.send_message(message.chat.id, 'Hello!')
|
||||
|
||||
bot.register_message_handler(start_executor, commands=['start']) # Start command executor
|
||||
|
||||
# See also
|
||||
# bot.register_callback_query_handler(*args, **kwargs)
|
@ -1,5 +0,0 @@
|
||||
import telebot
|
||||
|
||||
api_token = ''
|
||||
|
||||
bot = telebot.TeleBot(api_token)
|
@ -1,9 +0,0 @@
|
||||
# All handlers can be written in this file
|
||||
from config import bot
|
||||
|
||||
def start_executor(message):
|
||||
bot.send_message(message.chat.id, 'Hello!')
|
||||
|
||||
# Write more handlers here if you wish. You don't need a decorator
|
||||
|
||||
# Just create function and register in main file.
|
Reference in New Issue
Block a user