Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeesCBakker/d90915f6d94bbe1f463c613d09d99266 to your computer and use it in GitHub Desktop.
Save KeesCBakker/d90915f6d94bbe1f463c613d09d99266 to your computer and use it in GitHub Desktop.
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