Setting up Telegram Bots for Jenkins Pipeline Alerts


Intro
Continuous Integration and Delivery (CI/CD) have revolutionised software development, enabling engineers and companies to develop, test, and deploy asynchronously while significantly reducing time-to-market for features and fixes.
Despite their overwhelming advantages, CI/CD pipelines must be designed for resilience. One critical safeguard is real-time alerts ensuring teams can respond to issues immediately, even on the go.
Telegram’s seamless bot integration offers a powerful solution for pipeline monitoring. By configuring a Telegram bot with Jenkins, teams receive instant notifications directly to their devices, keeping everyone informed, whether at their desks or on the go. In this article, we will walk through setting up a Telegram bot for Jenkins pipeline alerts, from bot creation to secured integration with Jenkins for actionable notifications.
Prerequisites
Basic knowledge of Jenkins & groovy syntax.
General idea of how CI/CD pipelines work.
Telegram mobile or web app.
Step 1 - The Bot Father
In order to get a telegram bot to setup a pipeline notification, we will need to visit The BotFather (yes, I acknowledge The GodFather reference :)).
The BotFather is Telegram's official bot creation and management tool that lets users create, customise, and control their own Telegram bots through simple chat commands.
Unlike The Godfather, reaching the BotFather It’s quite easy, just search “@BotFather” on telegram’s search and this should be the result:
The first option is your Father. Open the chat, there are useful links such as API documentations on how to effectively utilise your soon to be created bot, you can check that out if you want but it’s not mandatory for this operation. Proceed to click on the “Start” button below, you’ll see a couple of options including one to create a new bot /newbot
.
After choosing to create a new bot you’ll be prompted to enter a display name for the bot, you can choose any name at all (e.g., "Jenkins Pipeline Bot"), The BotFather will reply with a HTTP API token, keep this handy, we will be needing it soon.
Step 2 - Create a Group Chat
While we are still on telegram, there is one more thing we need and that’s to add the newly created bot to a telegram group chat with ourself and other related engineers so we can all simultaneously receive notifications from the bot. It’s quite straightforward process using telegram so i’ll let you do that on your own.
Next, we get the chat id of the group, the chat id is a unique number that telegram uses to identify every chat. Telegram uses a negative prefix (-) for group chats and a positive prefix (+) for direct messages (1:1) chats. We will use this chat id to direct messages/alerts to the group. To get the group chat id, we will Post any message in the group where your bot is added (e.g., "Test").
After posting a message on the group, open this URL in your browser (replace <BOT_TOKEN>
with your actual telegram bot token): https://api.telegram.org/bot<BOT_TOKEN>/getUpdates
replace “BOT_TOKEN” with the actual telegram bot token.
On the web-page there will be a brief history of messages sent and other configs, find the message you just sent, it will be alongside a JSON of chat id starting with “-” the minus sign is reserved for group chat ids while pone on one chat ids are positive. Save this ID, you’ll need it for Jenkins integration.
Step 3 - Jenkins Integration
Now that we have setup properly on the telegram side, let’s add a telegram alert step in our Jenkins pipeline. We will first need to attach the bot token and group chat id to our Jenkins setup as Jenkins secrets.
For this you will need to Go to your Jenkins Dashboard → Manage Jenkins → Credentials → System → Global credentials. This should be the UI on your screen:
Click on “Add Credential” and proceed to select “Secret Text” as the kind then insert your secret and the id (e.g: CHAT_ID: -12345678), proceed to create. Repeat step for bot token.
Now that we have done that, we will proceed to the final step; using the Jenkins groovy syntax to write a post pipeline configuration that handles success and failure events. Our final code should look something like this:
pipeline {
// Rest of your pipeline stages such as builds and tests
post {
failure {
script {
def duration = currentBuild.durationString.replace(' and counting', '')
def telegramMsg = """
🚨 *Pipeline Failed* 🚨
*Duration*: ${duration}
""".stripIndent()
sh """
curl -s -X POST "https://api.telegram.org/bot${env.TELEGRAM_TOKEN}/sendMessage" \
-d "chat_id=${env.CHAT_ID}" \
-d "text=${telegramMsg}" \
-d "parse_mode=MarkdownV2"
"""
}
}
success {
script {
def duration = currentBuild.durationString.replace(' and counting', '')
def telegramMsg = "✅ Pipeline Deployed Successfully: ${env.JOB_NAME} (${env.BUILD_NUMBER})\nView logs: ${env.BUILD_URL}"
sh """
curl -s -X POST "https://api.telegram.org/bot${env.TELEGRAM_TOKEN}/sendMessage" \
-d "chat_id=${env.CHAT_ID}" \
-d "text=${telegramMsg}" \
-d "parse_mode=MarkdownV2"
"""
}
}
}
}
As seen in the code snippet above, the duration
variable stores the time it took for our pipeline to run, telegramMsg
variable stores the message that will be sent on either failure or success events.
In an event where our pipeline fails, we should be getting this as a telegram notification:
And with that, we have setup telegram alerts on our Jenkins pipeline. By integrating Telegram alerts into your Jenkins pipeline, you’re not just automating deployment, you’re ensuring real-time visibility into your CI/CD process, no matter where you are. With your bot token and chat ID secured, you’re now ready to catch failures early, respond faster, and keep your team in sync.
Happy automating! 🤖
Subscribe to my newsletter
Read articles from Isreal Hogan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Isreal Hogan
Isreal Hogan
A drive for problem solving and new technologies. Here to write about anything and everything development.