Ejemplo n.º 1
0
        private static void CreateAlexaCardFromAttachment(Activity activity, AlexaResponseBody response)
        {
            var attachment = activity.Attachments != null && activity.Attachments.Any()
                ? activity.Attachments[0]
                : null;

            if (attachment != null)
            {
                switch (attachment.ContentType)
                {
                case HeroCard.ContentType:
                case ThumbnailCard.ContentType:
                    if (attachment.Content is HeroCard)
                    {
                        response.Response.Card = CreateAlexaCardFromHeroCard(attachment);
                    }

                    break;

                case SigninCard.ContentType:
                    response.Response.Card = new AlexaCard()
                    {
                        Type = AlexaCardType.LinkAccount
                    };
                    break;
                }
            }
        }
 private void AddCardToResponse(ITurnContext context, AlexaResponseBody response, Activity activity)
 {
     if (context.TurnState.ContainsKey("AlexaCard") && context.TurnState["AlexaCard"] is AlexaCard)
     {
         response.Response.Card = context.TurnState.Get <AlexaCard>("AlexaCard");
     }
     else if (Options.TryConvertFirstActivityAttachmentToAlexaCard)
     {
         CreateAlexaCardFromAttachment(activity, response);
     }
 }
        public async Task <AlexaResponseBody> ProcessActivity(AlexaRequestBody alexaRequest, AlexaOptions alexaOptions, BotCallbackHandler callback)
        {
            TurnContext context = null;

            try
            {
                Options = alexaOptions;

                var activity = RequestToActivity(alexaRequest);
                BotAssert.ActivityNotNull(activity);

                context = new TurnContext(this, activity);

                if (alexaRequest.Session.Attributes != null && alexaRequest.Session.Attributes.Any())
                {
                    context.TurnState.Add("AlexaSessionAttributes", alexaRequest.Session.Attributes);
                }
                else
                {
                    context.TurnState.Add("AlexaSessionAttributes", new Dictionary <string, string>());
                }

                context.TurnState.Add("AlexaResponseDirectives", new List <IAlexaDirective>());

                Responses = new Dictionary <string, List <Activity> >();

                await base.RunPipelineAsync(context, callback, default(CancellationToken)).ConfigureAwait(false);

                var key = $"{activity.Conversation.Id}:{activity.Id}";

                try
                {
                    AlexaResponseBody response = null;
                    var activities             = Responses.ContainsKey(key) ? Responses[key] : new List <Activity>();
                    response = CreateResponseFromLastActivity(activities, context);
                    response.SessionAttributes = context.AlexaSessionAttributes();
                    return(response);
                }
                finally
                {
                    if (Responses.ContainsKey(key))
                    {
                        Responses.Remove(key);
                    }
                }
            }
            catch (Exception ex)
            {
                await alexaOptions.OnTurnError(context, ex);

                throw;
            }
        }
Ejemplo n.º 4
0
 private void AddCardToResponse(ITurnContext context, AlexaResponseBody response, Activity activity)
 {
     if (activity.Attachments != null &&
         activity.Attachments.Any(a => a.ContentType == SigninCard.ContentType))
     {
         response.Response.Card = new AlexaCard()
         {
             Type = AlexaCardType.LinkAccount
         };
     }
     else
     {
         if (context.TurnState.ContainsKey("AlexaCard") && context.TurnState["AlexaCard"] is AlexaCard)
         {
             response.Response.Card = context.TurnState.Get <AlexaCard>("AlexaCard");
         }
         else if (ConvertBotBuilderCardsToAlexaCards)
         {
             CreateAlexaCardFromAttachment(activity, response);
         }
     }
 }
Ejemplo n.º 5
0
 private static void AddDirectivesToResponse(ITurnContext context, AlexaResponseBody response)
 {
     response.Response.Directives = context.AlexaResponseDirectives().Select(a => a).ToArray();
 }
