How to obtain the chat_id of a private Telegram channel

Telegram Bot

Telegram Bot Problem Overview


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?

Telegram Bot Solutions


Solution 1 - Telegram Bot

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

Solution 2 - Telegram Bot

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

A crazy solution:

  1. log in under your account at web version of Telegram: https://web.telegram.org
  2. Find your channel. See to your URL. It should be like https://web.telegram.org/#/im?p=c**1055587116**_11052224402541910257
  3. Grab "1055587116" from it, and add "-100" as a prefix.

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

The solution was found at Web Channel ID .

Solution 3 - Telegram Bot

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

Solution 4 - Telegram Bot

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

/my_id @get_id_bot

Inside your chat

Solution 5 - Telegram Bot

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".

Solution 6 - Telegram Bot

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.

Solution 7 - Telegram Bot

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.

Solution 8 - Telegram Bot

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.

Solution 9 - Telegram Bot

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!

Solution 10 - Telegram Bot

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.

Solution 11 - Telegram Bot

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.

Solution 12 - Telegram Bot

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.

Solution 13 - Telegram Bot

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)

Solution 14 - Telegram Bot

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaborrusoView Question on Stackoverflow
Solution 1 - Telegram BotStas ParshinView Answer on Stackoverflow
Solution 2 - Telegram BotNigrimmistView Answer on Stackoverflow
Solution 3 - Telegram BotFabio MicheliniView Answer on Stackoverflow
Solution 4 - Telegram BotJurgo BoemoView Answer on Stackoverflow
Solution 5 - Telegram BotPeterView Answer on Stackoverflow
Solution 6 - Telegram BotSlavik MeltserView Answer on Stackoverflow
Solution 7 - Telegram BotcrystalbitView Answer on Stackoverflow
Solution 8 - Telegram BotMohammad AbdolahzadehView Answer on Stackoverflow
Solution 9 - Telegram BotapadanaView Answer on Stackoverflow
Solution 10 - Telegram BotHabeebView Answer on Stackoverflow
Solution 11 - Telegram Botlisichka gggView Answer on Stackoverflow
Solution 12 - Telegram BotChristian GollhardtView Answer on Stackoverflow
Solution 13 - Telegram BotRomanView Answer on Stackoverflow
Solution 14 - Telegram BotT.ToduaView Answer on Stackoverflow