【问题标题】:Obtaining chat_id having chat link in Telegram API在 Telegram API 中获取具有聊天链接的 chat_id
【发布时间】:2017-08-22 08:56:36
【问题描述】:

我正在使用 Telegram API 开发一个程序,通过链接加入 Telegram 群组或频道。

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

如何获取有链接的群组或频道的chat_idchannel_id

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

【问题讨论】:

    标签: api telegram


    【解决方案1】:

    我找到了答案:

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

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

    【讨论】:

      【解决方案2】:

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

      如果您想解析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']
      

      【讨论】:

        猜你喜欢
        • 2018-01-28
        • 2020-04-14
        • 2021-04-03
        • 1970-01-01
        • 2015-09-13
        • 2021-04-07
        • 2017-01-21
        • 2017-01-20
        • 2019-10-01
        相关资源
        最近更新 更多