How to get a Telegram Channel Id without sending a message to it
Asked Answered
S

6

16

Is there any way to find a telegram channel id without sending a message to it ?

Right now I go this way and find the channel id by calling this URL in my code and get the JSON as result: https://api.telegram.org/bot???????/sendMessage?chat_id=@?????&text=123

However, this cause sending the message "123" to the channel which is not good.

Spinose answered 17/1, 2017 at 6:55 Comment(1)
Welcome to SO. Please read What topics can I ask about and How to ask a good question And the perfect question And how to create a Minimal, Complete and Verifiable example SO is not a free Coding or Code Conversion or Debugging or Tutorial or Library Finding service Here at SO we fix your attempts, we do not code things for youStemson
D
45

Check this link for help.
It is using web telegram url param info.

Adding details from the referred link:

  1. log in web telegram
  2. Click on the target channel then you will find the url displayed on your browser.

If it's a public channel, the ID is @name of the channel.

If it's a private channel then the url must be similar to:
https://web.telegram.org/#/im?p=c1018013852_555990343349619165
For this case, the channel ID would be 1018013852.
It's important to know that channel's IDs are always negative and 13 characters long!
So add -100 to it, making the correct ID -1001018013852.

Danita answered 22/6, 2018 at 8:48 Comment(0)
P
16

You Can use Web Telegram to see each channel id in link of that page in your explorer
or

Just Simply Forward a message from your channel to This Bot: (https://telegram.me/getidsbot)

Pharmacopsychosis answered 8/8, 2017 at 20:45 Comment(2)
Good Answer, But unfortunately the Bot not answering. I am using (telegram.me/telebot_robot)[@telebot_robot] that works perfectHalsy
GetIDs Bot works fine at the moment. If not, one can deploy a new copy of the bot: github.com/wjclub/telegram-bot-getids . As a backup, here is my bot on AWS Lambda (which means that it's free to maintain): t.me/itpp_myid_botSkipbomb
E
5

Yes, just forward a message of the channel to your bot and get the message.forward_from_chat.id.

Exeter answered 5/2, 2017 at 16:36 Comment(0)
S
4
  1. Get your channel link

  2. install python library telethon

  3. get your api_id and api_hash here.

  4. write the following code and run:

     from telethon import TelegramClient
     api_id=
     api_hash=
     channel_link = 'your_channel_link'
     client = TelegramClient(session_name, api_id, api_hash,
                             update_workers=4, spawn_read_thread=False)
     client.start()
     entity = client.get_input_entity(**channel_link**)
     print(entity.channel_id)
    

By the way, if you use it for telegram bot, just add -100 before the printed id, this should be work!

Subvert answered 20/4, 2018 at 7:0 Comment(1)
I get AttributeError: 'coroutine' object has no attribute 'channel_id'Anticosti
D
1

Put your bot into your channel and go to https://api.telegram.org/bot/getUpdates.

If someone send message to that channel, you will get the channel ID there.

Defective answered 30/5, 2022 at 5:19 Comment(0)
A
0

Fixing @scruel's answer:

In [1]: api_id = ...                                                                                                                                  

In [2]: api_hash = '...'                                                                                                     

In [3]: channelLink = 'https://t.me/BTCST_Community_EN'                                                                                                   

In [4]: from telethon import TelegramClient, events                                                                                                       

In [5]: client = TelegramClient('test', api_id, api_hash)                                                                                                 

In [6]: client.start()                                                                                                                                    
Out[6]: <telethon.client.telegramclient.TelegramClient at 0x7fc90c352290>

In [7]: entity = await client.get_entity(channelLink)                                                                                                     

In [8]: channelId = '-100' + str(entity.id)                                                                                                               

In [9]: channelId                                                                                                                                        
Out[9]: '-1001236496320'

Here's a CLI util to do it:

#!env/bin/python

import sys
import asyncio

from telethon import TelegramClient

import config

async def channel_id_from_link(client, channel_link):
    return "-100" + str((await client.get_entity(channel_link)).id)

async def main(channel_link):
    async with TelegramClient(
        "test", config.api_id, config.api_hash
    ) as client:
        channel_id = await channel_id_from_link(client, channel_link)

    return channel_id

if __name__ == "__main__":
    channel_link = sys.argv[1]
    channel_id = asyncio.run(main(channel_link))
    print(channel_id)

Test:

> ./TelegramChannelId.py https://t.me/binance_api_english
-1001134190352
Anticosti answered 5/3, 2021 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.