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.

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

  1. 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.
  2. 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 the channel 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": []
  }
  1. Add your Bot as admin to your channel:
    1. go the "Manage channel" > "Administrators"
    2. Add your Bot as Administrator by entering the Bot Name.
    3. Click in "Add"
  2. 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