获取 chat_id 在 Telegram API 中聊天 link

Obtaining chat_id having chat link in Telegram API

我正在使用 Telegram API 开发一个程序,通过他们的 link 加入 Telegram 组或频道。

加入群组或频道的方法(例如channels.joinChannel)需要chat_idchannel_id,但我只有link的群组或频道(例如@channel_username 或 https://t.me/channel_username or https://t.me/joinChat/xxxxx)

我怎样才能获得 chat_idchannel_id 的群组或频道 link?

P.S: 我不是这些群组或频道的管理员。

我找到了答案:

首先我们必须使用checkChatInvite方法。它使用聊天 link 作为输入参数并输出聊天规范包括 chat_id.

然后我们使用joinChat方法方法。它使用从上一步获得的 chat_id 并加入该组或频道。

所选答案似乎已过时。在最近的版本中有 checkChatInviteLink 调用,但它需要聊天 url 以 https://t.me/joinchat/

开头

如果您想解决 link 之类的 https://t.me/chatname,您可以使用 searchPublicChat API 调用。

这对我有用(使用 https://github.com/alexander-akhmetov/python-telegram):

def resolve_channel_link(tg, link):
    if link.startswith('https://t.me/'):
        link = link.split('/')[-1]
    else:
        raise RuntimeError('cant parse link', link)

    r = tg.call_method('searchPublicChat', [{'username', link}])
    r.wait()

    if r.error:
        raise RuntimeError(r.error_info)
    assert(r.update['@type'] == 'chat')
    return r.update['id']