不知道如何处理pyTelegramBotApi中的多个回调

问题描述 投票:0回答:1

我期待制作某种电报象棋机器人。因此它是基于嵌入式键盘的。用户应该按下ilnlineButton来选择一个棋子,然后按下按钮来放置一个棋子。我不知道如何在仅选择了pawn之后在inlineKeyboard的“ board”中选择一个空闲单元。我尝试做bot.register_next_step_handler,但他没有给出我期望的结果。

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    try:
        if call.message:
            if call.data == "suck":
                bot.send_message(call.message.chat.id, "Just wait a bit, OK?")
            elif call.data == "frick":
                bot.send_message(call.message.chat.id, "No, frick you")
            elif call.data == "sad":
                bot.send_message(call.message.chat.id, "Well, shit happens")
            elif call.data == "good":
                bot.send_message(call.message.chat.id, "I am soulless robot. How do      you think I can feel?")
            elif call.data.partition('pawn')[1] == "pawn":
                bot.register_next_step_handler(call.data, process_move_step)

else:
                bot.send_message(call.message.chat.id, call.data)
                bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=
                                                                                                      "some_text",reply_markup=None)

    except Exception as e:
        print(repr(e))


def process_move_step(call):
    try:
        if call.message:
            if call.data.partition('empty')[1] == "empty":
                next_move = new_board.get_chessman(call.data)
                new_board.move(call, next_move.X, next_move.Y)
                bot.send_message(call.message.chat.id, "Moved to "+str(next_move.X+str(next_move.Y)))
                print(new_board)

    except Exception as e:
        print(repr(e))

因此,我希望进程跳至process_move_step并等待新的回调并在那里进行检查,但是在获得“ pawn”回调而不是“空”回调之后,我得到了其他结果:part而不是if如果call.data.partition('empty')[1] ==“ empty”:因此,如何从回调中获取“ pawn”单元,然后获取“ empty”单元,然后完成功能。对于“空”,代表对象EmptyCell,它具有属性X和Y,因此我可以将棋子移到Board obj中的确切位置,并编辑嵌入式键盘。我在@TrueMafiaBot中看到了类似的内容。当警察被问到是否要检查或射击某人,然后他选择一名球员采取选定的行动。

python-telegram-bot pyhook
1个回答
0
投票

它没有按您预期的那样工作。每个请求总是传递给您的主函数(callback_inline)。因此,如果您在选择典当后尝试进行下一步,则应保存用户的当前状态。如果用户选择pawn,则其状态集为is_pawn_selected = true。之后,您可以添加一些逻辑来处理此状态。在您的情况下,应该是这样的:

 if (users.get(user_ID).is_pawn_selected) { 
    user.is_pawn_selected = false
    process_move_step 
}
© www.soinside.com 2019 - 2024. All rights reserved.