在VB.NET中用WTelegramClient获取Telegram频道论坛话题
解决WTelegramClient获取频道论坛话题的VB.NET实现问题
关键问题解决
1. 获取access_hash并创建InputChannel
ChatBase是抽象基类,支持论坛功能的频道/超级群实际对应的是TL.Channel类型(普通群TL.Chat不支持论坛)。你需要先将ChatBase转换为TL.Channel实例,直接读取其access_hash属性即可:
' 先判断并转换为Channel类型 If TypeOf channel Is TL.Channel Then Dim channelObj As TL.Channel = DirectCast(channel, TL.Channel) access_hash = channelObj.access_hash Else Throw New InvalidOperationException("该聊天对象不支持论坛功能") End If
2. ForumTopicBase与ForumTopic的选择
ForumTopicBase是所有论坛话题的基类,实际调用Channels_GetForumTopics返回的topics集合中,有效话题都是ForumTopic实例(ForumTopicEmpty仅作为占位符使用)。后续要枚举话题内消息,需要ForumTopic的id等具体属性,因此直接转换为ForumTopic是合理的。
修改后的完整辅助函数
Public Shared Async Function GetChannelTopicsAsync(client As WTelegram.Client, channel As TL.ChatBase) _ As Task(Of ReadOnlyCollection(Of TL.ForumTopic)) If Not TypeOf channel Is TL.Channel Then Throw New InvalidOperationException("只有频道/超级群支持论坛话题功能") End If Dim channelObj As TL.Channel = DirectCast(channel, TL.Channel) Dim inputChannel As New InputChannel(channelObj.ID, channelObj.access_hash) Dim msgs_ForumTopics As Messages_ForumTopics = Await client.Channels_GetForumTopics(inputChannel) ' 直接转换为ForumTopic集合,过滤可能的空实例 Dim topics As List(Of TL.ForumTopic) = msgs_ForumTopics.topics. OfType(Of TL.ForumTopic)().ToList() ' 按话题ID排序(可根据需求自定义排序逻辑) topics.Sort(Function(t1, t2) t1.id.CompareTo(t2.id)) Return topics.AsReadOnly() End Function
测试代码修正
原测试代码中GetGroupTopicsAsync调用需改为GetChannelTopicsAsync,并传入client参数:
Dim sourceGroupTopics As ReadOnlyCollection(Of TL.ForumTopic) = Await WTelegramHelper.GetChannelTopicsAsync(client, sourceGroup)
补充说明
- 若需确保获取的聊天对象支持论坛,可在
GetChannelsByTitleAsync中增加判断:检查channel是否为TL.Channel且forum属性为True - 枚举话题消息时,可使用
Messages_GetHistory接口,传入包含话题ID的InputPeerChannel(同样基于Channel的ID和access_hash创建)
内容的提问来源于stack exchange,提问作者ElektroStudios
相关产品推荐
相关产品推荐

