Another one working solution:
So... your channel id will be "-1001543515057". Magic happens :)
The solution was found at Web Channel ID .
Open the private channel, then:
on web client:
if
it's for example https://web.telegram.org/#/im?p=c1192292378_2674311763110923980then
1192292378 is the channel IDon mobile and desktop:
if
it's for example https://t.me/c/1192292378/31then
1192292378 is the channel ID (bonus: 31 is the message ID)on Plus Messenger for Android:
WARNING
be sure to add -100 prefix when using Telegram Bot API:
if
the channel ID is for example 1192292378then
you should use -1001192292378The easiest way is to invite @get_id_bot in your chat and then type:
/my_id @get_id_bot
Inside your chat
Actually, it is pretty simple.
All you need to do is 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
.
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".
For now you can write an invite link to bot @username_to_id_bot and you will get the id:
Example:
It also works with public chats, channels and even users.
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.
There isn't any need to convert the channel to public and then make it private.
find the id of your private channel. (There are numerous methods to do this, for example see this Stack Overflow answer.)
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!
Simply and easy; just pass the channel username and it will show you the id:
from telethon.sync import TelegramClient, eventsclient = 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()
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.
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.
I used Telegram.Bot and got the ID the following way:
/authenticate
or foo
)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}");}
This translates to the getUpdates method in the plain API, which has an array of update which then contains channel_post.chat.id.
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.idconsole.info('chat_id': chat_id)
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.
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()
methodExample:
channel_id = "-100123456789" # channel_id = "123456789" # works too, no need for -100send_message(int(channel_id), "message")
You should add and make your bot an administrator of the private channel. Otherwise, the chat not found error happens.