コード例 #1
0
        public string ToGoogleAssistantResponse()
        {
            var ssml            = GetSsmlResponse(Platform.GoogleAssistant);
            var webhookResponse = new WebhookResponse
            {
                Payload = new Struct
                {
                    Fields =
                    {
                        ["google"] = Value.ForStruct(new Struct
                            {
                            Fields =
                            {
                            ["expectUserResponse"] = Value.ForBool(!ShouldEndSession),
                            ["userStorage"]        = Value.ForString($"{{ \"userId\": \"{UserId}\" }}"),
                            ["resetUserStorage"]   = Value.ForBool(true)
                            }
                            })
                    }
                }
            };

            // Media response for audio
            if (AudioItemObjects.Any())
            {
                webhookResponse.Payload.Fields["google"].StructValue.Fields.Add("richResponse", Value.ForStruct(new Struct
                {
                    Fields =
                    {
                        ["items"] = Value.ForList(
                            Value.ForStruct(new Struct
                                                    {
                                                    Fields =
                                                    {
                                                    ["simpleResponse"] = Value.ForStruct(new Struct
                                                    {
                                                    Fields =
                                                    {
                                                    ["ssml"] = Value.ForString(ssml)
                                                    }
                                                    })
                                                    }
                                                    }),
                            Value.ForStruct(new Struct
                                                    {
                                                    Fields =
                                                    {
                                                    ["mediaResponse"] = Value.ForStruct(new Struct
                                                    {
                                                    Fields =
                                                    {
                                                    ["mediaType"]    = Value.ForString("AUDIO"),
                                                    ["mediaObjects"] = Value.ForList(AudioItemObjects.Select(audio => Value.ForStruct(new Struct
                                                    {
                                                    Fields =
                                                    {
                                                    ["contentUrl"]  = Value.ForString(audio.Url),
                                                    ["description"] = Value.ForString(audio.SubTitle),
                                                    ["icon"]        = Value.ForStruct(new Struct
                                                    {
                                                    Fields =
                                                    {
                                                    ["url"] = Value.ForString(audio.ImageUrl ?? "http://storage.googleapis.com/automotive-media/album_art.jpg"),
                                                    ["accessibilityText"] = Value.ForString(audio.Title)
                                                    }
                                                    }),
                                                    ["name"] = Value.ForString(audio.Title)
                                                    }
                                                    })).ToArray())
                                                    }
                                                    })
                                                    }
                                                    })),
                    }
                }));

                if (!ShouldEndSession)
                {
                    webhookResponse.Payload.Fields["google"].StructValue.Fields["richResponse"].StructValue.Fields.Add("suggestions", Value.ForList(
                                                                                                                           Value.ForStruct(new Struct
                    {
                        Fields =
                        {
                            ["title"] = Value.ForString("Stop")
                        }
                    })));
                }
            }
            else
            {
                // normal
                webhookResponse.FulfillmentText = ssml;
            }

            return(webhookResponse.ToString());
        }
コード例 #2
0
        public SkillResponse ToAlexaResponse()
        {
            if (AudioItemObjects.Any(obj => obj.TargetPlatform == Platform.All || obj.TargetPlatform == Platform.Alexa))
            {
                // AudioPlayer
                var response = new SkillResponse
                {
                    Response = new ResponseBody
                    {
                        ShouldEndSession = ShouldEndSession,
                        Directives       = new List <IDirective>()
                    },
                    Version = "1.0"
                };

                if (OutputObjects.Any(obj => obj.TargetPlatform == Platform.All || obj.TargetPlatform == Platform.Alexa))
                {
                    response.Response.OutputSpeech = new SsmlOutputSpeech
                    {
                        Ssml = GetSsmlResponse(Platform.Alexa)
                    };
                    response.Response.Reprompt = !string.IsNullOrEmpty(RepromptText)
                        ? new Alexa.NET.Response.Reprompt(RepromptText)
                        : null;
                }

                foreach (var audio in AudioItemObjects
                         .Where(audio => audio.TargetPlatform == Platform.All || audio.TargetPlatform == Platform.Alexa))
                {
                    response.Response.Directives.Add(new AudioPlayerPlayDirective
                    {
                        PlayBehavior = audio.AudioPlayBehavior == AudioPlayBehavior.Enqueue
                            ? PlayBehavior.Enqueue
                            : PlayBehavior.ReplaceAll,
                        AudioItem = new Alexa.NET.Response.Directive.AudioItem
                        {
                            Stream = new AudioItemStream
                            {
                                Url   = audio.Url,
                                Token = audio.AudioItemId,
                                ExpectedPreviousToken = audio.PreviousAudioItemId
                            },
                            Metadata = new AudioItemMetadata
                            {
                                Title    = audio.Title,
                                Subtitle = audio.SubTitle
                            }
                        }
                    });
                }
                return(response);
            }
            else if (OutputObjects.Any(obj => obj.TargetPlatform == Platform.All || obj.TargetPlatform == Platform.Alexa))
            {
                // normal
                return(new SkillResponse
                {
                    Response = new ResponseBody
                    {
                        OutputSpeech = new SsmlOutputSpeech
                        {
                            Ssml = GetSsmlResponse(Platform.Alexa)
                        },
                        Reprompt = !string.IsNullOrEmpty(RepromptText)
                            ? new Alexa.NET.Response.Reprompt(RepromptText)
                            : null,
                        ShouldEndSession = ShouldEndSession
                    },
                    Version = "1.0"
                });
            }
            else
            {
                // nothing
                return(null);
            }
        }