I have written a simple telegramm bot with pyTelegramBotAPI. The dialog I need should look like this:
- You: 'something'
- Bot: Hi there, I am Example bot. What's your name?
- You: 'some name'
- Bot: 'Choose your number' (shows keyboard with numbers)
- You: chose the number
- Bot: 'You chose 1. Do you want to repeat?'
- You: 'yes'
- Bot: 'Choose your number' (shows keyboard with numbers)
But, for some reason, after selecting the "yes" option, the bot does not show the keyboard, but waits for me to write to it again (step 8). So it looks like this:
- You: 'something'
- Bot: Hi there, I am Example bot. What's your name?
- You: 'some name'
- Bot: 'Choose your number' (shows keyboard with numbers)
- You: chose the number
- Bot: 'You chose 1. Do you want to repeat?'
- You: 'yes'
- You: 'something'
- Bot: 'Choose your number' (shows keyboard with numbers)
How can I make the bot answer immediately after "yes" without having to send another message to it?
code:
import telebot
from telebot import types
API_TOKEN = 'place_token_here'
bot = telebot.TeleBot(API_TOKEN)
@bot.message_handler(func=lambda m: True)
def send_welcome(message):
msg = bot.send_message(message.from_user.id, """\
Hi there, I am Example bot.
What's your name?
""", allow_sending_without_reply=True)
bot.register_next_step_handler(msg, another_test_step)
def another_test_step(message):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('1', '2', '3')
bot.send_message(message.from_user.id, 'Choose your number', reply_markup=markup, allow_sending_without_reply=True)
bot.register_next_step_handler(message, after_key)
def after_key(message):
message = bot.send_message(message.from_user.id, 'You chose ' + message.text + '. Do you want to repeat?')
bot.register_next_step_handler(message, yet2_test_step)
def yet2_test_step(message):
if message.text == "yes":
bot.register_next_step_handler(message, another_test_step)
elif message.text == 'no':
bot.register_next_step_handler(message, send_welcome)
bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()
bot.infinity_polling()
I was also advised to change the yet2_test_step
function with calling function another_test_step
manually in yet2_test_step
. So code is:
import telebot
from telebot import types
API_TOKEN = ''
bot = telebot.TeleBot(API_TOKEN)
@bot.message_handler(func=lambda m: True)
def send_welcome(message):
msg = bot.send_message(message.from_user.id, """\
Hi there, I am Example bot.
What's your name?
""", allow_sending_without_reply=True)
bot.register_next_step_handler(msg, another_test_step)
def another_test_step(message):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('1', '2', '3')
bot.send_message(message.from_user.id, 'Choose your number', reply_markup=markup, allow_sending_without_reply=True)
bot.register_next_step_handler(message, after_key)
def after_key(message):
message = bot.send_message(message.from_user.id, 'You chose ' + message.text + '. Do you want to repeat?')
bot.register_next_step_handler(message, yet2_test_step)
def yet2_test_step(message):
if message.text == "yes":
another_test_step(message)
bot.register_next_step_handler(message, after_key)
elif message.text == 'no':
bot.register_next_step_handler(message, send_welcome)
This option works as I need, but causes a bug https://ibb.co/LxtH64z. So I still dont know how to deal with this problem