dcsimg
October 30, 2020
Hot Topics:

Amazon Skill Tools for .NET, Part 3: Creating a Basic Skill in .NET

  • By Hannes DuPreez
  • Send Email »
  • More Articles »

Introduction

There was a lot of set up work done in Parts 1, "Creating Amazon Skills with .NET, Part 1: First, Getting the Basics Right" and 2, "Creating Amazon Skills with .NET, Part 2: Getting Tools and Setting Up." Now, in Part 3 of the "Creating Amazon Skills with .NET" series, I will show you how to create a basic Amazon Skill using C# and all the tools you have installed previously.

Starting a Project

Launch Visual Studio and create a new AWS Lambda Project, as shown in Figure 1.

Project Type
Figure 1: Project Type

After you have specified a name and location for the project, another screen launches. This screen lets you specify the type of Blueprint you'd like to use in your skill. This is shown in Figure 2.

Select Blueprint
Figure 2: Select Blueprint

After you have selected your desired option, your Visual Studio Solution Explorer may look like Figure 3.

Solution Explorer
Figure 3: Solution Explorer

Function.cs is where you will spend most of your time coding. The provided class looks similar to the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input
// to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization
   .Json.JsonSerializer))]

namespace AWS_Demo
{
   public class Function
   {

      /// <summary>
      /// A simple function that takes a string and does a ToUpper
      /// </summary>
      /// <param name="input"></param>
      /// <param name="context"></param>
      /// <returns></returns>
      public string FunctionHandler(string input,
         ILambdaContext context)
      {
         return input?.ToUpper();
      }
   }
}

You now can insert your NuGet package you have created in the previous parts or insert another. Let's click the Project menu and select manage NuGet packages. In the Browse section, search for Slight.Alexa (an open source project), and select it from the list as shown in Figure 4. Then, Install it.

NuGet packages
Figure 4: NuGet packages

Add the namespaces for the Slight.Amazon functionalities.

using Slight.Alexa.Framework.Models.Requests;
using Slight.Alexa.Framework.Models.Responses;

Edit the signature of your FunctionHandler to the following:

public SkillResponse FunctionHandler(SkillRequest input,
   ILambdaContext context)

Now, we can handle requests.

Before we continue, add a JSON file to the project and name it intentSchema.json. Edit it to look like the following:

{
   "intents": [
      {
         "intent": "AddIntent",
         "slots": [
            {
               "name": "alphabet",
               "type": "AMAZON.SearchQuery"
            }
         ]
      }
   ]
}

For a full list of Amazon Slot types, have a look here. I have created a slot named alphabet here. You will make use of it in the next code. Edit your FunctionHandler to look as follows:

      public SkillResponse FunctionHandler(SkillRequest input,
         ILambdaContext context)
      {
         Response resp;
         IOutputSpeech speechOut = null;
         var log = context.Logger;

         if (input.GetRequestType() == typeof(Slight.Alexa
            .Framework.Models.Requests.RequestTypes.ILaunchRequest))
         {
            log.LogLine($"Hello");

            speechOut = new PlainTextOutputSpeech();
            (speechOut as PlainTextOutputSpeech).Text =
               "Ask me the alphabet! Start with 'A'";
         }
         else if (input.GetRequestType() == typeof(Slight.Alexa
            .Framework.Models.Requests.RequestTypes.IIntentRequest))
         {
            log.LogLine($"Requested {input.Request.Intent.Name}");

            speechOut = new PlainTextOutputSpeech();

            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"A"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"B"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"C"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"D"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"E"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"F"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"G"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"H"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"I"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"J"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"K"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"L"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"M"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"N"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"O"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"P"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"Q"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"R"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"S"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"T"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"U"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"V"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"W"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"X"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"Y"}.";
            (speechOut as PlainTextOutputSpeech).Text = $"Here is
               the Alphabet! {"Z"}.";


         }

         resp = new Response();
         resp.ShouldEndSession = true;
         resp.OutputSpeech = speechOut;

         SkillResponse skillResp = new SkillResponse();
         skillResp.Response = resp;
         skillResp.Version = "1.0";

         return skillResp;
      }

Here, you create a Response and a Request object. You ask Alexa to give you the alphabet, when you start with 'A'.

Add a file named Utterances.txt and edit it with the following:

AddIntent what is the rest of the alphabet {alphabet}

Now, when you ask the above question, and include 'A', it should respond with the rest of the alphabet.

It is not perfect or very high-level, but it should work.

Conclusion

Now that you know how to create a basic Amazon Skill with .NET, you can go on and explore building them further. In the next installment, I will talk about testing AWS skills.






This article was originally published on January 24, 2020

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date