Skip to content

Instantly share code, notes, and snippets.

@MarcL
Created November 28, 2017 23:34
Show Gist options
  • Save MarcL/50057a5c389f9cdfff8e17bad14152ce to your computer and use it in GitHub Desktop.
Save MarcL/50057a5c389f9cdfff8e17bad14152ce to your computer and use it in GitHub Desktop.
Node.js Code for Chatfuel Joke Bot - https://www.youtube.com/watch?v=oLFOy0f4AFQ
// server.js
// where your node app starts
// init project
var express = require('express');
var app = express();
var requestPromise = require('request-promise');
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});
app.get('/jokes', function(request, response) {
const requestOptions = {
uri: 'https://icanhazdadjoke.com/',
headers: {
Accept: 'application/json'
},
json: true
};
requestPromise(requestOptions)
.then(function(data) {
response.json({
messages: [
{text: data.joke},
{
"attachment": {
"type": "image",
"payload": {
"url": "https://media.giphy.com/media/3i7zenReaUuI0/giphy.gif"
}
}
}
]
});
});
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
@rlccphil
Copy link

Can't make it work.

@aroraakshit
Copy link

Thank you so much! It works perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment