0

I have written a simple telegramm bot with pyTelegramBotAPI. The dialog I need should look like this:

  1. You: 'something'
  2. Bot: Hi there, I am Example bot. What's your name?
  3. You: 'some name'
  4. Bot: 'Choose your number' (shows keyboard with numbers)
  5. You: chose the number
  6. Bot: 'You chose 1. Do you want to repeat?'
  7. You: 'yes'
  8. 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:

  1. You: 'something'
  2. Bot: Hi there, I am Example bot. What's your name?
  3. You: 'some name'
  4. Bot: 'Choose your number' (shows keyboard with numbers)
  5. You: chose the number
  6. Bot: 'You chose 1. Do you want to repeat?'
  7. You: 'yes'
  8. You: 'something'
  9. 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()

How it works in telegram now

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

Roman C
  • 1
  • 2

1 Answers1

0

Handler functions only work when they get messages. So you only register "another_test_step", but do not call it before next message. You can just call this function manually in yet2_test_step, and register after_key as next (which will do your after_key).

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":
        # CHANGES HERE
        another_test_step(message)
    elif message.text == 'no':
        bot.register_next_step_handler(message, send_welcome)


bot.enable_save_next_step_handlers(delay=2)
eightlay
  • 423
  • 2
  • 9
  • Thank you for your answer. Your method works, but unfortunately causes a very strange bug, with each choice if, the number of messages from the bot grows by 2 times (screenshot: https://ibb.co/LxtH64z) – Roman C Jul 11 '22 at 19:29