Expect user response in Telegram - Python

Asked

Viewed 148 times

-2

How can I expect user response after executing a command? I am using Python 3.6 and the lib Telegram-bot-python.

Example:

usuario - /apel
bot - Qual o seu apelido?
usuario - dfop
bot - Uau, seu apelido é dfop

I’ve made it so far:

def apel(bot, update):
        update.message.reply_text('Qual o seu apelido?')
        resposta = ??
        bot.send_message(chat_id = update.message.chat_id, text = "Uau, seu apelido é %s", resposta)

updater = Updater(token)
dispatcher = self.updater.dispatcher
dispatcher.add_handler(CommandHandler('/apel', apel))
updater.start_polling()
updater.idle()
  • It looks a lot like your last question: https://answall.com/q/335178/5878. What’s the difference?

  • I thought the last one got a little confused so I re-asked the question trying to simplify as much as possible to make it easier to read. I can delete the previous post if it will give any problem.

  • You could have edited the previous question in the edit button just below the tag's question. But now that you’ve done this and the other has no interaction, it’s best to remove the old.

1 answer

-4

I need to know some information about your project.

Type:

 - Qual a biblioteca que você está usando ?
 - Qual é a versão do python utilizada ?

Let’s say you are using the pyTelegramBotAPI library:

  • Use the register_next_step_handler method()

Example using this method in its code:

 def apel(bot, update):
    msg = update.message.reply_text('Qual o seu apelido?')
    bot.register_next_step_handler(msg, nome_de_uma_funcao)
    bot.send_message(chat_id = update.message.chat_id, text = "Uau, seu apelido é %s", resposta)


def nome_de_uma_funcao(message):
  # Guardando a resposta do usuario em uma variavel
  resposta = message.text
  print(resposta)

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.