Hi!
Well, in today’s post we will see how to create an Azure API App, which we will then use as an Alexa Skill backend that we created in the previous post.
Tommy describes in an excellent way how to create a website and prepare him to work with Alexa Skill in this post (link). This project uses the NuGet Package Alexa Skills Kit.
Net for the processing of messages with Amazon Alexa.
I have created a template for Visual Studio 2017 that can be downloaded from here (link) and that allows us to create a project already configured to work with Alexa Skills.
Note: the quick way to import this template is to copy the file [WebApi Alexa Skill.
zip] to the Visual Studio 2017 project directory. Usually in [% \ Documents \ Visual Studio 2017 \ Templates \ ProjectTemplates \ Visual C #]
We create a new project using the imported type.
We compiled the project to refresh the NuGet references, update the NuGet packages and we can start working.
The 2 main elements in the project are
- Controllers / AlexaController.cs. This is an standard ApiController wchi will handle the Alexa requests. It uses 2 HTTP requests
- HTTP GET mostly for testing
- HTTP POST process Alexa requests with the class [AlexaResponseAsync]
- Alexa / AlexaResponseAsync.cs. All the business logic is here.
So, in the Alexa Response class we will add some basic changes
- Línea 7, change the message used on the skill activation event
- Líneas 10 a 22, creates a message including the Intent name and the used slots.
Something similar to this
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
namespace AlexaSkillBrunoEvents.Alexa | |
{ | |
public class AlexaSkillBrunoEvents : SpeechletAsync | |
{ | |
public override Task<SpeechletResponse> OnLaunchAsync(LaunchRequest launchRequest, Session session) | |
{ | |
return Task.FromResult(CompileResponse("Launch Event for Bruno Events")); | |
} | |
public async override Task<SpeechletResponse> OnIntentAsync(IntentRequest intentRequest, Session session) | |
{ | |
var message = new StringBuilder(); | |
message.Append($"Intent called {intentRequest.Intent}."); | |
if (intentRequest.Intent.Slots.Count > 0) | |
{ | |
message.Append($" Slots used. "); | |
foreach (var intentSlot in intentRequest.Intent.Slots) | |
message.Append($"{intentSlot.Key} "); | |
} | |
var response = CompileResponse(message.ToString()); | |
return await Task.FromResult(response); | |
} | |
public override Task OnSessionStartedAsync(SessionStartedRequest sessionStartedRequest, Session session) | |
{ | |
return Task.FromResult(0); | |
} | |
public override Task OnSessionEndedAsync(SessionEndedRequest sessionEndedRequest, Session session) | |
{ | |
return Task.FromResult(0); | |
} | |
public static SpeechletResponse CompileResponse(string output) | |
{ | |
var response = new SpeechletResponse | |
{ | |
OutputSpeech = new PlainTextOutputSpeech { Text = output }, | |
ShouldEndSession = true | |
}; | |
return response; | |
} | |
} | |
} |
So far the creation of the web project. In the next post, we will see how to publish this website and connect it with the Alexa Skill.
Happy Coding!
Greetings @ Toronto
El Bruno
References
Could you make it a little simpler? I’m trying to understand each individual step that happens here
LikeLike
Have you downloaded the code?
Take a look at the final solution and it will make sense!
Regards
-Bruno
LikeLike
Hi. This is for Azure. Would the code be same if I plan to host it on a private server (IIS)?
LikeLike
You can’t do this in a private server. The Alexa skill needs to contact a public url, so you can use azure or any other public vloud services for this.
LikeLike
Hi Bruno. Thanks for reply. That’s what I meant – publicly visible from internet but not Azure or any other cloud. Just a hosting with a domain and IP address…. so, would your code work on it?
LikeLiked by 1 person
Hey MK, yes that will work!
If the AWS Alexa Skill can manage to contact your public IP with the code hosted there, the skill will work.
Let me know how this works 😀
Regards
-Bruno
LikeLike