如何用Pyrogram向指定Telegram Forum Topic发送消息?
问题描述
我需要向Forum Topic发送消息,现有以下代码:
from pyrogram import Client, filters from pyrogram.raw import functions,types app = Client("my_account", api_id="12345678", api_hash="g8u4h894urgh09eurhiugfdgfrgrg") async def get_chat_themes(client): result = await app.invoke( functions.messages.SendMessage(peer =types.InputPeerChannel(channel_id=12345,access_hash=-1234),message ="Test",random_id =-1) ) print(result) with app: app.loop.run_until_complete(get_chat_themes(app))
该代码会将消息发送至#General主题,请问如何将消息发送到其他主题?调用GetForumTopics接口得到如下输出:
{ "_": "types.messages.ForumTopics", "count": 1, "topics": [ { "_": "types.ForumTopic", "id": 12, "date": 1688764942, "title": "1", "icon_color": 16766590, "top_message": 12, "read_inbox_max_id": 13, "read_outbox_max_id": 0, "unread_count": 0, "unread_mentions_count": 0, "unread_reactions_count": 0, "from_id": { "_": "types.PeerUser", "user_id": }, "notify_settings": { "_": "types.PeerNotifySettings" }, "my": true, "closed": false, "pinned": false, "short": false, "hidden": false }, { "_": "types.ForumTopic", "id": 1, "date": 1688762115, "title": "General", "icon_color": 7322096, "top_message": 3, "read_inbox_max_id": 9, "read_outbox_max_id": 9, "unread_count": 0, "unread_mentions_count": 0, "unread_reactions_count": 0, "from_id": { "_": "types.PeerChannel", "channel_id": }, "notify_settings": { "_": "types.PeerNotifySettings" }, "my": true, "closed": false, "pinned": false, "short": false, "hidden": false } ], "messages": [ { "_": "types.MessageService", "id": 12, "peer_id": { "_": "types.PeerChannel", "channel_id": }, "date": 1688764942, "action": { "_": "types.MessageActionTopicCreate", "title": "1", "icon_color": 16766590 }, "out": true, "mentioned": false, "media_unread": false, "silent": false, "post": false, "legacy": false, "from_id": { "_": "types.PeerUser", "user_id": } }, { "_": "types.MessageService", "id": 3, "peer_id": { "_": "types.PeerChannel", "channel_id": }, "date": 1688762477, "action": { "_": "types.MessageActionChatAddUser", "users": [ ] }, "out": true, "mentioned": false, "media_unread": false, "silent": false, "post": false, "legacy": false, "from_id": { "_": "types.PeerUser", "user_id": } } ], "chats": [ { "_": "types.Channel", "id": , "title": "", "photo": { "_": "types.ChatPhotoEmpty" }, "date": 1688762115, "creator": true, "left": false, "broadcast": false, "verified": false, "megagroup": true, "restricted": false, "signatures": false, "min": false, "scam": false, "has_link": false, "has_geo": false, "slowmode_enabled": false, "call_active": false, "call_not_empty": false, "fake": false, "gigagroup": false, "noforwards": false, "join_to_send": false, "join_request": false, "forum": true, "access_hash": -, "restriction_reason": [], "admin_rights": { "_": "types.ChatAdminRights", "change_info": true, "post_messages": true, "edit_messages": true, "delete_messages": true, "ban_users": true, "invite_users": true, "pin_messages": true, "add_admins": true, "anonymous": false, "manage_call": true, "other": true, "manage_topics": true }, "default_banned_rights": { "_": "types.ChatBannedRights", "until_date": 2147483647, "view_messages": false, "send_messages": false, "send_media": false, "send_stickers": false, "send_gifs": false, "send_games": false, "send_inline": false, "embed_links": false, "send_polls": false, "change_info": false, "invite_users": false, "pin_messages": false, "manage_topics": false, "send_photos": false, "send_videos": false, "send_roundvideos": false, "send_audios": false, "send_voices": false, "send_docs": false, "send_plain": false }, "usernames": [] } ], "users": [ { "_": "types.User", "id": , "is_self": true, "contact": false, "mutual_contact": false, "deleted": false, "bot": false, "bot_chat_history": false, "bot_nochats": false, "verified": false, "restricted": false, "min": false, "bot_inline_geo": false, "support": false, "scam": false, "apply_min_photo": true, "fake": false, "bot_attach_menu": false, "premium": false, "attach_menu_enabled": false, "bot_can_edit": false, "access_hash": , "first_name": "", "username": "", "phone": "", "status": { "_": "types.UserStatusOffline", "was_online": }, "restriction_reason": [], "usernames": [] }, { "_": "types.User", "id": , "is_self": false, "contact": false, "mutual_contact": false, "deleted": false, "bot": true, "bot_chat_history": false, "bot_nochats": false, "verified": false, "restricted": false, "min": false, "bot_inline_geo": false, "support": false, "scam": false, "apply_min_photo": true, "fake": false, "bot_attach_menu": false, "premium": false, "attach_menu_enabled": false, "bot_can_edit": true, "access_hash": , "first_name": "Test", "username": "Testrobot123_robot", "bot_info_version": 1, "restriction_reason": [], "usernames": [] } ], "pts": 26, "order_by_create_date": false }
我查阅文档后仅找到functions.messages.SendMessage方法,请问是否有解决方案?也可考虑使用其他库。
解决方案
方法一:基于Pyrogram底层API修改
在functions.messages.SendMessage中添加reply_to_msg_id参数,指定目标主题的top_message值即可。从你提供的接口输出里,目标主题(title为"1")的top_message是12,修改代码如下:
from pyrogram import Client from pyrogram.raw import functions,types app = Client("my_account", api_id="12345678", api_hash="g8u4h894urgh09eurhiugfdgfrgrg") async def send_to_topic(client): result = await app.invoke( functions.messages.SendMessage( peer=types.InputPeerChannel(channel_id=12345, access_hash=-1234), message="Test", random_id=-1, reply_to_msg_id=12 # 填入目标主题的top_message值 ) ) print(result) with app: app.loop.run_until_complete(send_to_topic(app))
方法二:使用Pyrogram高层API(更简洁)
Pyrogram的高层API已封装论坛主题消息发送逻辑,直接调用app.send_message并指定forum_topic_id参数即可,无需手动处理底层参数:
from pyrogram import Client app = Client("my_account", api_id="12345678", api_hash="g8u4h894urgh09eurhiugfdgfrgrg") with app: # chat_id为频道完整ID(以-100开头),forum_topic_id为目标主题的id(比如12) app.send_message(chat_id=-10012345, text="Test", forum_topic_id=12)
频道完整ID可从GetForumTopics返回的chats数组中,将id值前加上-100得到。
其他库选项:python-telegram-bot(v20+)
如果考虑更换库,python-telegram-bot的新版本也支持论坛主题消息发送:
from telegram import Bot import asyncio async def main(): bot = Bot(token="YOUR_BOT_TOKEN") # chat_id为频道ID,message_thread_id为主题id await bot.send_message(chat_id=-10012345, text="Test", message_thread_id=12) asyncio.run(main())
内容的提问来源于stack exchange,提问作者Hack337
相关产品推荐
相关产品推荐