Ejemplo n.º 6
0
        private AlexaResponseBody CreateResponseFromLastActivity(IEnumerable <Activity> activities, ITurnContext context)
        {
            var response = new AlexaResponseBody()
            {
                Version  = "1.0",
                Response = new AlexaResponse()
                {
                    ShouldEndSession = context.GetAlexaRequestBody().Request.Type ==
                                       AlexaRequestTypes.SessionEndedRequest ||
                                       ShouldEndSessionByDefault
                }
            };

            if (context.GetAlexaRequestBody().Request.Type == AlexaRequestTypes.SessionEndedRequest &&
                (activities == null || !activities.Any()))
            {
                response.Response.OutputSpeech = new AlexaOutputSpeech()
                {
                    Type = AlexaOutputSpeechType.PlainText,
                    Text = string.Empty
                };
                return(response);
            }

            var activity = activities.First();

            // https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/25
            // https://stackoverflow.com/questions/53019696/special-characters-not-supported-by-aws-polly/53020501#53020501
            // Fixed the above issues
            if (!SecurityElement.IsValidText(activity.Text))
            {
                activity.Text = SecurityElement.Escape(activity.Text);
            }

            if (activity.Type == ActivityTypes.EndOfConversation)
            {
                response.Response.ShouldEndSession = true;
            }

            if (!string.IsNullOrEmpty(activity.Speak))
            {
                response.Response.OutputSpeech = new AlexaOutputSpeech()
                {
                    Type = AlexaOutputSpeechType.SSML,
                    Ssml = activity.Speak.Contains("<speak>")
                        ? activity.Speak
                        : $"<speak>{activity.Speak}</speak>",
                };

                if (!string.IsNullOrEmpty(activity.Text))
                {
                    response.Response.OutputSpeech.Text = $"{activity.Text} ";
                }
            }
            else if (!string.IsNullOrEmpty(activity.Text))
            {
                if (response.Response.OutputSpeech == null)
                {
                    response.Response.OutputSpeech = new AlexaOutputSpeech()
                    {
                        Type = AlexaOutputSpeechType.PlainText,
                        Text = activity.Text
                    };
                }
            }

            if (context.TurnState.ContainsKey("AlexaReprompt"))
            {
                var repromptSpeech = context.TurnState.Get <string>("AlexaReprompt");

                response.Response.Reprompt = new Reprompt()
                {
                    OutputSpeech = new AlexaOutputSpeech()
                    {
                        Type = AlexaOutputSpeechType.SSML,
                        Ssml = repromptSpeech.Contains("<speak>")
                        ? repromptSpeech
                        : $"<speak>{repromptSpeech}</speak>"
                    }
                };
            }

            AddDirectivesToResponse(context, response);

            AddCardToResponse(context, response, activity);

            switch (activity.InputHint)
            {
            case InputHints.IgnoringInput:
                response.Response.ShouldEndSession = true;
                break;

            case InputHints.ExpectingInput:
                response.Response.ShouldEndSession = false;
                break;

            case InputHints.AcceptingInput:
            default:
                break;
            }

            return(response);
        }
        private AlexaResponseBody CreateResponseFromLastActivity(IEnumerable <Activity> activities, ITurnContext context)
        {
            var response = new AlexaResponseBody()
            {
                Version  = "1.0",
                Response = new AlexaResponse()
                {
                    ShouldEndSession = context.GetAlexaRequestBody().Request.Type ==
                                       AlexaRequestTypes.SessionEndedRequest ||
                                       ShouldEndSessionByDefault
                }
            };

            if (context.GetAlexaRequestBody().Request.Type == AlexaRequestTypes.SessionEndedRequest ||
                activities == null || !activities.Any())
            {
                response.Response.OutputSpeech = new AlexaOutputSpeech()
                {
                    Type = AlexaOutputSpeechType.PlainText,
                    Text = string.Empty
                };
                return(response);
            }

            var activity = activities.First();

            if (activity.Type == ActivityTypes.EndOfConversation)
            {
                response.Response.ShouldEndSession = true;
            }

            if (!string.IsNullOrEmpty(activity.Speak))
            {
                response.Response.OutputSpeech = new AlexaOutputSpeech()
                {
                    Type = AlexaOutputSpeechType.SSML,
                    Ssml = activity.Speak.Contains("<speak>")
                        ? activity.Speak
                        : $"<speak>{activity.Speak}</speak>",
                };

                if (!string.IsNullOrEmpty(activity.Text))
                {
                    response.Response.OutputSpeech.Text = $"{activity.Text} ";
                }
            }
            else if (!string.IsNullOrEmpty(activity.Text))
            {
                if (response.Response.OutputSpeech == null)
                {
                    response.Response.OutputSpeech = new AlexaOutputSpeech()
                    {
                        Type = AlexaOutputSpeechType.PlainText,
                        Text = activity.Text
                    };
                }
            }

            if (context.TurnState.ContainsKey("AlexaReprompt"))
            {
                var repromptSpeech = context.TurnState.Get <string>("AlexaReprompt");

                response.Response.Reprompt = new Reprompt()
                {
                    OutputSpeech = new AlexaOutputSpeech()
                    {
                        Type = AlexaOutputSpeechType.SSML,
                        Ssml = repromptSpeech.Contains("<speak>")
                        ? repromptSpeech
                        : $"<speak>{repromptSpeech}</speak>"
                    }
                };
            }

            AddDirectivesToResponse(context, response);

            AddCardToResponse(context, response, activity);

            switch (activity.InputHint)
            {
            case InputHints.IgnoringInput:
                response.Response.ShouldEndSession = true;
                break;

            case InputHints.ExpectingInput:
                response.Response.ShouldEndSession = false;
                break;

            case InputHints.AcceptingInput:
            default:
                break;
            }

            return(response);
        }