示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AlexaController"/> class.
 /// </summary>
 /// <param name="userManager">The <see cref="UserManager{TUser}"/> to use.</param>
 /// <param name="telemetry">The <see cref="ISiteTelemetry"/> to use.</param>
 /// <param name="siteOptions">The current site options.</param>
 /// <param name="logger">The <see cref="ILogger"/> to use.</param>
 public AlexaController(
     UserManager <LondonTravelUser> userManager,
     ISiteTelemetry telemetry,
     SiteOptions siteOptions,
     ILogger <AlexaController> logger)
 {
     _userManager = userManager;
     _telemetry   = telemetry;
     _options     = siteOptions?.Alexa;
     _logger      = logger;
 }
        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;
            }
        }
 public AlexaRequestHandler(AlexaAdapter alexaAdapter, AlexaOptions alexaOptions)
 {
     _alexaAdapter = alexaAdapter;
     _alexaOptions = alexaOptions;
 }