177

I use curl to update my public channels. This kind of syntax:

curl -X POST "https://api.telegram.org/bot144377327:AAGqdElkZ-77zsPRoAXXXXXXXXXX/sendMessage" -d "chat_id=@MyChannel&text=my sample text"

But what's the chat_id of a private channel? It's not the one you have in the private invite.

Because now we can pass a channel username (in the format @channelusername) in the place of chat_id in all methods (and instead of from_chat_id in forwardMessage). But what's the @channelusername of a private channel that I administer?

3
  • Why do you need channel's chat_id? All sending methods to channel use string "@channel_name" Commented Nov 22, 2015 at 20:24
  • 1
    Hi @pengrad with curl command I ask a bot of mine to send a message to MyChannel. For a public channel the chat_id is equal to the last part of its URL: in telegram.me/MyChannel, the chat_id is "MyChannel". The private channels have only invite url like telegram.me/joinchat/AJhkwTviqL_7HWcGZgottQ and the chat_id is not "AJhkwTviqL_7HWcGZgottQ". For this reason I'm looking for the chat_id of a private channel.
    – aborruso
    Commented Nov 22, 2015 at 21:27
  • Is there a way to run libre taxi without telegram? Commented Feb 14, 2020 at 14:47

17 Answers 17

292

I found the way to write in private channels.

  1. You should convert it to public with some @channelName

  2. Send a message to this channel through the Bot API:

    https://api.telegram.org/bot111:222/sendMessage?chat_id=@channelName&text=123

  3. As the response, you will get information with chat_id of your channel.

    {
      "ok" : true,
      "result" : {
        "chat" : {
          **"id" : -1001005582487,**
          "title" : "Test Private Channel",
          "type" : "channel"
        },
        "date" : 1448245538,
        "message_id" : 7,
        "text" : "123ds"
      }
    }
    
  4. Now you can convert the channel back to private (by deleting the channel's link) and send a message directly to the chat_id "-1001005582487":

    https://api.telegram.org/bot111:222/sendMessage?chat_id=-1001005582487&text=123

6
  • 1
    I voted your question and I thank you for details! I'm perplex voting your answer here because is more an hacking :-). BTW, my general question is: What the need of a Private Channel? Maybe groups (recent supergroups) are a better way? In facs telegram documentation is unclear about Private channels. Commented Dec 1, 2015 at 9:10
  • 1
    I tried this and it worked only for some minutes. I think because once the channel becomes private there is a time frame where it is still publicly reachable: after, if you try to set it again as public you have to redefine the channel id.
    – roibeart
    Commented Jul 17, 2016 at 23:33
  • 3
    You should clarify that we need to submit our api token next to bot on first slash. Anyways thanks! It was a neat solution. Commented Mar 16, 2020 at 19:57
  • 1
    Also first thing you need to do is create a /newbot in BotFather and then you will get ApiKey and then add the bot to your channel as administrator. Thanks! Commented Feb 9, 2021 at 4:32
  • 1
    there's no nedd to convert your channel to private, you can still retrieve the chat id even its private, just by sending a message to it by yourself, not via the bot
    – mark
    Commented Sep 4, 2021 at 12:35
186

The easiest way:

Just send your invite link to your private channel to @username_to_id_bot (https://t.me/username_to_id_bot) bot. It will return its ID. The simplest level: maximum! :)

PS. I am not an owner of this bot.

PS 2. the Bot will not join your group, but to be sure in security. Just revoke your old invitation link if it is matter for you after bot using.

A more cumbersome way:

Making the channel public cannot be done by the user with exist at least five public groups/channels, so...the problem is not solved. Yes, you can revoke one of them, but for now, we cannot retrieve chat id another way.

Revoke

Another one working solution:

  1. log in under your account at web version of Telegram: https://web.telegram.org
  2. updated (thanks @Julian Espinel) Find your channel. See to your URL. It should be like https://web.telegram.org/z/#-1543515057
  3. Grab "1543515057" from it, and add "-100" as a prefix.

So... your channel id will be "-1001543515057". Magic happens :)

The solution was found at Web Channel ID .

7
  • 1
    Even my new channel switched private/public/private to set @channel_username, but while it's private, @channel_username is not working for me! I've used your posted solution, and worked
    – MohaMad
    Commented Sep 18, 2017 at 20:18
  • 1
    I don't think this -100 prefix will work forever, but after 4 year it still works!
    – Ali Tou
    Commented May 6, 2020 at 21:34
  • 1
    @AliTou Pavel Durov just found this comment and decided to not remove FEATURE, ahah :) Glad to hear, that it still works :)
    – Nigrimmist
    Commented May 6, 2020 at 21:46
  • @Nigrimmist your bot is not working anymore for some time now. I used to use it a lot, do you have a github repo of it?
    – LeoPucciBr
    Commented Mar 8, 2022 at 14:47
  • 2
    It sems like telegram changed the URL structure. Take this one for example: web.telegram.org/k/#-1779618123. The channel ID is -1001779618123. (Append 100 between - and the given number 1779618123) Commented Sep 17, 2022 at 22:28
