Agent skill
labs
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/development/labs
SKILL.md
Creating an Amazon Alexa Skill
Background
Alexa is Amazon's cloud-based voice service. Most people are familiar with the service through using an Amazon Echo or Echo dot but the service can also be integrated in to custom internet-connected devices like this talking robotic fish:
When building a skill for Alexa there are two components - the skill interface and the skill service.
The skill interface processes the speech that comes in from a device. It uses natural language processing to determine the intent of the user and then passes this as a JSON object to the skill service. It also passes JSON responses back in to voice to the user or device.
The skill service is the part that actually executes the functionality of your skill. It accepts the JSON from the skill interface, does some stuff, then returns a JSON object back to the skill interface where it can be converted to speech. For our example we are using IBM Functions as our skill service and the Amazon dev platform as our skill interface.
Creating the skill
Getting set up
Before we can create a new Alexa skill we need to create an Amazon developer account:
-
Sign up for an Amazon Developer Account here.
-
Go to https://developer.amazon.com/alexa/console/ask and click the
Create Skillbutton
- Put in the name
Business Strategy Generatorand choose custom as the model then hit thecreate skillbutton.
- Select start from scratch and click the
choosebutton.
Configuring the skill
The skill invocation name is what a user will say to an Alexa device to trigger our skill. To make this as realistic as possible, lets change ours to "senior management", so that our skill will be invoked by the phrase "Alexa, ask senior management..."
- Click on
Invocationon the left navigation and change the Skill Invocation Name tosenior managementand clickSave Model(in the top left corner).
When you start to build more complex Alexa skills, you need to define the intents that your user can utter. An intent is essentially a general action a user can perform e.g. "what is the forecast?" or "get my balance!". Since our skill doesn't handle multiple actions we'll just create one intent that will handle all requests.
- On the left side-bar click on
Slot Typesand hit+ Add:
- Use the name
BAG_OF_WORDSand hit theCreate custom slot typebutton:
- Now
BAG_OF_WORDSneeds a slot value. Just enterHello Worldand hit the plus sign so that it has a slot value:
- On the left side-bar click on
Intentsand click+ Add:
-
Use the name
EveryThingIntentand hit theCreate custom intentbutton. -
Enter the text
{EveryThingSlot}in to the text field under "Sample Utterances (0)" and click the plus sign to create the slot:
- Scroll down to where it says
Intent Slotsand use theSelect a slot typedropdown to select the value BAG_OF_WORDS for the EveryThingSlot:
- Click on
Save Modeland thenBuild Modelin the top left corner of the console:
Updating your serverless action
Your Alexa skill is almost ready to use but right now it won't work even if we integrate it with our serverless action. The Alexa skill interface expects a JSON payload to be returned in a certain format for it to work. To do that, we'll need to modify our existing generateStrategy action:
- Make a copy of your generator.js file and name it
alexa.js
$ cp generator.js alexa.js
- Open the new
alexa.jsfile and modify the followingreturnstatement inside yourmain()function:
Previously
return {
"text": toTitleCase(statement)
}
Now
return {
"version": "1.0",
"response": {
"shouldEndSession": true,
"outputSpeech": {
"type": "PlainText",
"text": toTitleCase(statement)
}
}
}
The functionality of our action hasn't changed but we're now returning our response in a format that will be accepted by Alexa. One important parameter is shouldEndSession: true which tells Alexa that we are finished conversing with the user.
- Save your
alexa.jsfile.
By default when you create actions on IBM Functions they are private. To allow our action to be called from the Alexa skill interface we need to make it public.
- Create a new action called
alexaGenerateStrategyand enable it for the web:
$ ibmcloud wsk action create alexaGenerateStrategy alexa.js --web true
ok: created action alexaGenerateStrategy
Using the --web flag with a value of true or yes allows an action to be accessible through a REST interface without the need for credentials. If you're interested in learning how to configure a web action with credentials see the Securing web actions docs. A web action can be invoked by using a URL that is structured as follows: https://{APIHOST}/api/v1/web/{namespace}/{packageName}/{actionName}.{EXT}.
The package name is default if the action is not in a named package.
- Get your action public url:
$ ibmcloud wsk action get alexaGenerateStrategy --url
ok: got action alexaGenerateStrategy
https://eu-gb.functions.cloud.ibm.com/api/v1/web/edmundshee%40uk.ibm.com_dev/default/alexaGenerateStrategy
$ curl https://eu-gb.functions.cloud.ibm.com/api/v1/web/edmundshee%40uk.ibm.com_dev/default/alexaGenerateStrategy.json
{
"response": {
"outputSpeech": {
"text": "We Are On A Journey To Objectively Seize Effective Core Competencies",
"type": "PlainText"
},
"shouldEndSession": true
},
"version": "1.0"
Note: if you add .json to the end of your url the platform will return a JSON object
🎉🎉🎉 Awesome work, you now have a web-enabled action that can be called from anywhere. Why not move on to integrate your action and finalise your Alexa skill... 🎉🎉🎉
Next Lab:
Didn't find tool you were looking for?