This project aims at simplifying sending proactive events to a skill from an external standalone process.
For each one of the skills configured in skills.json, this code performs the following operations:
- Obtains an authentication token from Alexa by using the skill's
client_idandclient_secret. You can obtain these in the Build Tab - Permissions section of your skill in the Alexa developer console (if they are not visible try enabling a permission such as reminder and then disable it). - Uses the authentication token obtained in the previous step to multicast notifications to those users of the skill that granted permissions to notifications in the Alexa app. Sends events following the
AMAZON.MessageAlert.Activatedschema. You can edit the event template inmessage-template.jsonif you need to. The propertymessageAlert.event.payload.messageGroup.creator.namein the template is overridden at runtime using themessageproperty provided inskills.jsonso be careful if you change to a different schema as this will result in an error.
skills.json is an array of objects with the following properties:
skill_name: this will only be used for logging purposes.client_id: the client_id obtained from the "Permissions" tab of Alexa developer console.client_secret: the client_secret obtained from the "Permissions" tab of Alexa developer console.message: the message you want to send to users which will be sent as the name of the sender.validity_hours: number of hours during which this message will be considered valid (defaults to 24).
Remember that if you want to send proactive events to your skill, you need to modify the skilll manifest (skill.json) to add notification permissions to the skill and declare the schema(s) of the proactive events your skill is allowed to send. Using SMAPI, you need to:
- Add the
permissionsobject to the skill'smanifestproperty (skill.json):
"permissions": [
{
"name": "alexa::devices:all:notifications:write"
}
]
- Add the
eventsobject to the skill'smanifestproperty:
"events": {
"publications": [
{
"eventName": "AMAZON.MessageAlert.Activated"
}
],
"endpoint": {
"uri": "** TODO: REPLACE WITH YOUR Lambda ARN after created **"
},
"subscriptions": [
{
"eventName": "SKILL_PROACTIVE_SUBSCRIPTION_CHANGED"
}
]
}
- Redeploy your skill by running:
ask deploy -t skill
If you want to declare that the skill accepts more than one schema, just add them into events.publications above and remember to change the template in message-template.json.
The file skill.json contains sample skill manifest for your reference.
The process is more thoroughly described in the official Alexa Proactive Events API documentation.
You can find a working example on how to configure your skill for Proactive Events in the official Alexa Github repository: https://github.com/alexa/alexa-cookbook/tree/master/feature-demos/skill-demo-proactive-events
You need Node.js >= 10.x to run the code and NPM to install the required dependencies.
How to install these two tools goes beyond the scope of this document.
To download project dependencies simply run the following in the project root:
npm install
You need to pass the following command line arguments:
environment: whether the target events will be sent to theliveordevelopmentendpoints. Allowed values aredevandpro.region: identifies the region of the Alexa endpoint to use to send proactive events. Allowed values areEU(Europe),NA(North America) andFE(Far East). Remember: if your users are located in NA and you are sending events trough the EU endpoint, users located in NA won't receive any notification. If you're using an Alexa Hosted Skill set the endpoint to NA.message: this is optional and lets you override the message property defined inskills.json. If you pass the message through this argument, the same message will be sent to all skills configured inskills.json.
Once you've configured your skill client_id and client_secret in skills.json, you can just run the project as:
node index.js -e <environment> -r <region> -m <message>
For example:
node index.js -e dev -r EU