【问题标题】:How to detect if a message contains the inline keyboard如何检测消息是否包含内联键盘
【发布时间】:2017-09-12 22:51:50
【问题描述】:

Telegram 机器人使用频道中的消息执行一些任务。它应该跳过带有内嵌按钮的消息(例如,带有投票按钮)。

有没有办法确定发布的消息是否包含内联键盘?

Message 对象似乎不包含类似的东西。 editReplyMarkup 只能换键盘了……

为了更清楚。

这是序号消息:

这是带有按钮的消息:

【问题讨论】:

    标签: telegram-bot


    【解决方案1】:

    机器人如何处理消息? 您必须检查输入 Update 对象。

    更新:

    from telegram import InlineKeyboardButton, InlineKeyboardMarkup
    from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
    
    def start(bot, update):
        print(update)
        keyboard = [[InlineKeyboardButton("one", callback_data='1'),
                    InlineKeyboardButton("two", callback_data='2')],
                    [InlineKeyboardButton("tree", callback_data='3')]]
        reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text('Please choose:', reply_markup=reply_markup)
    
    
    def button(bot, update):
        print(update)
        query = update.callback_query
        bot.edit_message_text(text="Your choose: %s" % query.data,
            chat_id=query.message.chat_id,
            message_id=query.message.message_id)
    
    
    up = Updater('***TOKEN***')
    up.dispatcher.add_handler(CommandHandler('start', start))
    up.dispatcher.add_handler(CallbackQueryHandler(button))
    up.start_polling()
    

    返回不同的

    消息:

    {消息:{message_id:20,new_chat_members:[],new_chat_member:无,日期:0000000001,delete_chat_photo:假, 来自:{id:000000005,用户名:用户名,名字:姓名,语言代码:en-US},实体:[{偏移量:0,长度:6, type: bot_command}], chat: {id: 000000005, username: username, first_name: name, type: private}, new_chat_photo: [], group_chat_created: False, photo: [], supergroup_chat_created: False, text: /start, channel_chat_created: False}, update_id: 000000051}

    内联:

    {callback_query:{chat_instance:-0000000000000000060,消息:{message_id:21,new_chat_members:[], new_chat_member:无,日期:1505211901,delete_chat_photo:False,来自:{id:000000005,用户名:botname, 名字:测试},实体:[],聊天:{id:000000005,用户名:用户名,名字:名字,类型:私人}, new_chat_photo: [], group_chat_created: False, photo: [], supergroup_chat_created: False, text: 请选择:, channel_chat_created:False},数据:2,id:0000000000000000094,来自:{id:000000005,用户名:用户名, first_name: 姓名, language_code: en-US}}, update_id: 000000005}

    【讨论】:

    • “getUpdate”方法返回“更新”对象,其中包含对象“消息”的填充“channel_post”字段(用于频道)。据我所知,那里没有其他东西可以检查。 :(
    • 您检查过inlinenot inline 对象之间的区别吗?我为小例子编辑了帖子
    • 这不是我在说的:没有回调。我已经用示例更新了帖子。我应该确定发布到频道(由其他成员)的消息是否包含内联键盘(第二个示例)或不包含(第一个示例)。是的,我使用相同的 python bot 引擎。 :)
    • 你的意思是,频道中的许多机器人会发送不同的消息,而你的机器人会识别消息的类型?如果未按下,则不包括键盘以进行查询。据我所知,客户端可以,但机器人不行。
    • 是的,那里有几个机器人。 IE。用户有时会通过机器人发布帖子以嵌入投票或订阅等按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2022-08-19
    相关资源
    最近更新 更多