如何向论坛主题发消息?ForumTopic无法转换为InputPeer报错
解决CS1503:ForumTopic无法转换为InputPeer的问题
错误原因很明确:SendMessageAsync方法要求第一个参数是InputPeer类型,但你直接传入了ForumTopic对象,类型不匹配导致编译错误。
要向论坛主题发送消息,你需要构造包含论坛主题ID的InputPeerChannel(因为目标是带论坛的群组/频道),具体操作如下:
- 从群组(
Channel对象)中获取id和access_hash - 从
ForumTopic对象中获取topic.id - 用这三个值创建
InputPeerChannel实例,作为SendMessageAsync的第一个参数
修改后的代码片段:
var myself = await client.LoginUserIfNeeded(); var chats = await client.Messages_GetAllChats(); foreach (var (id, chat) in chats.chats) switch (chat) { case Channel group when group.IsGroup && group.flags.HasFlag(Channel.Flags.forum): Console.WriteLine($"Group {group.username}: {group.title}"); var topics = await client.Channels_GetForumTopics(group, 0); foreach (ForumTopic topic in topics.topics) { Console.WriteLine($"Topic {topic.id}: {topic.title}"); // 构造带论坛主题ID的InputPeerChannel var targetPeer = new InputPeerChannel(group.id, group.access_hash, topic.id); await client.SendMessageAsync(targetPeer, myMessage); } break; }
关键说明:
InputPeerChannel的第三个参数就是论坛主题的ID,这样Telegram客户端就能定位到具体的论坛主题发送消息- 确保使用的库版本支持通过
InputPeerChannel指定论坛主题,主流Telegram开发库目前都支持该用法
内容的提问来源于stack exchange,提问作者super15
相关产品推荐
相关产品推荐

