簡體   English   中英

我編寫了我的電報機器人(在 python 中)提示用戶發送圖像。 如何確保用戶只發送圖像而不發送其他任何內容?

[英]I coded my telegram bot (in python) prompting user to send an image. How do I make sure users only send image and nothing else?

def process_color_step(message):
    chat_id = message.chat.id
    product.color = message.text
    msg = bot.send_message(chat_id, 'ok, send me a photo of your phone')
    bot.register_next_step_handler(msg,process_image_step)

def process_image_step(message):
    chat_id = message.chat.id
    product.image = message.photo[1].file_id
    msg = bot.send_message(chat_id, 'Ok, send me a description of your phone?')
    bot.register_next_step_handler(msg,process_description_step)

以上是代碼片段。 圖像 file_id 被傳遞到 product.image。

我會使用圖像庫來嘗試加載文件。 枕頭之類的東西可以解決問題。

您可以將消息處理程序用作處理程序列表。 例如在您的對話處理程序中:

conv_handler = ConversationHandler(
    entry_points=[CommandHandler('start', start)],
    states={
        PHOTO: [MessageHandler(Filters.photo, 'your function for getting photo),
                MessageHandler(Filters.all, 'your function for wrong entry),]
}
)

在這種情況下,如果用戶更新不是照片,第一個獲取照片的 MessageHandler 是不可接受的,因此處理程序嘗試捕獲列表中的下一個 MessageHandler:

MessageHandler(Filters.all, 'your function for wrong entry) 

您必須定義一個函數來響應用戶錯誤輸入並將其放入處理程序列表的第二個元素中。

暫無
暫無

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

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