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

如何通过Telethon中单个贴纸查询其所属贴纸包的id及access_hash

解决方法

你从message中获取到的贴纸对应的Document对象,其attributes列表里包含DocumentAttributeSticker类型的专属属性,该属性的stickerset字段直接存储了所属贴纸包的id和access_hash,无需额外调用其他接口。

完整实现代码如下:

from telethon.tl.types import DocumentAttributeSticker, InputStickerSetID
from telethon.tl.functions.messages import GetStickerSetRequest

async for message in client.iter_messages(group_name):
    if not message.sticker:
        continue
    # 遍历属性找到贴纸专属属性
    sticker_attr = None
    for attr in message.sticker.attributes:
        if isinstance(attr, DocumentAttributeSticker):
            sticker_attr = attr
            break
    # 校验属性和贴纸包字段是否存在
    if not sticker_attr or not hasattr(sticker_attr, 'stickerset'):
        continue
    # 获取贴纸包信息
    stickers = await client(GetStickerSetRequest(
        stickerset=InputStickerSetID(
            id=sticker_attr.stickerset.id,
            access_hash=sticker_attr.stickerset.access_hash
        )
    ))
    # 打印贴纸包包含的贴纸数量测试
    print(f"当前贴纸所属包共包含{len(stickers.documents)}张贴纸")

注意事项

  • 你之前代码中误用了消息对象的id和access_hash作为贴纸包的参数,这会导致请求报错,必须从Document的贴纸属性中读取对应字段
  • 极少数自定义的特殊贴纸可能没有关联贴纸包,代码中加入的校验逻辑可以避免这类场景下的运行报错

内容的提问来源于stack exchange,提问作者logos G

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 08:27:03