Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining chat_id having chat link in Telegram API

Tags:

api

telegram

I am using Telegram API to develop a program to join Telegram groups or channel by their links.

Methods that join group or channel (e.g. channels.joinChannel) need chat_id or channel_id, but I have only the links of the groups or channels (e.g. @channel_username or https://t.me/channel_username or https://t.me/joinChat/xxxxx)

How can I obtain chat_id or channel_id of a group or channel having its link?

P.S: I'm not the admin of these groups or channels.

like image 854
Alireza Zojaji Avatar asked Mar 29 '17 03:03

Alireza Zojaji


2 Answers

I found the answer:

First we must use checkChatInvite method. It uses the chat link as input parameter and outputs the chat specifications includes chat_id.

Then we use joinChat method method. it uses the chat_id got from the previous step and joins to that group or channel.

like image 61
Alireza Zojaji Avatar answered Oct 16 '22 10:10

Alireza Zojaji


Selected answer seems to be outdated. In recent versions there is checkChatInviteLink call, but it requires the chat url to start with https://t.me/joinchat/

If you want to resolve a link like https://t.me/chatname, you can use searchPublicChat API call.

This works for me (using 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']
like image 1
Dmitry Vyal Avatar answered Oct 16 '22 10:10

Dmitry Vyal