Setting up Telegram Bots for Jenkins Pipeline Alerts

Isreal HoganIsreal Hogan
4 min read

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 alert 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. This article walks through setting up a Telegram bot for Jenkins pipeline alerts, from bot creation to secure integration and actionable notifications.

Prerequisites

  1. Basic knowledge of Jenkins & groovy syntax.

  2. General idea of how CI/CD pipelines work.

  3. Telegram mobile or web app.

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.

Reaching the BotFather It’s quite easy, just search “@BotFather” on telegram, 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, you’ll see a couple of options including one to create a new bot /newbot.

Enter a display name for your bot (e.g., "Jenkins Pipeline Bot"), The BotFather will reply with a HTTP API token, save this securely, we will be needing it soon.

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, that’s quite straightforward so i’ll let you do that on your own.

Next step is getting the chat id of the group, to do that, we will Post any message in the group where your bot is added (e.g., "Test"). 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 there will be a 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.

Jenkins

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 DashboardManage JenkinsCredentialsSystemGlobal 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! 🤖

0
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.