Created
January 19, 2018 14:31
-
-
Save KeesCBakker/d90915f6d94bbe1f463c613d09d99266 to your computer and use it in GitHub Desktop.
This file contains 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
const | |
norrisUrl = 'https://api.icndb.com/jokes/random', | |
decode = require('decode-html'); | |
//the export is used to init the bot | |
module.exports = (robot) => { | |
//listen to phrases that contain "Norris" | |
robot.hear(/Norris/ig, (res) => { | |
//wrap the HTTP get call as a Promise | |
new Promise((resolve, reject) => | |
robot.http(norrisUrl).get()((err, response, body) => | |
err ? reject(err) : resolve(body) | |
) | |
) | |
//parse to js object | |
.then(body => JSON.parse(body)) | |
//get joke - jokes may have stuff like " | |
.then(json => decode(json.value.joke)) | |
//reply joke | |
.then(joke => res.reply(joke)) | |
//problems? Annoy the user with the problem | |
.catch(err => res.reply('Not even Chuck Norris can deal with this one: ' + err)); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment