[英]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.