Creating An Alexa Bot Using The Microsoft Bot Framework
If you desire to add support for Amazon Alexa, you will have to use custom code. The open source project, Bot Builder Community - .NET Extensions has recently added an Alexa Adapter middleware that makes integration easy. They even created a Sample to get you started.
To complete this tutorial you will need the following:
- An Microsoft Azure Account (If you don’t have one, go to https://www.azure.com and click the green Start free button)
- An Amazon Developer Account (Go to: https://developer.amazon.com/alexa and Click Sign-in to log in or to create an account)
- Visual Studio 2017 (or higher)
Get The Sample Code
The sample code is located at: https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/master/samples/Alexa%20Adapter%20Sample.
However, to get to it, go to: https://github.com/BotBuilderCommunity/botbuilder-community-dotnet and download it as a .zip file.
Unzip the code.
Navigate to ../botbuilder-community-dotnet-master\botbuilder-community-dotnet-master\samples\Alexa Adapter Sample and open the Alexa Adapter Sample.csproj file in Visual Studio.
The project will open in Visual Studio.
Rebuild the Solution.
The code will currently repeat whatever is sent to it. After you get it working, you would come back and alter this code.
- You can learn how to create bots with the Microsoft Bot Framework starting with this tutorial: Creating a Hello World! Bot (MS Bot Framework V4)
- The documentation on how to use the code that is specific to working with Amazon Alexa is here: https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/master/libraries/Bot.Builder.Community.Adapters.Alexa
Deploy To Azure
Right-click on the project node and select Publish.
Follow the directions in the article:
to publish the bot and Connect The Bot To The Azure Bot Service.
You will now have an App Service and a Bot Channels Registration.
Note the URL of your App Service.
You will need to point the Alexa Skill (to be created in later steps), to the URL with /api/skillrequests added to it.
Create The Alexa Skill
Note: All the steps we will now do are also covered on the GitHub page for the sample code at: https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/master/samples/Alexa%20Adapter%20Sample .
Go to: https://developer.amazon.com/alexa and click Sign In (Note: If you need to, you can also create a new account by clicking the Sign In link).
Note: If you want to test this on an actual Amazon Alexa device, you must log in using the account that the Alexa device is registered under.
Next, go to: https://developer.amazon.com/alexa/console/ask and click Create Skill.
Give the skill an original two word name, select Custom for the model, and click Create skill.
For template, choose Start from scratch.
Click the Choose button.
Choose JSON Editor.
Open another web browser, and go to: https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/master/samples/Alexa%20Adapter%20Sample and copy the JSON example on that page.
Return to the web browser that has the Amazon Alexa portal opened and…
- Paste the JSON into the box
- Change the invocationName to the name of your Alexa skill
- Click Save Model
- Click Build Model
The Model will build.
When the build is complete:
- Click Endpoint.
- Select HTTPS to indicate we will host the code on our own server (on Microsoft Azure)
- Enter the address of the Azure App Service URL with /api/skillrequests added to it (for example: https://alexaadaptersample.azurewebsites.net/api/skillrequests ).
- In the dropdown select: My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority (this is the second option in the dropdown).
At the top of the page, click Save Endpoints.
- Click Invocation
- Click Save Model
- Click Build Model
Wait until the build is complete.
Test It Out
Click the Test tab.
Click the slide to enable testing for the skill.
Now you can enter a message in the box and press enter.
You will see the response in the chat window.
If your skill is called adapter sample and the invocation name was also set as adapter sample , you would enter:
alexa launch adapter sample
and press Enter.
Alexa will indicate, in the chat window, that the skill has been launched.
You can then enter:
alexa ask adapter sample hello from AiHelpWebsite
and press Enter.
The Microsoft Bot Framework code, running in Microsoft Azure, will repeat back whatever you send it.
You can now interact with the Alexa Skill with any Alexa device that is registered to the developer account the Alexa Skill is created under.
To distribute the Alexa Skill to others, click the Distribution link in the Alexa developer console and follow the directions there.
An Alexa Skill needs a response within 8 seconds, so if your web app (on Azure) has gone to sleep, the first request may fail while it wakes up. To prevent this, enable always on .
Also, ensure you are just invoking the skill in a way that Alexa will recognize. For example, instead of saying "Alexa invoke ”, use "Alexa launch ". You can also use a phase such as "Alexa tell adapter sample hello world" to call it.
The Man Who Made This Happen
I’m just documenting the process. Gary Pretty created the code and literally had to jump into my Amazon Developer account to fix my configuration issues.
You can connect with him through Twitter: