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

如何使用Telethon的send_message方法向指定话题发送消息?

向Telegram指定话题发送消息(Telethon实现)

问题背景

我正在用Telethon编写Telegram消息处理器,已经实现了用ForwardMessagesRequest转发消息到指定话题:

await client(
    ForwardMessagesRequest(
        from_peer=event.chat_id,
        id=[event.message.id],
        to_peer=config.target_group,
        silent=True,
        top_msg_id=config.TOPIC_DICT["fruit"],  # 指定话题
    )
)

但send_message方法和SendMessageRequest类没有类似top_msg_id的参数,查看SendMessageRequest定义如下:

class SendMessageRequest(TLRequest):
    CONSTRUCTOR_ID = 0x983f9745
    SUBCLASS_OF_ID = 0x8af52aac

    def __init__(self, peer: 'TypeInputPeer', message: str, no_webpage: Optional[bool]=None, silent: Optional[bool]=None, background: Optional[bool]=None, clear_draft: Optional[bool]=None, noforwards: Optional[bool]=None, update_stickersets_order: Optional[bool]=None, invert_media: Optional[bool]=None, reply_to: Optional['TypeInputReplyTo']=None, random_id: int=None, reply_markup: Optional['TypeReplyMarkup']=None, entities: Optional[List['TypeMessageEntity']]=None, schedule_date: Optional[datetime]=None, send_as: Optional['TypeInputPeer']=None, quick_reply_shortcut: Optional['TypeInputQuickReplyShortcut']=None, effect: Optional[int]=None):
        """
        :returns Updates: Instance of either UpdatesTooLong, UpdateShortMessage, UpdateShortChatMessage, UpdateShort, UpdatesCombined, Updates, UpdateShortSentMessage.
        """
        self.peer = peer
        self.message = message
        self.no_webpage = no_webpage
        self.silent = silent
        self.background = background
        self.clear_draft = clear_draft
        self.noforwards = noforwards
        self.update_stickersets_order = update_stickersets_order
        self.invert_media = invert_media
        self.reply_to = reply_to
        self.random_id = random_id if random_id is not None else int.from_bytes(os.urandom(8), 'big', signed=True)
        self.reply_markup = reply_markup
        self.entities = entities
        self.schedule_date = schedule_date
        self.send_as = send_as
        self.quick_reply_shortcut = quick_reply_shortcut
        self.effect = effect

希望能通过类似thread_id的参数指定话题,但看不懂TDLib的C++示例,求实现方法。

解决方案

Telegram的话题(线程)本质是通过关联话题的起始消息来实现的,因此可以利用SendMessageRequest或send_message的reply_to参数,传入话题的起始消息ID(即你之前用的top_msg_id)来指定发送到对应话题。

步骤1:导入所需类型

首先导入InputReplyToMessage类型:

from telethon.tl.types import InputReplyToMessage

步骤2:使用send_message方法发送到指定话题

直接调用send_message时,通过reply_to参数传入话题的起始消息ID:

await client.send_message(
    entity=config.target_group,
    message="这里是要发送到话题的内容",
    reply_to=InputReplyToMessage(top_msg_id=config.TOPIC_DICT["fruit"])
)

步骤3:使用SendMessageRequest发送到指定话题

如果直接用SendMessageRequest,同样配置reply_to参数:

await client(
    SendMessageRequest(
        peer=config.target_group,
        message="这里是要发送到话题的内容",
        reply_to=InputReplyToMessage(top_msg_id=config.TOPIC_DICT["fruit"])
        # 可按需添加其他参数,比如silent=True等
    )
)

说明

  • InputReplyToMessage的top_msg_id字段就是用来指定消息所属话题的起始消息ID,和转发时用的top_msg_id是同一个值。
  • 这种方式相当于“回复”话题的第一条消息,从而让新消息被归类到该话题下,和手动在话题里发消息的逻辑一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 10:05:13