Last active
October 26, 2017 21:54
-
-
Save carlospliego/81a07c9cf9de54e834df75776a145dba to your computer and use it in GitHub Desktop.
Creating a Chuck Norris themed Slack app using Webtask.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment