Created
July 7, 2018 14:19
-
-
Save bs1180/f69bc36f6f3255c291d1805639ba1f0b to your computer and use it in GitHub Desktop.
This runs as a stdlib.com serverless function (built with the code.xyz IDE)
This file contains hidden or 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 axios = require("axios"); | |
const R = require("ramda"); | |
module.exports = async (latitude = 0, longitude = 0) => { | |
const { data } = await axios.get("https://api.foursquare.com/v2/venues/explore", { | |
params: { | |
client_id: process.env.FOURSQUARE_CLIENT_ID, | |
client_secret: process.env.FOURSQUARE_CLIENT_SECRET, | |
v: 20180707, | |
section: "coffee", | |
limit: 3, | |
ll: `${latitude.toFixed(2)},${longitude.toFixed(2)}` | |
} | |
}); | |
const recommendations = R.path(["response", "groups", 0, "items"], data); | |
if (!recommendations || recommendations.length === 0) { | |
return { messages: [{ text: "Sorry, no recommendations for you! :(" }] }; | |
} | |
const venueIds = R.map(R.path(["venue", "id"]), recommendations); | |
const queries = venueIds.map(id => | |
axios.get(`https://api.foursquare.com/v2/venues/${id}`, { | |
params: { | |
client_id: process.env.FOURSQUARE_CLIENT_ID, | |
client_secret: process.env.FOURSQUARE_CLIENT_SECRET, | |
v: 20180707 | |
} | |
}) | |
); | |
const fullResults = await Promise.all(queries); | |
const payload = fullResults.map(res => { | |
const details = R.path(["data", "response", "venue"], res); | |
const photo = R.path(["photos", "groups", 0, "items", 0], details); | |
return { | |
title: details.name, | |
subtitle: "Rated " + R.propOr("unknown", "rating", details) + " " + R.path(["hours", "status"], details), | |
image_url: photo && photo.prefix + "400x400" + photo.suffix, | |
buttons: [ | |
{ | |
type: "web_url", | |
url: details.canonicalUrl, | |
title: "View on Foursquare" | |
} | |
] | |
}; | |
}); | |
return { | |
messages: [ | |
{ | |
attachment: { | |
type: "template", | |
payload: { | |
template_type: "generic", | |
image_aspect_ratio: "square", | |
elements: payload | |
} | |
} | |
} | |
] | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment