Telegram notifications for Jenkins builds

It’s nice to get updates from your CI system when things build. It’s even nicer to do it without having to run any servers to do it. Here’s how I send build notifications to a Telegram bot from Jenkins.

Basically, Jenkins knows how to hit a webhook for every stage of the build, and Integram run a Telegram bot which knows how to respond to webhooks. All you need is a little bit of glue code to convert stuff Jenkins sends into stuff Integram receives, and you can put that glue code on any one of fifteen serverless systems. I used webtask.io.

The Webtask.io editor

Deploy the following code there, being sure to update the Integram URL in it to be yours:

var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
var Webtask;
try {
    Webtask = require('webtask-tools');
} catch(e) {}
var app = express()
app.use(bodyParser.json())
app.post('*', function (req, res) {
    if (req.body && req.body.name && req.body.build && req.body.build.url) {
        var status = req.body.build.status;
        if (req.body.build.phase == "STARTED") { status = "STARTED"; }
        if (req.body.build.phase == "FINALIZED") {
            // don't care about FINALIZED because we get COMPLETED and that's enough
            res.send("ok");
            return;
        }
        console.log("Received correct-looking JSON to webhook");
        var output = {"text": "Jenkins\nBuild of _" + req.body.name +
            "_ status *" + status + "* " +
            "at http://YOUR-JENKINS-URL/" + req.body.build.url};
        request({
            uri: "https://integram.org/YOUR-INTEGRAM-URL",
            method: "POST",
            json: output
        }, function(err) {
            if (err) {
                console.log("Messaging Telegram bot failed", err);
                res.send("failed");
            } else {
                console.log("Messaging Telegram bot succeeded");
                res.send("ok");
            }
        })
    } else {
        console.log("Input to webhook was invalid", req.body);
        res.send("Input was invalid: " + req.body.toString());
    }
})
if (Webtask) {
    module.exports = Webtask.fromExpress(app);
} else {
    app.listen(4569);
    console.log("Listening on port 4569");
}

Then, put the webtask URL into Jenkins as a webhook:

Add a notifications endpoint in the configure section of your build job

And invite the Integram bot to your Telegram channel (instructions at integram.org).

And you’re done. Every time Jenkins does anything, it sends a web request to your webtask, the webtask sends a message to the Integram bot, the Integram bot repeats it to you, and you get a notification. No extra servers required. I love the internet.

The Telegram bot speaks the message it's given, and so Telegram notifies you

More in the discussion (powered by webmentions)

  • Stephen Parkes responded at twitter.com It's easy (and serverless) to have your CI system send notifications to Telegram (or Slack etc). Here's how I did it kryogenix.org/days/2017/10/2…
  • Michael Scharnagl responded at ... (twitter.com)
  • Birmingham.IO responded at twitter.com Stuart Langridge: Telegram notifications for Jenkins builds by @sil buff.ly/2lkN4c8 #PlanetBirmingham
  • 007 responded at twitter.com It's easy (and serverless) to have your CI system send notifications to Telegram (or Slack etc). Here's how I did it kryogenix.org/days/2017/10/2…
  • Geoff Goodman responded at twitter.com It's easy (and serverless) to have your CI system send notifications to Telegram (or Slack etc). Here's how I did it kryogenix.org/days/2017/10/2…
  • Gleb Bahmutov responded at ... (twitter.com)
  • AnonOntario responded at twitter.com It's easy (and serverless) to have your CI system send notifications to Telegram (or Slack etc). Here's how I did it kryogenix.org/days/2017/10/2…
  • AnonOntario responded at ... (twitter.com)
  • Ahmed Mozaly responded at twitter.com It's easy (and serverless) to have your CI system send notifications to Telegram (or Slack etc). Here's how I did it kryogenix.org/days/2017/10/2…