Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
Created October 19, 2018 18:55
Show Gist options
  • Save DanLaufer/e69e9d527cc958e9f863d476409e4160 to your computer and use it in GitHub Desktop.
Save DanLaufer/e69e9d527cc958e9f863d476409e4160 to your computer and use it in GitHub Desktop.
Alexa Skill - Find lunch near fishtown
const respondToAskAboutLunch = require('./lunch-handler');
module.exports = {
AskAboutLunchIntent: respondToAskAboutLunch,
LaunchRequest: respondToLaunch,
'AMAZON.HelpIntent': respondToHelp,
'AMAZON.StopIntent': respondToCancel,
'AMAZON.CancelIntent': respondToCancel,
};
const helpSpeech = 'You can ask Lunchbot: where should I go to eat, or where can I get a certain kind of food?';
function respondToLaunch() {
this.emit(':ask', `Hello! ${helpSpeech}`);
}
function respondToHelp() {
this.emit(':ask', `Lunchbot will suggest an open restaurant for you in Fishtown, Philadelphia, based on what kind of food you're in the mood for. ${helpSpeech}`);
}
function respondToCancel() {
this.emit(':tell', 'Sure thing.');
}
const skill = require('./src/skill');
exports.handler = skill;
const yelp = require('./yelp');
module.exports = function respondToAskAboutLunch() {
if (this.event.request.dialogState !== 'COMPLETED') {
return this.emit(':delegate');
}
const intent = this.event.request.intent;
const cuisine = intent.slots.Cuisine.value.toLowerCase();
return getLunchOption(cuisine)
.then((option) => {
if (option) {
return this.emit(':tell', `You could go to ${option.name}. It's at ${option.location.address1}.`);
}
return this.emit(':tell', 'Sorry, I couldn\'t find anything nearby.');
})
.catch((error) => {
const errorBody = JSON.parse(error.response.body);
const errorCode = errorBody && errorBody.error.code;
if (errorCode === 'INVALID_CATEGORY') {
const promptSpeech = 'Sorry, I didn\'t catch that. What would you like?';
return this.emit(':elicitSlot', 'Cuisine', promptSpeech, promptSpeech);
}
const speech = 'Sorry, I had some trouble looking for restaurants. Try asking me again.';
return this.emit(':tell', speech);
});
};
function getLunchOption(cuisine) {
return yelp.fetchNearbyLunchOption(cuisine)
.then(options => getRandom(options));
}
function getRandom(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
const Alexa = require('alexa-sdk');
const handlers = require('./handlers');
module.exports = (event, context, callback) => {
const alexa = Alexa.handler(event, context, callback);
alexa.appId = process.env.ALEXA_APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const yelp = require('yelp-fusion');
function createClient() {
return yelp.client(process.env.YELP_ACCESS_TOKEN);
}
function fetchNearbyLunchOption(cuisine) {
return createClient()
.search({
categories: `${cuisine}`,
location: '1234 Frankford Ave, Philadelphia, PA 19125',
radius: 800,
})
.then(response => response.jsonBody.businesses);
}
module.exports = {
fetchNearbyLunchOption,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment