Last active
December 2, 2017 11:23
-
-
Save tony-go/53b4c6455ec2d3f21871c261300c9006 to your computer and use it in GitHub Desktop.
Diner Bot for slack
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
var request = require('request'); | |
module.exports = function (context, cb) { | |
var demand = context.body.text.replace(context.secrets.TRIGGER, ''); | |
var request_url = 'https://api.flickr.com/services/rest/'; | |
var final_url = request_url | |
+ '?method=flickr.photos.search' | |
+ '&api_key=' + context.secrets.API_KEY | |
+ '&tags=' + demand | |
+ '&per_page=10&page=1' | |
+ '&format=json' | |
+ '&nojsoncallback=1'; | |
request.get(final_url, function (err, res, body) { | |
if (err) { | |
console.log(err); | |
} else { | |
var random = Math.round(Math.random()) | |
var result = JSON.parse(body); | |
var photoId = result.photos.photo[random].id; | |
var url_photo = request_url + '?method=flickr.photos.getSizes' | |
+ '&api_key=' + context.secrets.API_KEY | |
+ '&photo_id=' + photoId | |
+ '&format=json' | |
+ '&nojsoncallback=1'; | |
request.get(url_photo, function(err, res, body){ | |
if (err) { | |
console.log(err); | |
} else { | |
var result = JSON.parse(body); | |
var picture_URL = result.sizes.size[4].source; | |
cb(null, { text : picture_URL }); | |
} | |
}) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment