Skip to content

Instantly share code, notes, and snippets.

@nokenwa
Last active January 15, 2020 16:06
Show Gist options
  • Save nokenwa/f61f88ddc0c383a78bf8315503f1cbce to your computer and use it in GitHub Desktop.
Save nokenwa/f61f88ddc0c383a78bf8315503f1cbce to your computer and use it in GitHub Desktop.
Receive Restaurants based on Location Data in a Whatsapp Message (Twilio Function)
const axios = require("axios");
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
if (!event.Latitude || !event.Longitude) {
twiml.message("If you would like some food, please send me your location.");
callback(null, twiml);
} else {
const location = {
lat: event.Latitude,
lon: event.Longitude
};
axios
.get( `https://developers.zomato.com/api/v2.1/search?count=10&lat=${location.lat}&lon=${location.lon}&cuisines=healthy%20food`,
{
headers: {
Accept: "application/json",
"user-key": context.ZOMATO_API_KEY
}
}
)
.then(response => {
const restaurants = response.data.restaurants;
restaurants.forEach(restaurant => {
twiml.message(
`${restaurant.restaurant.name} - ${restaurant.restaurant.url}`
);
});
callback(null, twiml);
})
.catch(error => {
callback(error);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment