不知道如何处理PyAPI中的几个回调

2024-06-25 23:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我期待着制作某种电报。所以它是基于内联键盘的。用户应按下ILNLINE按钮选择棋子,然后按下按钮放置棋子。我不知道在选择了一个棋子之后,如何在inlineKeyboard的“board”上选择一个空闲的单元格,只是现在。我试着做bot.register\u next\u step\u 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))

所以我希望进程跳转到进程移动步骤,等待新的回调并在那个里检查它,但在得到“典当”回调和“空”回调之后,我得到了来自else的结果:part而不是if

if call.data.partition('empty')[1] == "empty":

那个么我怎样才能从回调中得到“pawn”单元格,然后是“empty”单元格,然后完成函数呢。“empty”代表对象EmptyCell,它有属性X和Y,所以我可以将兵移动到Board obj中的确切位置,并编辑内联键盘。我在@TrueMafiaBot中看到过类似的东西。当警察被问到是否要检查或射杀某人时,他会挑选一名球员进行选择


Tags: boardsendidmessagedatamoveifbot
2条回答

最直接的方法是在运行bot时保持某种状态,并对回调查询应答执行if-else检查。例如,您可以执行以下操作:

from enum import Enum
from dataclasses import dataclass


class Color(Enum):
    WHITE = 0
    BLACK = 1


@dataclass
class GameState:
    chat_id: int
    turn: Color
    holding_chessman: Chessman
    # ...another useful data for game flow


# data is your callback data received from users
# gamestate is local instanse of GameState on server (or database)

# inside callback answer handling:
if gamestate.turn != data.user.color:
    return error('Not your turn, hold on!')
if gamestate.holding_chessman is None:
    gamestate.holding_chessman = data.pressed_figure
    return info('Now press a button where to move a chessman.')
else:
    perform_chessman_move()
    switch_turn()
    return info('You moved a chessman, now another user must turn')

它没有像你期望的那样工作。每个请求总是传递给主函数(回调函数)。因此,如果您尝试在选择典当后捕获下一步,您应该保存用户的当前状态。如果用户选择pawn,则其状态设置为\u pawn\u selected=true。在此之后,您可以添加一些逻辑来处理此状态。在您的情况下,应该是这样的:

 if (users.get(user_ID).is_pawn_selected) { 
    user.is_pawn_selected = false
    process_move_step 
}

相关问题 更多 >