88

Open the private channel, then:


WARNING be sure to add -100 prefix when using Telegram Bot API:

  • if the channel ID is for example 1192292378
  • then you should use -1001192292378
4
  • 1
    I don't think this method works now because the invitation link does not have this format anymore Commented Dec 8, 2020 at 8:17
  • 2
    This method works! It's not the invitation link, it's the URL of the channel. When you open the web client, and navigate to your private channel, the URL in browser still have this format and so the private channel id can be derived from it. Awesome! Commented Apr 9, 2021 at 12:35
  • 3
    Wow! Works like charm. You saved me so much time. Thanks. Commented Apr 26, 2021 at 21:45
  • adding prefix -100 worked because I was using a bot token
    – HackRx
    Commented Feb 6, 2023 at 12:00
45

The easiest way is to invite @get_id_bot in your chat and then type:

/my_id @get_id_bot

Inside your chat

5
  • 2
    According to my test, bot should be added as administrator in channel.
    – Morteza
    Commented Jul 12, 2017 at 10:05
  • Yes, has to be added as administrator it seems Commented Jul 23, 2018 at 12:27
  • 5
    is this still current? /my_id@... doesnt seem to do anything (apart from have that message appear)
    – wal
    Commented Jun 7, 2019 at 13:24
  • 1
    @wal I got the id by 2 methods /start @get_id_bot and also /my_id @get_id_bot space seems important. Add @get_id_bot as member and give only post right as Administrator. Remove it for safety once you get ID.
    – rahoolm
    Commented May 11, 2020 at 20:38
  • just forward a message from your channel to the bot Commented Feb 18, 2021 at 6:54
23

Actually, it is pretty simple.

