Creating a Chuck Norris themed Slack app using Webtask.io

You’ll see how easy it is to decorate your normal workplace conversations with useful Chuck Norris pros like “Chuck Norris puts mustard gas on his hotdogs.” As exciting as he is, the point of this post is to illustrate how easy it is to create a slack app with webtask.io. In fact! the Chuck Norris public api we’ll be using already has a slack application. https://api.chucknorris.io/

Install Slash Webtask

Right so the first thing you’ll want to do is install the wt slack application to your slack team. Click Here to install Slash Webtask app.

Creating the application

Type /wt create norris ( or whatever you want to call it ).

Editing the source

click on “Edit it in Webtask Editor”.

See this? Good!

module.exports = (ctx, cb) => {
cb(null, { text: `Hello, @${ctx.body.user_name}!` });
}

Thats it! now you have a slack app.. now lets make it fun.

Lets add some Chuck

"use strict";
const rp = require('request-promise');
const Promise = require("bluebird");
// Get Random Chuck Norris Quote
const getRandomChuckNorrisQuote = () => {

return rp({
uri:"https://api.chucknorris.io/jokes/random",
json:true
}).then(data=>data.value);
}
const askChuckNorris = (q) => {  
  return rp({
uri:`https://api.chucknorris.io/jokes/search?query=${q}`,
json:true
}).then((data)=>{
if(data.total){ // return a random result
return data.result[
Math.floor(Math.random()*data.total, 1)
].value;
}else{
return `I don't understand ${q}`;
}
});
}
module.exports = (ctx, cb) => {

const errorHandler = (err) =>{
cb(err);
}

const successHandler = (text) =>{
cb(null, {text});
}

const params = ctx.body.text;

if(params){
askChuckNorris(params)
.then(successHandler)
.catch(errorHandler);
}else{
getRandomChuckNorrisQuote()
.then(successHandler)
.catch(errorHandler);
}
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.