Created
January 2, 2019 10:14
-
-
Save germanviscuso/e75af3d6c14775939da1a036bb628acd to your computer and use it in GitHub Desktop.
Alexa Skill Basics: Reminders API using the Request library
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
{ | |
"interactionModel": { | |
"languageModel": { | |
"invocationName": "demo of reminders", | |
"types": [ | |
], | |
"intents": [ | |
{ | |
"name": "AMAZON.CancelIntent", | |
"samples": [ | |
] | |
}, | |
{ | |
"name": "AMAZON.HelpIntent", | |
"samples": [ | |
] | |
}, | |
{ | |
"name": "AMAZON.StopIntent", | |
"samples": [ | |
] | |
}, | |
{ | |
"name": "SetReminderIntent", | |
"slots": [ | |
], | |
"samples": [ | |
"set reminder", | |
"set a reminder", | |
"reminder", | |
"remind me" | |
] | |
} | |
] | |
} | |
} | |
} |
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
// Now obsolete: please use https://github.com/germanviscuso/alexa-cookbook/blob/feature-demos/feature-demos/skill-demo-reminders/lambda/custom/index.js | |
/* eslint-disable func-names */ | |
/* eslint-disable no-console */ | |
const Alexa = require('ask-sdk'); | |
const moment = require('moment-timezone'); | |
const rp = require('request-promise'); | |
// Hanldlers | |
const LaunchRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'Welcome to the reminders a.p.i. demo, you can say: set a reminder'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.getResponse(); | |
}, | |
}; | |
const SetReminderIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'SetReminderIntent'; | |
}, | |
async handle(handlerInput) { | |
const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput; | |
const {deviceId} = requestEnvelope.context.System.device; | |
const minutes = 1; //set reminder 1 minute from now | |
const reminderMessage = "This is just a demo reminder. Here you go!"; | |
const alert = {}; | |
let timezone, now, consentToken; | |
consentToken = requestEnvelope.context.System.apiAccessToken; | |
if(!consentToken) { | |
return responseBuilder | |
.speak('You need to enable permissions in the Alexa app so this skill can send you reminders. I just sent you a card to do this') | |
.withAskForPermissionsConsentCard(['alexa::alerts:reminders:skill:readwrite']) | |
.getResponse(); | |
} | |
const upsServiceClient = serviceClientFactory.getUpsServiceClient(); | |
const tz = await upsServiceClient.getSystemTimeZone(deviceId); | |
timezone = tz ? tz : 'Europe/Madrid'; | |
moment.locale('en'); | |
now = moment().tz(timezone); | |
alert.requestTime = now.format('YYYY-MM-DDTHH:mm:00.000'); | |
alert.trigger = { | |
type: 'SCHEDULED_ABSOLUTE', | |
scheduledTime: now.add(minutes, 'minutes').format('YYYY-MM-DDTHH:mm:00.000'), | |
timeZoneId: timezone, | |
}; | |
alert.alertInfo = { | |
spokenInfo: { | |
content: [{ | |
locale: requestEnvelope.request.locale, | |
text: reminderMessage | |
}] | |
} | |
}; | |
alert.pushNotification = { | |
status: 'ENABLED' | |
}; | |
const params = { | |
url: requestEnvelope.context.System.apiEndpoint + '/v1/alerts/reminders', | |
method: 'POST', | |
headers: { | |
'Authorization': 'bearer ' + consentToken, | |
}, | |
json: alert, | |
}; | |
// Post the reminder | |
console.log('Params: ' + JSON.stringify(params)); | |
let result = await rp(params); | |
console.log('Result: ' + JSON.stringify(result)); | |
let speechOut; | |
if(result.status === "ON") | |
return handlerInput.responseBuilder | |
.speak('Done!') | |
.getResponse(); | |
else | |
return handlerInput.responseBuilder | |
.speak('Failed to create reminder. Sorry!') | |
.getResponse(); | |
} | |
} | |
const HelpIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'You can say set reminder'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.getResponse(); | |
}, | |
}; | |
const CancelAndStopIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent' | |
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent'); | |
}, | |
handle(handlerInput) { | |
const speechText = 'Goodbye!'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.getResponse(); | |
}, | |
}; | |
const SessionEndedRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest'; | |
}, | |
handle(handlerInput) { | |
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`); | |
return handlerInput.responseBuilder.getResponse(); | |
}, | |
}; | |
const ErrorHandler = { | |
canHandle() { | |
return true; | |
}, | |
handle(handlerInput, error) { | |
console.log('Error handled: ' + JSON.stringify(error)); | |
return handlerInput.responseBuilder | |
.speak('Sorry, there was an error. Please say again.') | |
.reprompt('Sorry, there was an error Please say again.') | |
.getResponse(); | |
}, | |
}; | |
const skillBuilder = Alexa.SkillBuilders.custom(); | |
exports.handler = skillBuilder | |
.addRequestHandlers( | |
LaunchRequestHandler, | |
SetReminderIntentHandler, | |
HelpIntentHandler, | |
CancelAndStopIntentHandler, | |
SessionEndedRequestHandler | |
) | |
.addErrorHandlers(ErrorHandler) | |
.withApiClient(new Alexa.DefaultApiClient()) | |
.lambda(); |
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
{ | |
"name": "reminder-demo", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"ask-sdk": "^2.3.0", | |
"moment-timezone": "^0.5.23", | |
"request": "^2.88.0", | |
"request-promise": "^4.2.2" | |
} | |
} |
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
{ | |
"manifest": { | |
"publishingInformation": { | |
"locales": { | |
"en-US": { | |
"summary": "A simple deo of the Reminders API", | |
"examplePhrases": [ | |
"Alexa open reminder demo" | |
], | |
"name": "reminders-demo", | |
"description": "Sample Full Description" | |
} | |
}, | |
"isAvailableWorldwide": true, | |
"testingInstructions": "Sample Testing Instructions.", | |
"category": "EDUCATION_AND_REFERENCE", | |
"distributionCountries": [] | |
}, | |
"apis": { | |
"custom": { | |
"endpoint": { | |
"sourceDir": "lambda/custom" | |
} | |
} | |
}, | |
"manifestVersion": "1.0", | |
"permissions": [ | |
{ | |
"name": "alexa::alerts:reminders:skill:readwrite" | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment