How to use a Telegram Bot with a private channel (or how to post a message to a private channel)
TL;DR: just prepend -100 to the private channel ID and add your Telegram Bot as Admin to your channel.
UPDATE 2024-11-16
It seems there was a change in the Telegram API. If the instructions on this article don't work, try just prepending a -
instead of -100
. That did the trick for me. I haven't researched further, but the previous method (prepending -100
) still work in some services like activepieces (check that method)
Try both ways if the first one does not work.
You can also get the correct id using curl
# Replace YOUR_BOT_TOKEN with your actual bot token curl https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
and you will get something like this as a response:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 920 100 920 0 0 7931 0 --:--:-- --:--:-- --:--:-- 7931 { "ok": true, "result": [ { "update_id": 1111111, "my_chat_member": { "chat": { "id": -11111111, # <- this is your ID "title": "title", "type": "group", "all_members_are_administrators": true }, "from": { "id": 11111111, "is_bot": false, "first_name": "Diego", "last_name": "Carrasco Gubernatis", "username": "diegocarrasco", "language_code": "en" }, "date": 111111111, "old_chat_member": { "user": { "id": 11111111, "is_bot": true, "first_name": "first_name", "username": "username" }, "status": "left" }, "new_chat_member": { "user": { "id": 111111111, "is_bot": true, "first_name": "first_name", "username": "username" }, "status": "member" } } }, { "update_id": 111111111, "message": { "message_id": 3, "from": { "id": 1111111111, "is_bot": false, "first_name": "Diego", "last_name": "Carrasco", "username": "username", "language_code": "en" }, "chat": { "id": -111111111, "title": "title", "type": "group", "all_members_are_administrators": true }, "date": 1731779802, "group_chat_created": true } } ] }
you can also test if your chat id is correct by using the following command (replace -123456789
with your chat id.)
curl -X POST \ https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage \ -H 'Content-Type: application/json' \ -d '{"chat_id": "-123456789", "text": "Test message from curl"}'
END UPDATE
Context
I’ve been writing semi-regularly the last few years, and I’ve noted that there are some regular readers in my site. (They’ve told me themselves).
I also noticed I’ve been having troubles trying to follow other sites I read. I’m getting to many emails with updates and, in the end, there is to much noise.
In an effort to make it easier for others to get updates on what I write, I cross post to other sites such as Medium, Twitter/ X, Mastodon, LinkedIn and so on. And now to a Telegram Channel. (taking into account that almost everyone knows how to use a chat-app or messaging-app)
Of course, I don’t like to do manual work, so I automated that task using a Telegram Bot and ActivePieces here a referral link to get 500 task/month extra and remove daily limits. There are only 5 spots available. (similar to Zapier, n8n, make and others).
Each time there is a new item on my RSS Feed, ActivePieces will publish an update (share the article) to many channels, one of which is Telegram.
To do that you need to identify the channel ID, which is a little tricky if the channel is private (in Telegram there are public channels and private channels).
What you need to do
- Create a Telegram Bot using @BotFather (check here for tutorials). Save the Bot Token in a secure place (for example a keepass database using KeepassXC) and do not make it public, as it allows anyone with the token to manage your Bot.
- Get the private channel ID.
- I did this by going to the Telegram Desktop App, and exporting the channel history as JSON. There is an is which begins with channel and a number. Use that number and pretend -100. For example, for an id channel12345, the id to use would be -10012345.
- The JSON file looks like this. There is an
actor_id
key which starts with channel. Just replace thechannel
part with-100
{ "name": "Channel_Name, "type": "private_channel", "id": 12345, "messages": [ { "id": 1, "type": "service", "date": "2023-08-17T12:03:51", "date_unixtime": "1692266631", "actor": "DiegoCarrasco’s Blog", "actor_id": "channel12345", "action": "create_channel", "title": "Here comes a title", "text": "", "text_entities": [] }
- Add your Bot as admin to your channel:
- go the "Manage channel" > "Administrators"
- Add your Bot as Administrator by entering the Bot Name.
- Click in "Add"
- Use the id to make request to the bot. You may use any tool, build a script or use curl directly. I used ActivePieces and I’m happy with it.
That's it! 😁 Enjoy your new automation! 🤖
References
- ActivePieces support
- this answer https://stackoverflow.com/a/56546442
- Trial and error
Comments
Comments powered by Disqus