php - 处理来自多用户的多请求
问题描述
我用 php 制作了一个电报机器人 我有一个使用机器人的小组。我的问题是当几个人单击一个内联键盘按钮时,我希望该按钮对第一个单击的用户起作用,而对同时单击的其余用户不起作用。我该如何处理必须执行一次的请求?
解决方案
抱歉,我不懂 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
推荐阅读
- javascript - mouseDown 和 mousePress javascript 的区别
- c++ - 如何在视网膜显示器上使用 QPainter、QOpenGLPaintDevice 和 QOpenGLWidget 平滑绘制?
- c# - Microsoft.Configuration.Extensions:如何获取节/复杂值作为 json 字符串?
- html - css 动画 - 鼠标平滑
- javascript - AngularJS:如何根据条件检查 state.go 中的 url 选择?
- java - JavaFX:在数据准备好之前禁用大部分阶段
- angular - 为什么我在尝试通过 id 获取元素时得到 null?
- sql-server-2012 - 下划线和 LEFT 函数
- python - 如何将 None 存储为 JSON 'null' 而不是 SQL NULL?
- amazon-web-services - AWS KMS 如何确定解密时使用哪个密钥?