Skip to content

Instantly share code, notes, and snippets.

@nokenwa
Last active Jan 15, 2020
Embed
What would you like to do?
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