Enhancing Jenkins Automation: Integrating with Telegram Notifications

Zuda Pradana Putra
4 min readDec 4, 2023

In our continuous journey to fortify our development pipelines, we’ve previously explored the synergies of Jenkins and SonarQube. Building upon that foundation, this article delves into the realm of real-time notifications by integrating Jenkins with Telegram. Let’s streamline our communication channels and stay informed on the pulse of our pipelines. This project will only cover simple automatic message sending, by triggering the previous pipeline so that it will send if the pipeline is successfully created.

1. Creating a Telegram Bot with BotFather

On Desktop or Mobile: Opening the Telegram App Whether you’re on your desktop or mobile device, launch the Telegram app.

  1. In the search bar at the top of the Telegram app, type “BotFather.”
  2. From the search results, select the official @BotFather account or click this link.
Creating Telegram Bot

Open a chat with BotFather by clicking on the “Start” button. Once the chat is initiated, send the command /newbot. Follow the on-screen instructions from BotFather. You’ll be prompted to choose a name and username for your bot. Upon successful creation, BotFather will provide you with a unique API token for your newly created bot. Copy this token securely; we’ll need it for Jenkins to communicate with Telegram.

3. you still need one more thing, namely getting a telegram ChatID. you can search @myidbot or use the link. after you enter the chat room you can initiate it with /start then /getid, the bot will automatically provide your chatid. Copy and securely save the provided Chat ID. This ID is essential for configuring Jenkins to send notifications to your Telegram account.

2. Creating Jenkins Global Credentials

When it comes to integrating Jenkins with external services like Telegram, safeguarding sensitive information is paramount. The creation of global credentials ensures the confidentiality of critical data, such as your Telegram Bot Token and Chat ID.

  1. Token Credential:
  • The first credential, holding your Telegram Bot Token, acts as the secret key allowing Jenkins to authenticate and communicate with your Telegram bot.

2. Chat ID Credential:

  • The second credential stores your Telegram Chat ID, enabling Jenkins to accurately direct notifications to your specific chat room.
Add Telegram Credentials

Key Security Measures:

  1. Access Control:
  • By creating global credentials, you restrict access to this sensitive information, ensuring that only authorized entities (like Jenkins pipelines) can use them.

2. Script Security:

  • When scripting your Jenkins pipeline, you can refer to these credentials by their IDs without exposing the actual values, maintaining a secure and controlled environment.

3. Creating Pipeline for notify Telegram

As we strengthen our Jenkins setup, a vital move is creating a special pathway for Telegram alerts. In the Jenkins dashboard, we start a new section, naming it “TelegramNotifications.” This system is set up to get its instructions straight from our GitHub storage using the “Pipeline script from SCM” feature. It relies on the GitHub link, repository specifics, and a customized Jenkinsfile2 made especially for Telegram notifications.

Build Pipeline

but before you build the pipeline, let’s take a look at the jenkins2 file:

pipeline {
agent any

environment {
// Telegram configuration
TOKEN = credentials('telegram-credentials')
CHAT_ID = credentials('Telegram_ChatID')
}

stages {
stage('Run Stage') {
steps {
echo 'Running'
}
}
}

post {
success {
script {
bat "curl -X POST -H \"Content-Type: application/json\" -d \"{\\\"chat_id\\\":${CHAT_ID}, \\\"text\\\": \\\"Pipeline succeeded!\\\", \\\"disable_notification\\\": false}\" https://api.telegram.org/bot${TOKEN}/sendMessage"
}
}
failure {
script {
bat "curl -X POST -H \"Content-Type: application/json\" -d \"{\\\"chat_id\\\":${CHAT_ID}, \\\"text\\\": \\\"Pipeline failed!\\\", \\\"disable_notification\\\": false}\" https://api.telegram.org/bot${TOKEN}/sendMessage"
}
}
}
}

The provided Jenkins file outlines a Jenkins Pipeline configuration tailored for Telegram notifications upon the completion of specific stages. Let’s break down key components:

  1. Post-Build Actions:
  • post: Defines actions to be taken after the stages are completed.
  • success: If the stages complete successfully, a curl script sends a message to Telegram stating "Pipeline succeeded!"
  • failure: In case of failure, a Telegram message is sent indicating "Pipeline failed!"
  1. Script using bat:
  • The bat command is used to run the curl command using the Batch shell in a Windows environment.

2. Curl Commands:

  • Both curl commands are utilized to send messages to Telegram using the Telegram Bot API. A success message is dispatched when the pipeline succeeds, and a failure message is sent upon encountering issues.

This Jenkins file serves as a robust foundation for integrating Telegram notifications into your Jenkins workflow, ensuring your team receives real-time updates on the pipeline status. As I said at the beginning, I want to trigger the Jenkins file for sonar that I described in my previous article. I added a post script like this:

 post {
success {
script {
// Trigger another pipeline upon success
build job: 'telegram-notification' //your pipeline for telegram
}
}
failure {
script {
// Trigger another pipeline upon failure
build job: 'telegram-notification' //your pipeline for telegram
}
}
}

With one last click to save the configuration, the stage is complete. Running a pipeline or attempting to trigger from an existing pipeline in the previous article marks the start of a new era, where Jenkins will communicate successes and failures directly to our Telegram feed.

--

--