不要使用Pyrogram的inlinekeyboard发送消息
创始人
2025-01-10 22:00:22
0

在 Pyrogram 中使用菜单时,通常通过 InlineKeyboardButton、InlineKeyboardMarkup 和 CallbackQueryHandler 等类和方法来创建和处理菜单。为了防止用户在菜单中发送消息,我们可以在菜单中添加一个“禁止发送消息”的回调函数,并在用户或机器人尝试发送消息时执行该回调函数。

示例代码如下:

from pyrogram import Client, InlineKeyboardButton, InlineKeyboardMarkup
from pyrogram.api import functions
from pyrogram.api.types import InputPeerChat

# 创建一个名为 "menu" 的回调函数
def menu(client, update):
    # 构建菜单按钮
    button_list = [
        [
            InlineKeyboardButton("按钮1", callback_data="btn1"),
            InlineKeyboardButton("按钮2", callback_data="btn2"),
        ],
        [InlineKeyboardButton("禁止发送消息", callback_data="dont_send")],
    ]
    # 创建 InlineKeyboardMarkup
    reply_markup = InlineKeyboardMarkup(button_list)

    # 发送菜单并获取消息 ID
    message = client.send_message(
        chat_id=update.chat.id,
        text="菜单",
        reply_markup=reply_markup
    )
    message_id = message.message_id

    # 将消息 ID 存储到 `data` 数据中,以备后续使用
    client.set_data(update.chat.id, "last_menu_message_id", message_id)

# 创建一个名为 "dont_send_message" 的回调函数,用于禁止用户发送消息
def dont_send_message(client, update):
    chat_id = update.chat.id
    message_id = client.get_data(chat_id, "last_menu_message_id")
    user_id = update.from_user.id

    # 如果是机器人发送的消息,不做任何处理
    if update.from_user.is_bot:
        return

    # 如果用户尝试发送消息,使用 `delete_messages` API 删除该消息
    client.send(functions.messages.DeleteMessages(
        peer=InputPeerChat(chat_id),
        id=[update.message.message_id]
    ))

    # 异步发送警告信息
    asyncio.ensure_future(client.send_message(
        chat_id=chat_id,
        text=f"禁止发送消息\n您在菜单中尝试发送消息,但已被禁止。\n\nUser ID: {user_id}",
        reply_to_message_id=message_id,
        parse_mode="html"
    ))

# 创建 Pyrogram 客户端并注册回调函数
app = Client("my_bot

相关内容

热门资讯

wepok软件透明挂!德州竞技... wepok软件透明挂!德州竞技联盟辅助,(wePOke)起初存在有挂(详细辅助2025新版教程)关于...
透视教程(德州微扑克辅助)微扑... 透视教程(德州微扑克辅助)微扑克真的有挂吗(详细辅助我来教教你)其实有挂(1)透视教程(德州微扑克辅...
aapoker有猫腻!德州ai... 《aapoker有猫腻!德州ai机器人辅助,(aa扑克)先前是有挂(详细透视细节方法)》 德州ai机...
透视软件(wepoke的确有挂... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
aapoker挂!aapoke... aapoker挂!aapoker透视辅助,(wepoker)原来真的是有挂(详细透视技巧教程);科技...
透视攻略(wepoke黑科技)... 一、简介了解软件请加微:136704302是一款在线扑克游戏平台,玩家可以在平台上进行多人在线扑克游...
德州微扑克辅助!德州poker... 德州微扑克辅助!德州poker有外挂吗,(nzt德州)最初是有挂(详细辅助必备教程);最新版2024...
透视线上(wepower有外挂... 透视线上(wepower有外挂)wpk专用辅助程序(详细辅助总结教程)一向是有挂;一、有挂的是的,亲...
aapoker透视辅助!wep... 您好,aapoker透视辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视系统(wepoke的确有挂... 1、透视系统(wepoke的确有挂)wepoke辅助透(详细辅助解密教程)原生真的是有挂;详细教程。...