This post originated from an RSS feed registered with Web Buzz
by Stuart Langridge.
Original Post: Telegram notifications for Jenkins builds
Feed Title: as days pass by
Feed URL: http://feeds.feedburner.com/kryogenix
Feed Description: scratched tallies on the prison wall
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.
Deploy the following code there, being sure to update the Integram URL in it to be yours:
varexpress=require('express')varbodyParser=require('body-parser')varrequest=require('request')varWebtask;try{Webtask=require('webtask-tools');}catch(e){}varapp=express()app.use(bodyParser.json())app.post('*',function(req,res){if(req.body&&req.body.name&&req.body.build&&req.body.build.url){varstatus=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 enoughres.send("ok");return;}console.log("Received correct-looking JSON to webhook");varoutput={"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:
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.