Created
November 28, 2017 23:34
-
-
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
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
// 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); | |
}); |
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
Can't make it work.