RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-566893

shadowflexer's questions

Martin Hope
shadowflexer
Asked: 2024-11-13 23:24:48 +0000 UTC

如何防止在 Telebot 中更改消息后处理内联按钮?

  • 5

我有 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)

但即使远程机器人库中缺乏异步性,它仍然接受该条件为真。

python
  • 1 个回答
  • 35 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5