不知道如何处理 pyTelegramBotApi 中的多个回调
Don't know how to handle several callbacks in pyTelegramBotApi
我期待制作某种电报国际象棋机器人。所以它基于内联键盘。用户应该按下 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" 回调而不是获得 "empty" 回调之后我从其他地方得到了结果: if
部分而不是进入那个
if call.data.partition('empty')[1] == "empty":
那么我怎样才能从回调中获取 "pawn" 单元然后 "empty" 单元然后完成函数。因为 "empty" 代表对象 EmptyCell 并且它具有属性 X 和 Y,所以我可以将 pawn 移动到 Board obj 中的确切位置并编辑内联键盘。我在@TrueMafiaBot 中看到过类似的东西。当警官被问及他是否要检查或射击某人时,他会选择一名玩家来执行选定的动作。
它没有像您预期的那样工作。每个请求总是传递给您的主要功能 (callback_inline)。因此,如果您尝试在 pawn 选择后进行下一步操作,您应该保存用户的当前状态。如果用户选择 pawn,那么他的状态设置为 is_pawn_selected = true。在此之后,您可以添加一些逻辑来处理此状态。在你的情况下,它应该是这样的:
if (users.get(user_ID).is_pawn_selected) {
user.is_pawn_selected = false
process_move_step
}
最直接的方法是在 运行 机器人时保持某种状态并对回调查询答案执行 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')
我期待制作某种电报国际象棋机器人。所以它基于内联键盘。用户应该按下 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" 回调而不是获得 "empty" 回调之后我从其他地方得到了结果: if
部分而不是进入那个if call.data.partition('empty')[1] == "empty":
那么我怎样才能从回调中获取 "pawn" 单元然后 "empty" 单元然后完成函数。因为 "empty" 代表对象 EmptyCell 并且它具有属性 X 和 Y,所以我可以将 pawn 移动到 Board obj 中的确切位置并编辑内联键盘。我在@TrueMafiaBot 中看到过类似的东西。当警官被问及他是否要检查或射击某人时,他会选择一名玩家来执行选定的动作。
它没有像您预期的那样工作。每个请求总是传递给您的主要功能 (callback_inline)。因此,如果您尝试在 pawn 选择后进行下一步操作,您应该保存用户的当前状态。如果用户选择 pawn,那么他的状态设置为 is_pawn_selected = true。在此之后,您可以添加一些逻辑来处理此状态。在你的情况下,它应该是这样的:
if (users.get(user_ID).is_pawn_selected) {
user.is_pawn_selected = false
process_move_step
}
最直接的方法是在 运行 机器人时保持某种状态并对回调查询答案执行 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')