簡體   English   中英

獲取輸入用戶 Python Telegram Bot

[英]Get input user Python Telegram Bot

在提出以下問題后,我試圖從用戶那里讀取書名:您在尋找什么書? 如何將用戶的響應保存在變量中以用於我的算法?

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()

有人問了這個問題,但機器人沒有通過發送帶有書名的消息來回應......提前非常感謝!

起初總是從你的更新中獲取chat_id,如下所示:

chat_id = update.effective_user.id

並且 send_message 方法需要一個 chat_id 來發送它你有兩個選擇來回答這個更新:

  • 甚至不需要將處理程序添加到您的調度程序
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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM