首页 > 解决方案 > 处理来自多用户的多请求

问题描述

我用 php 制作了一个电报机器人 我有一个使用机器人的小组。我的问题是当几个人单击一个内联键盘按钮时,我希望该按钮对第一个单击的用户起作用,而对同时单击的其余用户不起作用。我该如何处理必须执行一次的请求?

标签: phptelegram-bot

解决方案


抱歉,我不懂 PHP,所以我的例子是用 Python 编写的。首先,添加内联按钮按下处理程序。例如

# when initialising the bot
dispatcher.add_handler(CallbackQueryHandler(bot.inline_button_press))

其次,在按钮的回调数据中定义某种 id。例如

# when creating the inline buttons
button_value_dict = {
    "ID": "XYZ"
}
button_value = json.dumps(button_value_dict)

key_row.append(
    InlineKeyboardButton(button_text, callback_data=button_value)
)

第三,在处理程序中,获取按钮数据,并在某种形式的持久存储中检查 ID 是否已被使用/寻址。如果没有,请将其添加到存储中并执行操作。函数(get_meta 和 set_meta)只是示例。例如

# in the routine that handles inline buttons presses
# inline_button_press in the example above
# update is a passed parameter

query = update.callback_query
button_data = query.data
button_data_dict = json.loads(button_data)
button_id = button_data_dict.get("ID")

var_value = get_meta (var=button_id)
if var_value is None:

    # first time button press
    # record this ID as used
    set_meta (var=button_id, value="used")

    # do your stuff

else:
    # someone has already pressed the button
    # do whatever

推荐阅读