[telegram-bot] How to obtain the chat_id of a private Telegram channel?

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?

This question is related to telegram-bot

The answer is


NEEDED ANSWER:

You should add & make your BOT as administrator of the PRIVATE channel, otherwise chat not found error happens.


The option that I do is by using the popular Plus Messenger on Android. The play store link is: https://play.google.com/store/apps/details?id=org.telegram.plus&hl=en

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

Supergroup and Channel Ids will looks like 1068773197 on plus messenger. For your usage on API, you can prefix -100 which would make it -1001068773197.


Found the solution for TelegramBotApi for python. Maybe will work for other languages.

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

@your_bot_name hi

In the console I get response with all info that I need.


update #2 :

Found another one easiest way : Just send to @username_to_id_bot bot your invite link to your private channel, it will return it's ID. Simplest level : maximum! :)

ps. I am not a an owner of this bot.

ps2. To be sure in security, just revoke your old invitation link if it is matter for you after bot using.

Original post :

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

revoke

Did anybody found solution for that case?

update

I found crazy solution :

  1. login 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 happen :)

Solution found here : https://github.com/GabrielRF/telegram-id#web-channel-id


No need to convert the channel to public then make it private.

  1. find the id of your private channel. (There are numerous methods to do this, for example see this SO 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 channel id is 1234 it would be chat_id=-1001234.

All done!


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

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

/my_id @get_id_bot

Inside your chat


I use 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 (eg: /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


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


You Can Too Do This:

Step 1)Convert Your Private Channel To 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 Channel To Private For a short time.


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

also works with public chats, channels and even users