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

如何用Python的aiogram获取Telegram中其他频道在群组的名称?

获取发送消息的频道名称(aiogram)

当频道向其他频道的讨论组发送消息时,message.from_user指向的是频道的官方机器人账号,所以你拿到的是Channel_bot和Channel。正确的发送方频道信息在message.from_chat属性里。

核心代码

async def process_channel_message(message: types.Message):
    # 获取频道的显示名称(即你要的"gdfgd")
    sender_channel_name = message.from_chat.title
    # 若频道设置了自定义用户名(@xxx),也可以用这个:
    # sender_channel_username = message.from_chat.username
    
    # 将名称存入变量,后续按需使用
    print(f"发送消息的频道:{sender_channel_name}")

# 注册处理器,只处理来自频道的消息
dp.message.register(process_channel_message, chat_type=types.ChatType.CHANNEL)

注意事项

  • 确保机器人已被添加到发送消息的频道,且拥有读取消息的权限
  • from_chat.title返回的是频道的显示名称,和你在聊天中看到的昵称一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:27:23