Search code examples
pythoninputtext-to-speechpy-telegram-bot-api

telebot: How to take a massage in input here?


I don't know how to take a message in input here?

@bot.message_handler(commands=['tts'])
def send_text_request(message):
    bot.reply_to(message, "Write a phrase")
    #input...

Solution

  • You can manage it in two different ways:

    • Using custom_state, a new feature from recent releases. After tts command you can set the state of user at 1, with:

      bot.set_state(message.from_user.id, '1')
      

      and create a new handler:

      @bot.message_handler(state = '1'):
      def new_function(message):
          ....
      

      At the end of the code, add:

      from telebot import custom_filters
      bot.add_custom_filter(custom_filters.StateFilter(bot))
      

      Source: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/custom_states.py

    • Another way could be using register_next_step_handler, but honestly I used it only once and then I defined my personal states, because it's a mess. But I itroduce you to it anyway. Define a new function that will handle the input from the user, so:

      def function(message):
          ....
      

      Change the code you already wrote with the following:

      @bot.message_handler(commands=['tts'])
      def send_text_request(message):
          msg = bot.reply_to(message, "Write a phrase")
          bot.register_next_step_handler(msg, function)
      

    That's all!