You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyrogram无法读取含Premium Emoji/引用的消息,如何实现关键词转发?

问题描述

基于Pyrogram开发的Python程序,在处理包含Premium Emoji(高级表情)或引用内容的消息时,无法读取消息文本。需求是:当消息中存在指定关键词时,接收并转发该消息。

我的代码

from pyrogram import Client,filters
client = Client("my_account", api_id, api_hash) 
@client.on_message(filters.channel & filters.chat(channels))
async def my_handler(client, message):
    print(message)

打印的Message对象

{
    "_": "Message",
    "id": 309,
    "sender_chat": {
        "_": "Chat",
        "id": --------,
        "type": "ChatType.CHANNEL",
        "is_verified": false,
        "is_restricted": false,
        "is_creator": false,
        "is_scam": false,
        "is_fake": false,
        "title": "test",
        "username": "-------",
        "has_protected_content": false
    },
    "date": "2024-04-12 17:57:43",
    "chat": {
        "_": "Chat",
        "id": -------,
        "type": "ChatType.CHANNEL",
        "is_verified": false,
        "is_restricted": false,
        "is_creator": false,
        "is_scam": false,
        "is_fake": false,
        "title": "-------",
        "username": "-------",
        "has_protected_content": false
    },
    "forward_from_chat": {
        "_": "Chat",
        "id": --------,
        "type": "ChatType.CHANNEL",
        "is_verified": false,
        "is_restricted": false,
        "is_creator": false,
        "is_scam": false,
        "is_fake": false,
        "title": "-------",
        "username": "-------",
        "photo": {
            "_": "ChatPhoto",
            "small_file_id": "AQADAgADKdIxG9-bCEgAEAIAAzSnBNwW____ow5QrQzS_tIABB4E",
            "small_photo_unique_id": "AgADKdIxG9-bCEg",
            "big_file_id": "AQADAgADKdIxG9-bCEgAEAMAAzSnBNwW____ow5QrQzS_tIABB4E",
            "big_photo_unique_id": "AgADKdIxG9-bCEg"
        },
        "dc_id": 2,
        "has_protected_content": false
    },
    "forward_from_message_id": 14845,
    "forward_date": "2024-04-03 13:29:22",
    "mentioned": false,
    "scheduled": false,
    "from_scheduled": false,
    "has_protected_content": false,
    "views": 1,
    "forwards": 0,
    "outgoing": true
}

解决方案

直接打印message对象无法看到完整文本,是因为Pyrogram将富内容(高级表情、引用)的文本存储在特定字段中,需要针对性提取:

1. 提取完整消息文本

优先使用message.text获取普通文本,没有则取message.caption(媒体消息的说明文本)。如果需要带格式的完整内容,可用message.text_markdown或message.text_html:

from pyrogram import Client, filters

client = Client("my_account", api_id, api_hash)

@client.on_message(filters.channel & filters.chat(channels))
async def my_handler(client, message):
    # 合并主文本和媒体说明文本
    full_text = message.text or message.caption or ""
    
    # 若需要带格式文本,替换成下面一行
    # full_text = message.text_markdown or message.caption_markdown or ""
    
    # 设置要检测的关键词
    target_keywords = ["指定关键词1", "指定关键词2"]
    
    # 检查是否包含目标关键词
    if any(keyword in full_text for keyword in target_keywords):
        # 转发到目标聊天,替换为实际目标ID
        await message.forward(chat_id=你的目标聊天ID)
    
    # 打印完整文本用于调试
    print(f"完整消息内容: {full_text}")

2. 处理引用(回复)消息

如果消息是引用其他消息,引用内容存于message.reply_to_message字段,需单独提取合并:

async def my_handler(client, message):
    full_text = message.text or message.caption or ""
    
    # 提取引用的消息内容
    if message.reply_to_message:
        reply_content = message.reply_to_message.text or message.reply_to_message.caption or ""
        full_text += f"\n引用内容: {reply_content}"
    
    # 关键词检测与转发逻辑同上

3. 高级表情的处理

高级表情会被Pyrogram自动解析为Unicode编码,直接包含在message.text中,无需额外处理。若需查看表情的实体信息,可遍历message.entities字段。


内容的提问来源于stack exchange,提问作者BATRAZ ZALEEV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 23:49:52