Alexa Skill With .NET Core – DZone Web Dev | xxxAlexa Skill With .NET Core – DZone Web Dev – xxx
菜单

Alexa Skill With .NET Core – DZone Web Dev

二月 29, 2020 - MorningStar

Over a million developers have joined DZone.

  • Alexa Skill With .NET Core - DZone Web Dev

    {{node.title}}

    {{node.type}} · {{ node.urlSource.name }} by

    Download {{node.downloads}}

  • {{totalResults}} search results

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0px;”>
1 && !articles[0].partner.isSponsoringArticle && (width > 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

Alexa Skill With .NET Core

DZone ‘s Guide to

Alexa Skill With .NET Core

In this article, we discuss how to program an Alexa Skill with Alexa Lambda functions and a REST API in C# and .NET Core.

Apr. 08, 20 · Web Dev Zone ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

Alexa Skills can be developed using Alexa Lambda functions or a REST API endpoint. Lambda functions are Amazon’s implementation of serverless functions available in AWS. Amazon recommends using Lambda functions even though they are not easy to debug. While you can log to a CloudWatch log, you can’t hit a breakpoint and step into the code.

Alexa Skill With .NET Core

This makes live debugging of Alexa requests a challenge. This post explains a simple but useful solution: it is to wrap code in a .NET Standard class library and stand up a REST API project for debugging and development and a Lambda function project for AWS deployment. This article shows how to create an environment to debug a locally-hosted Web API that uses the same logic that is used by a Lambda function. Everything’s written in C#.

Structuring the Solution

This approach requires a minimum of two projects in the solution:

  1. Lambda function project (.NET Core 2.1).
  2. Web API project (.NET Core 2.1).

Prerequisites

Here you have the technologies used in this project:

  1. .NET Core 2.1.
  2. Visual Studio Community 2019.
  3. Nugget Package Manager.
  4. Alexa .NET (Version 1.13.0).
  5. ngrok.

This article assumes familiarity with creating Lambda functions. The AWS Toolkit for Visual Studio includes Lambda function projects and can be used to create .NET Core Lambda functions. All meaningful logic should be contained in the BusinessLogic folder and referenced by both the Lambda function project and the Web API project. Both the Lambda project and the Web API project should be thin wrappers for the business logic.

Install Amazon.Lambda.Tools Global Tools if not already installed.

Shell

 

x
 

1

dotnet tool install -g Amazon.Lambda.Tools

If already installed check if a new version is available.

Shell

 

xxxxxxxxxx
1

 

1

dotnet tool update -g Amazon.Lambda.Tools

Project Files

These are the main files of the project:

Alexa Skill With .NET Core - DZone Web Dev

  • serverless.template — an AWS CloudFormation Serverless Application Model template file for declaring your serverless functions and other AWS resources.
  • aws-lambda-tools-defaults.json — default argument settings for use with Visual Studio and command line deployment tools for AWS.
  • Function.cs — class that derives from Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction. The code in this file bootstraps the ASP.NET Core hosting framework. The Lambda function is defined in the base class.
  • LocalEntryPoint.cs — for local development this contains the executable Main function which bootstraps the ASP.NET Core hosting framework with Kestrel, as for typical ASP.NET Core applications.
  • Startup.cs — usual ASP.NET Core Startup class used to configure the services ASP.NET Core will use.
  • web.config — used for local development.
  • Controllers/AlexaController.cs — Alexa API controller that will receive all the requests from the cloud. This controller allows us to debug our Skill locally.
  • BsuinessLogic/AlexaProcessor.cs — The code that will process all the Alexa requests from the Lambda Function in Function.cs and from the POST controller in AlexaController.cs.

Once the structure has been explained, it is time to understand how it works in the entire project:

Alexa Skill With .NET Core - DZone Web Dev

Alexa Request Processor

Alexa.NET is a helper library for working with Alexa Skill requests/responses in C#. Whether you are using the AWS Lambda service or hosting your own service on your server, this library aims just to make working with the Alexa API more natural for a C# developer using a strongly-typed object model.

You can find all the documentation in their official GitHub repository.

Below, you have the class that will manage all the Alexa requests using Alexa.NET Nugget package showing how easy is to develop these kinds of voice apps in .NET:

C#

 

xxxxxxxxxx
1

51

 

1

    public class AlexaRequestProcessor

2

    {

3

        public SkillResponse Process(SkillRequest input)

4

        {

5

 

6

            Session session = input.Session;

7

xxxxxxxxxx

0

8

xxxxxxxxxx

1

9

xxxxxxxxxx

2

10

xxxxxxxxxx

3

11

xxxxxxxxxx

4

12

xxxxxxxxxx

5

13

xxxxxxxxxx

6

14

xxxxxxxxxx

7

15

xxxxxxxxxx

8

16

xxxxxxxxxx

9

17

dotnet tool update -g Amazon.Lambda.Tools

0

18

dotnet tool update -g Amazon.Lambda.Tools

1

19

dotnet tool update -g Amazon.Lambda.Tools

2

20

dotnet tool update -g Amazon.Lambda.Tools

3

21

dotnet tool update -g Amazon.Lambda.Tools

4

22

dotnet tool update -g Amazon.Lambda.Tools

5

23

dotnet tool update -g Amazon.Lambda.Tools

6

24

dotnet tool update -g Amazon.Lambda.Tools

7

25

dotnet tool update -g Amazon.Lambda.Tools

8

26

dotnet tool update -g Amazon.Lambda.Tools

9

27

xxxxxxxxxx

0

28

xxxxxxxxxx

1

29

xxxxxxxxxx

2

30

xxxxxxxxxx

3

31

xxxxxxxxxx

4

32

xxxxxxxxxx

5

33

xxxxxxxxxx

6

34

xxxxxxxxxx

7

35

xxxxxxxxxx

8

36

xxxxxxxxxx

9

37

    public class AlexaRequestProcessor

0

38

    public class AlexaRequestProcessor

1

39

    public class AlexaRequestProcessor

2

40

    public class AlexaRequestProcessor

3

41

    public class AlexaRequestProcessor

4

42

    public class AlexaRequestProcessor

5

43

    public class AlexaRequestProcessor

6

44

    public class AlexaRequestProcessor

7

45

    public class AlexaRequestProcessor

8

46

    public class AlexaRequestProcessor

9

47

    {

0

48

    {

1

49

    {

2

50

    {

3

51

    {

4

Build the Skill With Visual Studio

Visual Studio offers a lot of built-in functionality. If you want to build the whole solution within the IDE, you can do it interactively:

Alexa Skill With .NET Core - DZone Web Dev

Run the Skill With Visual Studio

Run the Alexa Skill is as easy as click on the play button in Visual Studio. Using the configuration alexa_dotnet_lambda_helloworld:

Alexa Skill With .NET Core - DZone Web Dev

After executing it, you can send Alexa POST requests to http://localhost:5000/api/alexa.

This execution will run the web API project. If you want to run the Lambda function, you have to run the configuration Mock Lambda Test Tool:

Alexa Skill With .NET Core - DZone Web Dev

This execution will run a browser with the Mock Lambda Test Tool, and from there, you can execute all requests directly towards your Function.cs:

Alexa Skill With .NET Core - DZone Web Dev

Debug the Skill With Visual Studio

Following the steps before, now you can set up breakpoints wherever you want inside all C# files in order to debug your Skill:

Alexa Skill With .NET Core - DZone Web Dev

Test Requests Locally

I’m sure you already know the famous tool, Postman. REST APIs have become the new standard in providing a public and secure interface for your service. Though REST has become ubiquitous, it’s not always easy to test. Postman makes it easier to test and manage HTTP REST APIs. Postman gives us multiple features to import, test, and share APIs, which will help you and your team be more productive in the long run.

After running your application, you will have an endpoint available at http://localhost:5000/api/alexa. With Postman, you can emulate any Alexa Request.

For example, you can test a LaunchRequest:

JSON

 

    {

5

38

 

1

    {

6

2

    {

7

3

    {

8

4

    {

9

5

        public SkillResponse Process(SkillRequest input)

0

6

        public SkillResponse Process(SkillRequest input)

1

7

        public SkillResponse Process(SkillRequest input)

2

8

        public SkillResponse Process(SkillRequest input)

3

9

        public SkillResponse Process(SkillRequest input)

4

10

        public SkillResponse Process(SkillRequest input)

5

11

        public SkillResponse Process(SkillRequest input)

6

12

        public SkillResponse Process(SkillRequest input)

7

13

        public SkillResponse Process(SkillRequest input)

8

14

        public SkillResponse Process(SkillRequest input)

9

15

        {

0

16

        {

1

17

        {

2

18

        {

3

19

        {

4

20

        {

5

21

        {

6

22

        {

7

23

        {

8

24

        {

9

25

 

0

26

 

1

27

 

2

28

 

3

29

 

4

30

 

5

31

 

6

32

 

7

33

 

8

34

 

9

35

            Session session = input.Session;

0

36

            Session session = input.Session;

1

37

            Session session = input.Session;

2

38

            Session session = input.Session;

3

You can execute unit tests as well

Shell

 

            Session session = input.Session;

4

1

 

1

            Session session = input.Session;

5

2

            Session session = input.Session;

6

Deploy Your Alexa Skill

You can do it directly from Visual Studio or from your CLI:

Here Are Some Steps to Follow From Visual Studio:

To deploy your Serverless application, right-click the project in Solution Explorer and select Publish to AWS Lambda.

To view your deployed application, open the Stack View window by double-clicking the stack name shown beneath the AWS CloudFormation node in the AWS Explorer tree. The Stack View also displays the root URL to your published application.

Alexa Skill With .NET Core - DZone Web Dev

Getting Started From the Command Line

Once you have edited your template and code, you can deploy your application using the Amazon.Lambda.Tools Global Tool from the command line.

Deploy application:

Shell

 

            Session session = input.Session;

7

1

 

1

            Session session = input.Session;

8

2

            Session session = input.Session;

9

Test Requests Directly From Alexa

Ngrok is a very cool, lightweight tool that creates a secure tunnel on your local machine, along with a public URL you can use for browsing your local site or APIs.

When ngrok is running, it listens on the same port that your local web server is running on and proxies external requests to your local machine

From there, it’s a simple step to get it to listen to your web server. Let’s say you’re running your local web server on port 5000. In the terminal, you’d type: ngrok http 5000. This starts ngrok listening on port 5000 and creates the secure tunnel:

Alexa Skill With .NET Core - DZone Web Dev

Now, you have to go to Alexa Developer console, go to your skill > endpoints > https, add the HTTPS URL generated above followed by /api/alexa. Eg: https://fe8ee91c.ngrok.io/api/alexa.

Select the My development endpoint as a sub-domain…. option from the dropdown and click save the endpoint at the top of the page.

Go to the Test tab in the Alexa Developer Console and launch your Skill.

The Alexa Developer Console will send an HTTPS request to the ngrok endpoint,(https://fe8ee91c.ngrok.io/api/alexa), which will route it to your Skill running on the Web API server at http://localhost:5000/api/alexa.

Conclusion

This example can be useful for all those developers who do not want to write their code in Java, NodeJS, or Pyhton. These are the official languages supported by Alexa. We have to take into account that Alexa.NET nugget package is not an Official Alexa Skill Kit (ASK). 

 I recommend you to access its official GitHub repository to check the status of the project, bugs, updates, etc. As you have seen in this example, community developers give you the possibility to create Skills in different ways. I hope this example project is useful to you.

That’s all, folks! You can find all the code in my GitHub.

I hope it will be useful! If you have any doubts or questions, do not hesitate to contact me or put a comment below!

Happy coding!

Topics:
alexa ,alexa skill ,alexa skill development ,alexa skills ,alexa skills developer ,alexa skills development ,dotnet ,.net ,visual studio ,.net core

Opinions expressed by DZone contributors are their own.

Web Dev Partner Resources

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

by

CORE

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}



Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51