0
votes

I'm trying to read a book name from a user after asking the question: What book are you looking for? How can I save the user's response in a variable for use in my algorithms?

def bookinfo(bot, update):
    chat_id = update.message.chat_id
    bot.send_message(chat_id=chat_id, text='What book are you looking for?????')
    dp.add_handler(MessageHandler(Filters.text))
    BOOK_NAME = update.message.text
    BOOK_NAME = str.lower(BOOK)
    answer = 'You have wrote me ' + BOOK_NAME
    bot.send_message(answer)
    
updater = Updater('TOKEN')
dp = updater.dispatcher
    
dp.add_handler(CommandHandler('bookinfo', bookinfo))

updater.start_polling()
updater.idle()

The question is asked, but the bot does not respond by sending the message with the name of the book... Many thanks in advance!

1

1 Answers

0
votes

at first always get chat_id from your update like this:

chat_id = update.effective_user.id

and also send_message method need a chat_id to send it you have two choice to answer to this update:

  • even don't need to add handler to your dispatcher
bot.send_message(chat_id, message)
update.message.reply_text(message)
def bookinfo(bot, update):
    update.message.reply_text(text='What book are you looking for?🔎')
    

def get_bookinfo(bot, update):
    book_name = update.message.text
    book_name = str.lower(book_name)
    # TODO: do what you want with book name

    answer = f'You have wrote me {book_name}'  
    update.message.reply_text(answer)
    

updater = Updater('TOKEN')
dp = updater.dispatcher
dp.add_handler(CommandHandler('bookinfo', bookinfo))
dp.add_handler(MessageHandler(Filters.text, get_bookinfo))
updater.start_polling()
updater.idle()