The Telegram version of this exercise is provided by Sahil Rajput
NB: In case you end up having a lot of workflows running simultaneously (when you push a commit) in your github actions tab, you can disable all workflows except the one you are currently working on, as shown in the figure.
To get started, you need to create a telegram bot and to do so you have to start by sending a message /start to @BotFather which is itself a bot to help users in creating and managing their own custom bots. Further, you need to send /newbot to create a new bot of your own, and follow the process. Though the process only consists of asking for name and username (must be unique, e.g., my_responsible_bot1) for our bot. After creating the bot we can request the token for the bot using /token message.
Now make a group on telegram, say “My CI-CD Notifications” using your personal telegram account on mobile app or desktop web app of telegram. After that you’ll be prompted to add users, just enter your bot’s username there (e.g., @my_responsible_bot1) to add bot to the group. You can also use any existing telegram group too if you want but you need to be administrator of that group to be able to add a bot to it as per telegram’s security policies.
Any bot needs a chat id to send any sort of message to a user, group or channel. In telegram, chat id is a unique identifier for any user/group/channel and our bot will use it to send message to the group we just created. Since we added our bot to the group, the backend api of telegram has recorded the event for it and we can see that event’s details to fetch the chat id of our group.
To fetch the chat id of the group we can use either of below ways:
Way 1: Remove your chat bot manually from the group and add it again and we are good to go(ignore this if you added the chat bot to already existing group). We need to do this because there’s some issue with adding a chat bot at the time of creating a new group, read the most rated comment of this stackoverflow answer. Browse https://api.telegram.org/bot<BOT_TOKEN>/getUpdates
to view the event log history of our bot, and look for something title: “My CI-CD Notifications” you’ll notice there is the id for the group in there too. That is our chat id for the group, it looks something like -123456789. Note the hyphen(-) as it is part of chat id, telegram uses - for groups and channels ids as to distinguish them from user accounts. If reading from the json is struggling for you, there’s a easy way to get chat id of the group using way 2.
Way 2 (easy way): Add @getidsbot using Add Member option in group settings and you’ll see the chat id of the group as response from this bot along with other details of the group i.e., title, username, etc. After getting chat id of the group you can remove the @getidsbot from the group.
Add two environment variables to github repo i.e., TELEGRAM_TO for chat id of the group and TELEGRAM_TOKEN for the bot’s token which we fetched earlier while creating the bot with botfather. You can navigate to Settings > Secrets > New Repository Secret in your github repo to add these environment variables.
Create a new workflow file say TelegramNotifcation.yml and add a job using actions/telegram-message-notify github action and try if the messages are delivered to your telegram group. Tip: You can have a default message which includes basic log of the workflow event simply by omitting the args from the actions/telegram-message-notify job’s definition.
Add another step to your job in existing TelegramNotifcation.yml workflow file to deliver message directly to yourself by using chat id associated with your own account. To do this add another secret say TELEGRAM_TO_ME to your github repo settings. Probably doing this exercise, you would see an error which would break your workflow saying chat not found, this is a security concern made by telegram, so you would first need to send a message to bot first and instantly after that your new workflow events will succeed. This security concern ensures that any chatbot might not end up sending spams to any unauthorized user otherwise any hacker could easily spam you in a variety of way using his/her bot.
Tip: You can get the chat id of your personal telegram account by sending a “Hello” message to @userinfobot simply. You can also use @userinfobot link to message the bot directly from mobile or using web version of telegram on your desktop to send the “Hello” message.