Telegram bot in Go app. Notifications are important part of any… – Telegram 中文版

Telegram

Telegram bot in Go app

Notifications are important part of any consumer oriented application. There are a wide variety of notifications with mobile push notifications to users as a major mechanism. But there is a caveat to app notifications that you need to have a consumer facing client or app.

I am a backend guy and haven’t really got any experience with building user facing UI or a dashboard. I like to play around building somethings useful but too lazy to build a frontend or an app to it. I sometimes even my colleagues to build it for me but with no monetary incentive, either they deny or loose interest along the way. Recently I came about this concept of telegram bots to deliver notifications and got interested to learn about it.

The primary usecases which I felt it would be helpful are :
1. Build a notification mechanism on telegram chat app for subscribers.
2. The notification can be hooked up with API endpoint links to your backend which can be used to open some URL in the browser.
3. Reminders or promotional events.

UseCase

The usecase I am trying to solve is:
1. My news app backend syncs news from google news.
2. The user on telegram subscribes to the bot.
3. When the user tyes “Hi”, I need to deliver the top 5 news links for the day to the subscriber. Important point to note is that the url is of the publishers and not my backend.
4. A cron also runs at the back which collects the top 5 news for the day and pushes notification to all the subscribers of the bot.

PS : I will not be discussing the news app backend which I would have referred to in my previous blogposts. This post only details the telegram bot and how it uses push notifications to deliver the news.

Lets start!
First, lets create a new bot in telegram. This link documents each and every aspect of the bot world in telegram. Since this is to be done on the app itself, I will just highlight the steps in bullet :

1. The big daddy of all bots in telegram is the BotFather. You need to find this as chat with it to create a bot. All bot converstations start with “/start” in the chat.
2. Highlighting the command sequence
>> /start
>> /newbot
>> <bot_name>
This is it. A bot is created. Attaching a screenshot for the same

Now you can go to the url t.me/<bot_username> and add your bot to your chat list. Or you can search your bot in the app.

The bot is ready. Now you can explore the various functionalities provided by the bot.

Backend integration with bot

For interacting with your bot via your backend server, you need the following

  1. An auth token — Which you would have got while creating the bot in botfather
  2. Understanding about telegram chat APIs — refer to the link shared above about documentation of the API
  3. Your own backend.

The can be two mechanism of notifications as was discussed — pull and push. 1. The pull mechanism would be to poll a telegram updates API to get the information about who subscribed to your bot or what message was posted by your subscribers. This has to be run in periodical intervals. And respond based on the updates received.

https://api.telegram.org/bot<auth-token>/getUpdates

2. The other mechanism is push wherein our backend receives the updates from telegram servers in realtime for any user action using webhooks. This is the mechanism that I have opted for since this is more realistic and provides better UX. Create APIs at my end and register it as a webhook.

https://api.telegram.org/bot<auth-token>/setWebhook?url={your_API_server_url}

3. Telegram Send API is used to push the notification from our backend to the telegram bot.

https://api.telegram.org/bot<auth-token>/sendMessage?chat_id={chat_id}&text={text}

Enough talk!!! Show me the code…

Entity/Dto layer

Service layer

Telegram HTTP client (Auth key is removed)

This is it. This is enough code to send notifications to the bot subcribers.
Mentioning this again. Since the approach is realtime updates using webhooks and replying from our server to telegram bot subscriber, the chat_id info that is required in the SEND API can be extracted from the telegram subscriber update and replied back.
The alternative approach would be to persist
chat_id in some storage(I use cassandra table). This can then be used later to push message to the subscriber without any subscriber actions as a reminder notification mechanism. I use it for periodic notifications like a cron.

Reply to user with news links
Backend started conversation as notification

Ten articles before and after

Monitorar suas aplicações na AWS usando o Telegram – Telegram 中文版

Example of a telegram bot in python without using specific libraries – Telegram 中文版

Домашняя бухгалтерия в telegram. Электронные чеки. QR. – Telegram 中文版

How to write UK Rent Telegram Bot – Telegram 中文版

Tasker and Telegram integration: live location – Telegram 中文版

Домашняя бухгалтерия в telegram. Начало. – Telegram 中文版

data-rh=”true”>Telegram Bot for Lazy – Max Makhrov – Medium – Telegram 中文版

Django Telegram Bot. В данной статье я расскажу о том как… – Telegram 中文版

Stream Tweets to a Telegram channel – Telegram 中文版

Introducing zilWatchBot. We are proud to introduce zilWatchBot… – Telegram 中文版

About Me

Pretium lorem primis senectus habitasse lectus donec ultricies tortor adipiscing fusce morbi volutpat pellentesque consectetur risus curae malesuada dignissim lacus convallis massa mauris.