Bot Telegram

How to Integrate Jenkins with Telegram for Simple Build Notifications

D2Y MVN
5 min readOct 12, 2024

In many Jenkins use cases, you don’t always need a full-fledged CI/CD pipeline. Sometimes, you simply want a notification whenever a job succeeds or fails. Jenkins can easily send these notifications via Telegram using a bot and simple webhook commands.

In this tutorial, we will show you how to integrate Jenkins with Telegram to send build success and failure notifications without implementing a full CI/CD pipeline. The example script will include only basic pipeline stages, and we’ll use the curl command to send messages to Telegram directly.

Prerequisites

Before proceeding, make sure you have:

  1. Jenkins installed and running on your system.
  2. A Telegram account.
  3. A Telegram bot created using BotFather (this step is covered below).
  4. Admin access to create a pipeline job in Jenkins.

Step 1: Create a Telegram Bot Using BotFather

First, create a Telegram bot using the BotFather bot.

  1. Open the Telegram app and search for BotFather.

2. If so, then we click the start button at the top, then start a conversation with Botfather and type the command /newbot to create a new bot.

3. BotFather will ask for a name for your bot. Pick something like Jenkins_trial_bot. Then, choose a unique username for the bot, ending with “bot” (e.g., Jenkins_exp_bot).

4. After creation, BotFather will provide a token that is essential for sending messages. Save this token securely; you will use it later in Jenkins.

Step 2: Create a Telegram Group

You will need to create a Telegram group where the bot will send the build notifications.

  1. In Telegram, create a new group and add at least one member (yourself).
  2. Invite your newly created bot to the group by searching for its username and adding it as a member.
  3. Send a message in the group to make sure it’s active.
  4. To retrieve the Chat ID of the group, visit the following URL in a browser (replace <YourBotToken> with your actual bot token):
https://api.telegram.org/bot<YourBotToken>/getUpdates

5. Look for the JSON response that contains the chat object, and find the id field. This is your group’s Chat ID. Save this ID for later use.

Alternatively, you can use a bot created through BotFather. For instance, I was provided with this bot: t.me/Jenkins_exp_bot. Simply click the “Start” button and chat with the bot to interact with chatobjects and more.

Step 3: Set Up Jenkins Pipeline

Now that you have the bot token and Chat ID, it’s time to set up Jenkins to send notifications based on the build status.

  1. In Jenkins, create a new pipe job, then when done click OK.

2. Under the Pipeline section, choose Pipeline Script.

3. Use the following sample script to configure your pipeline:

pipeline {
agent any

stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post {
success {
script {
bat '''
curl -s -X POST https://api.telegram.org/bot<YourBotToken>/sendMessage -d chat_id=<YourChatID> -d text="Build Success: Job completed successfully!"
'
''
}
}
failure {
script {
bat '''
curl -s -X POST https://api.telegram.org/bot<YourBotToken>/sendMessage -d chat_id=<YourChatID> -d text="Build Failed: Job encountered an error!"
'
''
}
}
}
}

Explanation of the Script:

  • pipeline { }: This defines the entire Jenkins pipeline.
  • agent any: This tells Jenkins to run the pipeline on any available agent.
  • stages { }: The Hello stage is the core step in this basic pipeline, simply printing "Hello World".
  • post { }: The post section handles actions after the job is complete, including sending success or failure messages to Telegram.
  • success { }: When the job succeeds, a message “Build Success: Job completed successfully!” is sent to the Telegram group.
  • failure { }: When the job fails, the message “Build Failed: Job encountered an error!” is sent to the group.

Step 4: Modify the Script with Your Bot Token and Chat ID

Make sure to replace the placeholders <YourBotToken> and <YourChatID> with your actual bot token and chat ID:

curl -s -X POST https://api.telegram.org/bot<YourBotToken>/sendMessage -d chat_id=<YourChatID> -d text="Build Success!"

For example, if your bot token is 123456789:ABCDEF_GHIJKLMNOPQRSTU and your chat ID is 987654321, the command will look like this:

curl -s -X POST https://api.telegram.org/bot123456789:ABCDEF_GHIJKLMNOPQRSTU/sendMessage -d chat_id=987654321 -d text="Build Success!"

Step 5: Run and Test the Pipeline

After you’ve saved your pipeline script, manually run the pipeline:

  1. Click Build Now from the Jenkins job dashboard.
  2. Check your Telegram group after the build completes to see if you received a message.

You should receive one of two notifications:

  • Success: “Build Success: Job completed successfully!”
  • Failure: “Build Failed: Job encountered an error!”

Step 6: Troubleshooting

If you don’t receive notifications, double-check the following:

  • Bot Token: Ensure you are using the correct bot token provided by BotFather.
  • Chat ID: Verify that you’ve retrieved the correct chat ID from Telegram.
  • Network Issues: Make sure your Jenkins server can access the Telegram API (no network restrictions or firewalls).
  • Curl Errors: Check for any errors in the console output of your Jenkins job related to the curl command.

Conclusion

Integrating Jenkins with Telegram is a quick and simple way to receive notifications for build successes and failures. By using a straightforward curl command in the post-build actions of your Jenkins pipeline, you can stay informed about the status of your builds without the need for a full CI/CD pipeline setup. This integration is ideal for developers who want real-time feedback on their Jenkins jobs, whether they succeed or fail.

With just a few easy steps, you’ve set up instant notifications to keep you in the loop. Now, you’ll never miss another build status update again!

Happy Coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

D2Y MVN
D2Y MVN

Written by D2Y MVN

Lets make a plane and take a risk!

No responses yet

Write a response