From 50c0c976a250b50d1fd360fd657d0d512cdcfd06 Mon Sep 17 00:00:00 2001 From: Tim Heuer Date: Thu, 23 Mar 2017 20:04:41 -0700 Subject: [PATCH] merge audio context changes from Matthias --- Alexa.NET/Request/Context.cs | 19 ++++++++++++ Alexa.NET/Request/Device.cs | 20 +++++++++++++ Alexa.NET/Request/SkillRequest.cs | 3 ++ Alexa.NET/Request/SupportedInterfaces.cs | 12 ++++++++ Alexa.NET/Request/System.cs | 20 +++++++++++++ Alexa.NET/project.json | 2 +- README.md | 38 +++++++++++++++++++++++- 7 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 Alexa.NET/Request/Context.cs create mode 100644 Alexa.NET/Request/Device.cs create mode 100644 Alexa.NET/Request/SupportedInterfaces.cs create mode 100644 Alexa.NET/Request/System.cs diff --git a/Alexa.NET/Request/Context.cs b/Alexa.NET/Request/Context.cs new file mode 100644 index 0000000..bbac0d4 --- /dev/null +++ b/Alexa.NET/Request/Context.cs @@ -0,0 +1,19 @@ +using Alexa.NET.Request.Type; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request +{ + public class Context + { + [JsonProperty("System")] + public AlexaSystem System { get; set; } + + [JsonProperty("AudioPlayer")] + public PlaybackState AudioPlayer { get; set; } + + } +} diff --git a/Alexa.NET/Request/Device.cs b/Alexa.NET/Request/Device.cs new file mode 100644 index 0000000..4b24d55 --- /dev/null +++ b/Alexa.NET/Request/Device.cs @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request +{ + public class Device + { + [JsonProperty("supportedInterfaces")] + public Dictionary SupportedInterfaces { get; set; } + + public bool IsInterfaceSupported(string interfaceName) + { + var hasInterface = SupportedInterfaces?.ContainsKey(interfaceName); + return (hasInterface.HasValue ? hasInterface.Value : false); + } + } +} diff --git a/Alexa.NET/Request/SkillRequest.cs b/Alexa.NET/Request/SkillRequest.cs index e1b6fe0..bed96f7 100644 --- a/Alexa.NET/Request/SkillRequest.cs +++ b/Alexa.NET/Request/SkillRequest.cs @@ -11,6 +11,9 @@ public class SkillRequest [JsonProperty("session")] public Session Session { get; set; } + [JsonProperty("context")] + public Context Context { get; set; } + [JsonProperty("request")] [JsonConverter(typeof(RequestConverter))] public Type.Request Request { get; set; } diff --git a/Alexa.NET/Request/SupportedInterfaces.cs b/Alexa.NET/Request/SupportedInterfaces.cs new file mode 100644 index 0000000..e0b9c28 --- /dev/null +++ b/Alexa.NET/Request/SupportedInterfaces.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request +{ + public class SupportedInterfaces + { + + } +} diff --git a/Alexa.NET/Request/System.cs b/Alexa.NET/Request/System.cs new file mode 100644 index 0000000..1a347cf --- /dev/null +++ b/Alexa.NET/Request/System.cs @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request +{ + public class AlexaSystem + { + [JsonProperty("application")] + public Application Application { get; set; } + + [JsonProperty("user")] + public User User { get; set; } + + [JsonProperty("device")] + public Device Device { get; set; } + } +} diff --git a/Alexa.NET/project.json b/Alexa.NET/project.json index a0f557f..4586a2c 100644 --- a/Alexa.NET/project.json +++ b/Alexa.NET/project.json @@ -1,7 +1,7 @@ { "name": "Alexa.NET", "title": "Alexa.NET", - "version": "1.0.0-beta-4", + "version": "1.0.0-beta-5", "authors": [ "Tim Heuer" ], "description": "A simple .NET Core library for handling Alexa Skill request/responses.", diff --git a/README.md b/README.md index 83e5028..bc1dbb5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context) // your function logic goes here } ``` -## Get the request type (Launch, Intent, etc) +## 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. ```csharp // check what type of a request it is like an IntentRequest or a LaunchRequest @@ -26,6 +26,10 @@ 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 @@ -42,7 +46,24 @@ if (intentRequest.Intent.Name.Equals("MyIntentName")) } ``` +## 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. + +```csharp +// 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: ```csharp // build the speech response @@ -87,7 +108,22 @@ 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). + +```csharp +// 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. ```csharp