Git Product home page Git Product logo

alexa-skills-dotnet's Introduction

Alexa Skills SDK for .NET

Build status

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.

Some Quick Samples

Here are some simple examples of how to use this library assuming the default signature of the AWS Lambda C# function:

public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
    // your function logic goes here
}

Get the request type (Launch, Intent, Audio, etc)

You most likely are going to want to get the type of request to know if it was the default launch, an intent, or maybe an audio request.

// check what type of a request it is like an IntentRequest or a LaunchRequest
var requestType = input.GetRequestType();

if (requestType == typeof(IntentRequest))
{
    // do some intent-based stuff
}
else if (requestType == typeof(Alexa.NET.Request.Type.LaunchRequest))
{
    // default launch path executed
}
else if (requestType == typeof(AudioPlayerRequest))
{
    // do some audio response stuff
}

Get the intent and look at specifics

Once you know it is an IntentRequest you probably want to know which one (name) and perhaps pull out parameters (slots):

// do some intent-based stuff
var intentRequest = input.Request as IntentRequest;

// check the name to determine what you should do
if (intentRequest.Intent.Name.Equals("MyIntentName"))
{
    // get the slots
    var firstValue = intentRequest.Intent.Slots["FirstSlot"].Value;
}

Get an audio request and determine the action

Once you know it is an AudioPlayerRequest, you have to determine which one (playback started, finished, stopped, failed) and respond accordingly.

// do some audio response stuff
var audioRequest = input.Request as AudioPlayerRequest;

// these are events sent when the audio state has changed on the device
// determine what exactly happened
if (audioRequest.AudioRequestType == AudioRequestType.PlaybackNearlyFinished)
{
    // queue up another audio file
}

Build a simple voice response

There are various types of responses you can build and this library provides a helper function to build them up. A simple one of having Alexa tell the user something using SSML may look like this:

// build the speech response 
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "<speak>Today is <say-as interpret-as=\"date\">????0922</say-as>.<break strength=\"x-strong\"/>I hope you have a good day.</speak>";

// create the response using the ResponseBuilder
var finalResponse = ResponseBuilder.Tell(speech);
return finalResponse;

Build a simple response with a Card

In your response you can also have a 'Card' response, which presents UI to the Alexa companion app for the registered user. Cards presently are simple and contain basically titles and plain text (no HTML :-(). To create a response with cards, you can use the ResponseBuilder:

// create the speech response - cards still need a voice response
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "<speak>Today is <say-as interpret-as=\"date\">????0922</say-as>.</speak>";

// create the card response
var finalResponse = ResponseBuilder.TellWithCard(speech, "Your Card Title", "Your card content text goes here, no HTML formatting honored");
return finalResponse;

Build a simple response with a reprompt

If you want to reprompt the user, use the Ask helpers

// create the speech response
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "<speak>Today is <say-as interpret-as=\"date\">????0922</say-as>.</speak>";

// create the speech reprompt
var repromptMessage = new Alexa.NET.Response.PlainTextOutputSpeech();
repromptMessage.Text = "Would you like to know what tomorrow is?";

// create the reprompt
var repromptBody = new Alexa.NET.Response.Reprompt();
repromptBody.OutputSpeech = repromptMessage;

// create the response
var finalResponse = ResponseBuilder.Ask(speech, repromptBody);
return finalResponse;

Play an audio file

If your skill is registered as an audio player, you can send directives (instructions to play, enqueue, or stop an audio stream).

// create the speech response - you most likely will still have this
string audioUrl = "http://mydomain.com/myaudiofile.mp3";
string audioToken = "a token to describe the audio file"; 

var audioResponse = ResponseBuilder.AudioPlayerPlay(PlayBehavior.ReplaceAll, audioUrl, audioToken);

return audioResponse

Build a response without using helpers

If you do not want to use the helper Tell/Ask functions for the simple structure you can build up the response manually using the Response and some IOutputSpeech objects.

// create the speech response - you most likely will still have this
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "<speak>Today is <say-as interpret-as=\"date\">????0922</say-as>.</speak>";

// create the response
var responseBody = new Alexa.NET.Response.ResponseBody();
responseBody.OutputSpeech = speech;
responseBody.ShouldEndSession = true;

var skillResponse = new Alexa.NET.Response.SkillResponse();
skillResponse.Response = responseBody;
skillResponse.Version = "1.0";

return skillResponse;

Build a reprompt without using helpers

To add reprompt to the response you just need to include that as well.

// create the speech response - you most likely will still have this
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "<speak>Today is <say-as interpret-as=\"date\">????0922</say-as>.</speak>";

// create the reprompt speech
var repromptMessage = new Alexa.NET.Response.PlainTextOutputSpeech();
repromptMessage.Text = "Would you like to know what tomorrow is?";

// create the reprompt object
var repromptBody = new Alexa.NET.Response.Reprompt();
repromptBody.OutputSpeech = repromptMessage;

// create the response
var responseBody = new Alexa.NET.Response.ResponseBody();
responseBody.OutputSpeech = speech;
responseBody.ShouldEndSession = false; // this triggers the reprompt
responseBody.Reprompt = repromptBody;

var skillResponse = new Alexa.NET.Response.SkillResponse();
skillResponse.Response = responseBody;
skillResponse.Version = "1.0";

return skillResponse;

alexa-skills-dotnet's People

Contributors

adriangodong avatar fuzzysb avatar matthiasxc avatar stenmannsar avatar stoiveyp avatar techpreacher avatar timheuer avatar tobiasviehweger avatar vinaykapadia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.