【问题标题】:How to receive multiple messages in python-telegram-bot?如何在 python-telegram-bot 中接收多条消息?
【发布时间】:2020-08-10 13:46:18
【问题描述】:

我一次向电报中的机器人发送多张图像。我正在尝试使用 python-telegram 机器人创建一个conversational chatbot

这是我的代码:

def main():
updater = Updater("1141074258:Axxxxxxxxxxxxxxxxxxxxxxxxg", use_context=True)

dp = updater.dispatcher
conv_handler = ConversationHandler(
    entry_points = [CommandHandler('start',start)],

    states = {
    CHOSEN_OPTION: [MessageHandler(Filters.regex('^(Option2|Option3|Option4)$'),choose_option)],
    PRODUCTS: [MessageHandler(Filters.text | Filters.photo,products)],
    Option2: [MessageHandler(Filters.text,option2)],
    Option3: [MessageHandler(Filters.text,option3)],
    Option4: [CommandHandler('create', create_order)]
    },
    fallbacks=[CommandHandler('cancel', cancel)]
)
dp.add_handler(conv_handler)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()



@run_async
def products(update,context):
    logger.info("update is %s",update)
    input_message = update.message.text
    if input_message:
        data['products'] = input_message
        logger.info("product  text is:%s",input_message)
    elif update.message.photo:
        photo_list = []
        bot = context.bot
        length = len(update.message.photo)
        for photo in range(0,length):
            ident = update.message.photo[photo].file_id
            getFile = context.bot.get_file(ident)
            photo_list.append(getFile['file_path'])
        data['products_image'] = photo_list
    update.message.reply_text("Please type name.",)
    return Option3

如果我同时发送 2 张图片,我会收到一张不同大小的图片(3 次),我如何才能收到实际的两条消息?

【问题讨论】:

  • 可以分享一下你的产品功能吗?
  • 哦,你的产品功能有问题。在获得第一张照片后,您返回等待文本的 Option3,然后转到 option3 函数。并且不接受照片
  • 当您在更新中有照片时返回产品
  • @mujad 是的,这正是问题所在。我该如何解决这个问题?我如何知道返回选项 3 的消息结束?
  • 你到底想做什么?

标签: telegram-bot python-telegram-bot


【解决方案1】:

如果更新包含照片返回产品以获取其他照片 获取文本并返回到您想要的每个状态

@run_async
def products(update,context):
    logger.info("update is %s",update)
    if update.message.photo:
        # to what you want with your photos
        
        return PRODUCTS
    
    if update.message.text:
        # getting product text 
        
        return Option3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2020-03-26
    • 2018-04-20
    相关资源
    最近更新 更多