All you need to do is to:

  1. Invite the bot to the channel/group (doesn't matter private or not).
  2. Send any message in the channel/group.
  3. Go to getUpdates API URL in the browser:
    https://api.telegram.org/bot<BOT_TOKEN>/getUpdates
    

Now, look at the results.
You will see a JSON output. In this JSON, look for the chat data. Usually, latest updates are at the end.

{
   "ok":true,
   "result":[
      ... 
      {
         ... 
         "my_chat_member":{
            "chat":{
               "id":-100987654321,
               "title":"My Channel",
               "type":"channel"
            },
            ...
         }
      }
   ]
}

Your chat ID is -100987654321.

4
  • 4
    This is the safest option, without adding any unknown bot to your group.
    – Habeeb
    Commented Mar 10, 2022 at 5:53
  • 1
    Agreed, to me this looks the simplest and safest option, thank you.
    – A.Infante
    Commented Dec 10, 2022 at 8:18
  • I don't think step 2 is needed. Worked for me without sending any messages
    – diaa
    Commented Dec 10, 2023 at 9:52
  • @diaa That's because you've probably already sent a message while the bot was there before. Without this step, you will not be able to get any data in the API's result. Commented Apr 20 at 21:09
9

The id of your private channel is the XXXXXX part (between the "p=c" and the underscore).

To use it, just add "-100" in front of it. So if "XXXXXX" is "4785444554" your private channel id is "-1004785444554".

7

For now you can write an invite link to bot @username_to_id_bot and you will get the id:

Example:

Enter image description here

It also works with public chats, channels and even users.

1
  • i do not know how to do that
    – toha
    Commented Oct 22, 2022 at 3:29
4

You can also do this:

Step 1. Convert your private channel to a public channel

Step 2. Set the ChannelName for this channel

Step 3. Then you can change this channel to private

Step 4. Now sending your message using @ChannelName that you set in step 3

Note: For Step 1, you can change one of your public channels to private for a short time.

2
  • correct, @Channel Name in private is no name sometimes
    – toha
    Commented Oct 22, 2022 at 5:51
  • This does not work anymore : name is removed
    – Shatiz
    Commented Apr 4, 2023 at 11:48
4

There isn't any need to convert the channel to public and then make it private.

  1. find the id of your private channel. (There are numerous methods to do this, for example see this Stack Overflow answer.)

  2. curl -X POST "https://api.telegram.org/botxxxxxx:yyyyyyyyyyy/sendMessage" -d "chat_id=-100CHAT_ID&text=my sample text"

Replace xxxxxx:yyyyyyyyyyy with your bot id, and replace CHAT_ID with the channel id found in step 1. So if the channel id is 1234, it would be chat_id=-1001234.

All done!

2

Run this command in your terminal.

curl https://api.telegram.org/bot{your_bot_token}/getUpdates

But You have added your bot to your private channel.You will get your private channel chat id in json response.

2

Simply and easy; just pass the channel username and it will show you the id:

from telethon.sync import TelegramClient, events
client = TelegramClient("bot", API_ID, API_HASH)


channel_name = "channel username"
channel = client.get_entity(channel_name)
print(f'👉 Channel ID: {channel.id}')

client.start()
client.run_until_disconnected()
0

The option that I do is by using the popular Plus Messenger on Android.

You can click on the channel and in Channel info below the group name, you can find the channel Id.

Supergroup and channel ids will look like 1068773197 on Plus Messenger. For your usage on API, you can prefix -100 which would make it -1001068773197.

0

I found the solution for TelegramBotApi for Python. Maybe it will work for other languages.

I just add my bot to the private channel and then do this:

@your_bot_name hi

In the console I get a response with all the information that I need.

0

I used Telegram.Bot and got the ID the following way:

  1. Add the bot to the channel
  2. Run the bot
  3. Write something into the channel (for example, /authenticate or foo)

Telegram.Bot:

private static async Task Main()
{
    var botClient = new TelegramBotClient("key");
    botClient.OnUpdate += BotClientOnOnUpdate;
    Console.ReadKey();
}

private static async void BotClientOnOnUpdate(object? sender, UpdateEventArgs e)
{
    var id = e.Update.ChannelPost.Chat.Id;
    await botClient.SendTextMessageAsync(new ChatId(id), $"Hello World! Channel ID is {id}");
}

Plain API:

This translates to the getUpdates method in the plain API, which has an array of update which then contains channel_post.chat.id.

0

Yet another way to use JavaScript and the Axios library. So you might want to explore /getUpdates method of Telegram API:

const headers: any = {
  'Access-Control-Allow-Origin': '*',
  'Content-Type': 'application/json',
  timestamp: +new Date(),
}

const options = { headers: { ...headers } }

const urlTelegramBase =
  'https://api.telegram.org/bot123456:ABCDEF'

const urlGetUpdates = `${urlTelegramBase}/getUpdates`
const username = 'user_name'

const {
  data: { result: messages },
} = await axios.get(urlGetUpdates, options)

const chat_id = messages.find(
  messageBlock => messageBlock.message.chat.username === username
).message.chat.id

console.info('chat_id': chat_id)
0

At the moment of writing this comment August 2023, I could not use the methods recommended here, including the "-100" thingy. But using telethon I found a quite simple solution to write to private channels by just parsing the channel id to int using in-built int() method Example:

channel_id = "-100123456789" 
# channel_id = "123456789" # works too, no need for -100

send_message(int(channel_id), "message")
-2

You should add and make your bot an administrator of the private channel. Otherwise, the chat not found error happens.

Not the answer you're looking for? Browse other questions tagged or ask your own question.