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

如何使用WTelegramClient添加机器人到频道?遇access_hash等问题

解决方案

1. 两次尝试失败的原因

  • 第一次调用Users_GetFullUser时传入access_hash=0,Telegram API无法识别有效用户,因此返回USER_ID_INVALID,必须获取正确的access_hash才能查询用户信息。
  • 第二次虽然成功获取到机器人的InputUser对象,但Channels_InviteToChannel接口仅支持邀请普通用户,邀请机器人需要使用专门的添加管理员接口,所以返回USER_BOT错误。

2. 正确获取机器人的access_hash

你可以通过以下两种可靠方式获取机器人的access_hash:

方式一:通过机器人ID查询

' 替换为机器人的数字ID
Dim botId As Long = 123456789
Dim inputUser As New InputUser(botId, 0)
Dim userList = Await Client.Users_GetUsers(New List(Of InputUser) From {inputUser})
Dim botUser = userList(0)
' 此时botUser.AccessHash即为正确的access_hash值

方式二:通过机器人用户名解析

' 替换为机器人的用户名(不带@前缀)
Dim botUsername As String = "your_bot_username"
Dim resolved = Await Client.Contacts_ResolveUsername(botUsername)
Dim botUser = resolved.User
' botUser.AccessHash即为所需的access_hash值

3. 邀请机器人加入频道的正确方法

Telegram限制了普通邀请接口对机器人的支持,必须通过Channels_EditAdmin接口将机器人添加为频道管理员(即使设置最低权限也可完成加入操作):

' 解析频道ID和access_hash
Dim chParts = "{channel_id}:{channel_hash}".Split(":")
Dim inputChannel = New InputChannel(Long.Parse(chParts(0)), Long.Parse(chParts(1)))

' 获取机器人的InputUser对象(包含正确的access_hash)
Dim resolved = Await Client.Contacts_ResolveUsername("your_bot_username")
Dim botInputUser = New InputUser(resolved.User.Id, resolved.User.AccessHash)

' 定义管理员权限(可按需调整,这里设置最低权限)
Dim adminRights = New ChatAdminRights(
    ChangeInfo = False,
    PostMessages = False,
    EditMessages = False,
    DeleteMessages = False,
    BanUsers = False,
    InviteUsers = False,
    PinMessages = False,
    ManageCall = False
)

' 添加机器人到频道
Await Client.Channels_EditAdmin(inputChannel, botInputUser, adminRights, "机器人")

执行上述代码后,机器人会自动加入目标频道,无需提前与机器人交互。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 16:32:09