就上下文而言,我使用 pyTelegramBotAPI 来构建飞行跟踪器。我觉得下面的代码是正确的,但由于某种原因,当我单击任何内联按钮时,我只是在按钮上看到一个时钟图标,但没有任何响应。这就是我传递信息的方式,这个函数本身运行良好并且每次都会运行:
@bot.message_handler(commands=["start"], func=lambda msg: is_user(msg.chat.id))
async def start_is_user(m):
cid = m.chat.id
if next_step_handler(cid) == 'menu' or next_step_handler(cid) == 0: #function to define my next step
currUser = db.users.find_one(str(cid)) #using mongodb here to get the currUser
userStep[cid] = 'menu' #dict to keep track of the current step of the currUser
await bot.send_chat_action(cid, "typing")
await bot.send_message(
cid,
(f"Hello {currUser['fname']}, I'm a bot programmed"
" to find you the best tickets home. To get started click on the following 👇"),
reply_markup=inline_generator(menu_options)
)
这就是我的 inline_generator 函数生成键盘的方式。我已经包含了上下文的 menu_options:
menu_options = [
{'id': 1, 'name': 'New Flight'},
{'id': 2, 'name': 'Track Flights'},
{'id': 3, 'name': 'Settings'},
]
#fairly simple code to produce a grid of keys with width 2
def inline_generator(options):
temp = []
curr = 0
keyboard = []
while curr < len(options):
while len(temp) < 2 and curr<len(options): #looks redundant but len(temp) can be under 2 and have curr>len(options) which I don't want
temp.append(types.InlineKeyboardButton(text=options[curr]['name'], callback_data=options[curr]['id']))
curr += 1
keyboard.append(temp)
temp = []
return types.InlineKeyboardMarkup(keyboard=keyboard)
这里的问题再次是,每次我单击内联键盘中的某些内容时,我似乎没有收到任何回调数据,因为我没有输入回调查询处理程序,它看起来像这样:
@bot.callback_query_handler(func=lambda call: True)
async def menu_callback_query(call):
cid = call.message.chat.id
await bot.answer_callback_query(call.id)
await bot.send_chat_action(cid, "typing")
print(f"reached menu_callback_query and current call is {call.data}") #this print statement is never reached
if call.data == 0:
userStep[cid] = "menu"
if call.data == 1 :
await bot.edit_message_text(
text=responses["ticket_type"],
chat_id=cid,
reply_markup=inline_generator(ticket_options),
)
userStep[cid] = "ticket_type"
if call.data == 2:
await bot.answer_callback_query(call.id, responses["no_flights"])
if call.data == 3:
await bot.answer_callback_query(call.id, responses["not_user"])
我尝试使用消息处理程序而不是回调查询处理程序,但这不起作用。我还将回调查询处理程序中的 func 更改为 None 而不是 (func=lambda call: True),但这没有改变任何内容。
任何帮助理解如何将其调试到我的控制台的帮助也将不胜感激!这是我第一次为学校项目编码,所以我不确定捕获此类错误的最佳实践是什么