当我点击按钮时,按钮 id 应该返回给我。 JSON 个文件
When I click on the button, the button id should be returned to me. JSON file
我有一本字典,其中有一个问题及其编号。我遍历所有问题并将它们显示为按钮,然后当我单击按钮时我应该 return 这个问题的 id,如何做到这一点 ??
def start(message):
r = requests.get("http://x.x.x.x/api/tests/list/getusertestlist", headers=headers)# I get JSON file
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
for i in dp.values(r.json(), "/tests/*/value"):# value it's questions.
itembtn = types.KeyboardButton(i)
markup.add(itembtn)
bot.send_message(message.chat.id,"Get test",reply_markup=markup)
Structure JSON file.
{'tests': [{'description': 'Choose your preferred answer from the suggested ones.', 'id': 85, 'value': 'test 1'}, {'description': 'Choose your preferred answer from the suggested ones.', 'id': 88, 'value': 'test 1'}]}
最后我应该得到一个带有 2 个按钮的键盘测试 1 和测试 2,当我点击测试 1 时我应该得到它的 ID
我迷失了你的代码片段。 "dp" 或 "types" 等变量未声明。另外,我手头没有 json 互联网上的测试文件,所以我在同一个文件中声明了它(使用你给出的例子)。
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
import json
jsonfile='{"tests": [{"description": "Choose your preferred answer from the suggested ones.", "id": 85, "value": "test 1"}, {"description": "Choose your preferred answer from the suggested ones.", "id": 88, "value": "test 1"}]}'
def start(update, context):
keybrd = []
jsondic=json.loads(jsonfile)
for item in jsondic['tests']:
keybrd.append([InlineKeyboardButton(item['value'], callback_data=item['id'])])
markup = InlineKeyboardMarkup(keybrd)
context.bot.send_message(chat_id=update.effective_chat.id,text="Get test",reply_markup=markup)
def button(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(text="Selected option: {}".format(query.data))
def main():
updater = Updater(tgtoken, use_context=True)
updater.dispatcher.add_handler(CommandHandler("start", start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
就是这个意思,您可能需要重新排列按钮。您还需要使用 start_polling() 完成主函数并声明记录器,但所有这些细节都可以在这个包装器的 github 的内联键盘示例中找到。
我有一本字典,其中有一个问题及其编号。我遍历所有问题并将它们显示为按钮,然后当我单击按钮时我应该 return 这个问题的 id,如何做到这一点 ??
def start(message):
r = requests.get("http://x.x.x.x/api/tests/list/getusertestlist", headers=headers)# I get JSON file
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
for i in dp.values(r.json(), "/tests/*/value"):# value it's questions.
itembtn = types.KeyboardButton(i)
markup.add(itembtn)
bot.send_message(message.chat.id,"Get test",reply_markup=markup)
Structure JSON file.
{'tests': [{'description': 'Choose your preferred answer from the suggested ones.', 'id': 85, 'value': 'test 1'}, {'description': 'Choose your preferred answer from the suggested ones.', 'id': 88, 'value': 'test 1'}]}
最后我应该得到一个带有 2 个按钮的键盘测试 1 和测试 2,当我点击测试 1 时我应该得到它的 ID
我迷失了你的代码片段。 "dp" 或 "types" 等变量未声明。另外,我手头没有 json 互联网上的测试文件,所以我在同一个文件中声明了它(使用你给出的例子)。
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
import json
jsonfile='{"tests": [{"description": "Choose your preferred answer from the suggested ones.", "id": 85, "value": "test 1"}, {"description": "Choose your preferred answer from the suggested ones.", "id": 88, "value": "test 1"}]}'
def start(update, context):
keybrd = []
jsondic=json.loads(jsonfile)
for item in jsondic['tests']:
keybrd.append([InlineKeyboardButton(item['value'], callback_data=item['id'])])
markup = InlineKeyboardMarkup(keybrd)
context.bot.send_message(chat_id=update.effective_chat.id,text="Get test",reply_markup=markup)
def button(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(text="Selected option: {}".format(query.data))
def main():
updater = Updater(tgtoken, use_context=True)
updater.dispatcher.add_handler(CommandHandler("start", start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
就是这个意思,您可能需要重新排列按钮。您还需要使用 start_polling() 完成主函数并声明记录器,但所有这些细节都可以在这个包装器的 github 的内联键盘示例中找到。