As we ran a business for electric skateboard/mountainboard parts, we participate in the community. Still, we cannot always track all the posts and topics where people mention our brand or products as they don't tag as directly but use our name to mention, so I decided to make a Telegram bot that would post URL to discourse post where specific keywords mentioned. That way, we can keep track of what's happening in the community.

Goals of this project:

  • Fetch a list of the latest post until previous first received post id
  • Check each post if it does contain our defined list of keywords
  • Create and format a message for Telegram channel
  • Send all collected posts since last post id
  • Make this bot run scheduled every ten minutes
  • Find a place to host it for free

Introduction

For this project, I decided to use a plain PHP script. I know there are many Telegram bot frameworks and libraries for creating Telegram bots, but I wanted something simple and easy to maintain for this problem.

Step 1: Create a Telegram new bot via @BotFather

A first step would be to register a telegram bot using @BotFather bot on Telegram and obtain a bot token.

I followed this guide for registering the new bot:

Bots: An introduction for developers
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands…

Step 2: Figure out a way to send a message to the Telegram Channel

Now that we have a bot token available, we can try to play with it. I choose to use the Telegram Channel instead of group chat because I wanted that place to be only for messages from the bot and have all discussion happening in another group (Telegram provides an excellent way to register "comment" group for a Channel)

Following a Telegram Bot API docs, I figured out that you can interact with Telegram Bot API via simple HTTP GET calls.

All that we need is sendMessage endpoint to send a message to the Channel:

Telegram Bot API
The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram.To learn how to create…

Everything looks nice, but how I am going to get the chatId? It took a bit of brain wracking, but then I found out about the getUpdates endpoint, added bot the channel, sent a message myself, and used a simple GET call to getUpdates I managed to find a required chatId.

carbon--1-

Step 3: Fetching Discourse latest posts

Now that we have a way to send messages into the channel now, we need some content.

A quick visit to Discourse API docs and found an endpoint /posts.json but the problem is it only returns the latest 11 posts, and as far I can see, nothing about pagination is mentioned. Diving into discourse source code, I managed to find an accepted parameter called before.

It looks like I can do some simple pagination by providing the last post id to it, and it will return post after it.

carbon--2-

Step 4: Wrap everything with some magic and keyword matching

Now that all the essential bits are figured out now, it's only left to connect it all.

Step 5: Find a place to host it for free and have it run on scheduled time.

Now that we have a fully working bot, we need a place to host it. At first, I was considering using Heroku, but it's way too complicated. When I looked around, I remembered that Travis had scheduled cron style runs. I was going to look into it when I remembered Github Actions.

After looking that Github Actions supported all required features:

  • Run job on scheduled time (every 10mins)
  • Possible to have encrypted/hidden secrets
  • Possible to run PHP script
  • Possible to save a file between runs (saves last post id)

It was a green light, batching up a workflow file, I was able to get everything working!

Conclusion

I managed to get it working, and everything is okay with it. I pushed the code to the TechProwd GitHub account.

techprowd/php-discourse-telegram-mention-bot
A simple plain PHP Telegram Bot to notify about Discourse posts which has defined list of keywords and post those urls to the Telegram Channel. - techprowd/php-discourse-telegram-mention-bot

Don't forget to subscribe to the newsletters down bellow. Every new article will be delivered in a friendly email, readable format straight into your mailbox!

If you like my articles and would like to support me, here is my Patreon account:

Patreon
Patreon is a membership platform that makes it easy for artists and creators to get paid. Join over 200,000 creators earning salaries from over 6 million monthly patrons.