我有 2 个函数,这两个函数都编辑机器人(远程机器人)的消息,其中 1 个函数,当另一个函数随后调用时,应该阻止它编辑消息
用户可以编写“搜索”并随内联键盘一起接收消息,但是通过再次调用“搜索”,之前使用键盘的消息应更改为消息“键盘不再可用”并丢失键盘。使用内联键盘,用户还可以更改消息并为同一消息生成新键盘(因为他必须使用它输入多个参数)。但可能存在一种情况,用户在更改消息之前发送“搜索”一词后,单击内联按钮 - 在这种情况下,我希望消息仍然不执行以下代码中的这部分代码@bot.callback_query_handler():
global users_keyboard_data
users_keyboard_data = change_to_other_inline_keyboard(bot, call, users_keyboard_data)
也就是说,只有这部分应该在以下位置工作(因为它是首先调用的)@bot.message_handler():
if message.text == "Поиск":
global users_keyboard_data
users_keyboard_data = search(bot, message.chat.id, users_keyboard_data)
我没有复制所有代码,而是复制主要工作片段。我还想澄清一下,全局变量users_keyboard_data负责存储从内联键盘接收到的用户参数;如果有更简单的方法来存储它,请告诉我。
from telebot import TeleBot
from telebot.types import Message, CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup
from settings import config
users_keyboard_data = {}
bot = TeleBot(config.TOKEN)
def build_inline_keyboard(buttons: list[str]) -> InlineKeyboardMarkup:
return InlineKeyboardMarkup().add(*[InlineKeyboardButton(text=button, callback_data=button) for button in buttons])
def get_keys_if_chat_id_in_dict(chat_id: int, dictionary: dict) -> list[str]:
keys = []
for key in dictionary:
if key.split("_")[0] == str(chat_id):
keys.append(key)
return keys
def delete_previous_searches_besides(bot: TeleBot, id: int, users_keyboard_data: dict[dict[str: str]], besides: Message) -> dict[dict[str: str]]:
to_delete_lst = get_keys_if_chat_id_in_dict(id, users_keyboard_data)
to_delete_lst.remove(f"{id}_{besides.message_id}")
for key in to_delete_lst:
users_keyboard_data.pop(key, None)
bot.edit_message_text(chat_id=id, message_id=int(key.split("_")[1]), text="Клавиатура более недоступна")
return users_keyboard_data
def search(bot: TeleBot, id: int, users_keyboard_data: dict[dict[str: str]]) -> dict[dict[str: str]]:
message = bot.send_message(chat_id=id, text="Выберите округ", reply_markup=build_inline_keyboard(["Кнопка 1", "Кнопка 2"]))
users_keyboard_data[f"{id}_{message.message_id}"] = {}
users_keyboard_data = delete_previous_searches_besides(bot, id, users_keyboard_data, message)
return users_keyboard_data
def change_to_other_inline_keyboard(bot: TeleBot, call: CallbackQuery, users_keyboard_data: dict[dict[str: str]]) -> dict[dict[str: str]]:
users_keyboard_data[f"{call.from_user.id}_{call.message.message_id}"] = {"FIRST_DATA": f"{call.data}"}
bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.id, text="Новая клавиатура",
reply_markup=build_inline_keyboard(["Новая кнопка 1", "Новая кнопка 2"]))
return users_keyboard_data
@bot.message_handler()
def handle_message(message: Message) -> None:
if message.text == "Поиск":
global users_keyboard_data
users_keyboard_data = search(bot, message.chat.id, users_keyboard_data)
@bot.callback_query_handler(func=lambda call: True)
def handle_query(call: CallbackQuery) -> None:
global users_keyboard_data
users_keyboard_data = change_to_other_inline_keyboard(bot, call, users_keyboard_data)
if __name__ == "__main__":
bot.polling(none_stop=True)
我尝试这样做:
@bot.callback_query_handler(func=lambda call: True)
def handle_query(call: CallbackQuery) -> None:
if call.message.text != "Клавиатура более недоступна":
global users_keyboard_data
users_keyboard_data = change_to_other_inline_keyboard(bot, call, users_keyboard_data)
但即使远程机器人库中缺乏异步性,它仍然接受该条件为真。