Post

Create a Telegram bot and send a message

Telegram provides an API service that is relatively easy to use. Therefore, it can be used as an easy tool to notify events in CI/CD systems where security is not a major issue. I am trying to integrate Telegram into the Jenkins system, and I would like to introduce the basic method of creating a Telegram bot.

Overview

To send a Telegram message, token matching the Telegram bot and chatid indicating the chat room are required.

  • token : You can see this when creating a bot in Telegram
  • chatid : Once you have found the token, you can find it by calling the Telegram API getUpdates using the token.

In conclusion, if you have found both the token and chatid, you can send an im bot message to the Telegram chat room by calling the URL below.

1
2
3
4
5
6
7
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chatid>&text=im bot

# example
- token: 6870000000:AAEz8Oaaaaaaaaaa-RBixhsHabbbbbbbbbb
- chatid : 1230000000

https://api.telegram.org/bot6870000000:AAEz8Oaaaaaaaaaa-RBixhsHabbbbbbbbbb/sendMessage?chat_id=1230000000&text=im bot

Create a Telegram bot - token

Find botfather on Telegram and enter the chat room. BotFather is a service bot that can create/manage Telegram bots.

Telegram - Search `BotFather` Telegram - Search BotFather

Create a new bot using the newbot command.

BotFather asks for the bot’s name and username. In effect, username is the chat room where you will communicate with the bot. If a Telegram bot is successfully created, BotFather provides a token, which is an argument for communicating with the bot.

Telegram - `BotFather` - `newbot` Telegram - BotFather - newbot

Token is the most basic credential for controlling bots using the Telegram API. Therefore, write it down carefully so that it is not made public.

Telegram getUpdates API call - chatid

If BotFather created a bot, find the bot in Telegram and send it a message. I sent a message saying hello.

Telegram - Enter `hello` on Bot' Chat Room Telegram - Enter hello on Bot’ Chat Room

Then, in the web browser you use, such as Chrome or Edge, call the URL below by combining it with the token found when creating the bot above.

1
2
3
4
5
6
7
https://api.telegram.org/bot<token>/getUpdates

# example
- token: 6870000000:AAEz8Oaaaaaaaaaa-RBixhsHabbbbbbbbbb

https://api.telegram.org/bot6870000000:AAEz8Oaaaaaaaaaa-RBixhsHabbbbbbbbbb/getUpdates

Then, a string in JSON format will be displayed in the browser, and the id in chat will be the chatid. Take note of the chatid. If you use the Edge browser, you can see the line-aligned JSON string.

Internet Browser - Telegram - `getUpdates` Internet Browser - Telegram - getUpdates

Send a Telegram message via browser - sendMessage API

In conclusion, we will use the Jenkins plugin to send a Telegram message, but for testing purposes we will send the message using the sendMessage Telegram API to the browser.

The API is as follows, and you can send a Telegram message using the token and chatid found in the steps above.

1
2
3
4
5
6
7
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chatid>&text=im bot

# example
- token: 6870000000:AAEz8Oaaaaaaaaaa-RBixhsHabbbbbbbbbb
- chatid : 1230000000

https://api.telegram.org/bot6870000000:AAEz8Oaaaaaaaaaa-RBixhsHabbbbbbbbbb/sendMessage?chat_id=1230000000&text=im bot

Did it work well? I was able to check the message im bot on Telegram as shown below.

Telegram - `sendMessage` API Telegram - sendMessage API

This post is licensed under CC BY 4.0 by the author.