Ejemplo n.º 1
0
        public async Task InvokeAsync(HttpContext context, GrossIncomeIntentWorker grossIncomeIntentWorker, NetIncomeIntentWorker netIncomeIntentWorker, OutgoingInvoicePaymentCheckWorker outgoingInvoicePaymentCheckWorker)
        {
            var streamReader = new StreamReader(context.Request.Body);
            var rawBody      = await streamReader.ReadToEndAsync();

            var           skillRequest = JsonConvert.DeserializeObject <SkillRequest>(rawBody.ToString());
            SkillResponse response     = null;

            if (skillRequest.Request is IntentRequest)
            {
                var request    = (IntentRequest)skillRequest.Request;
                var intentName = request.Intent.Name;
                if (intentName == "Gross_Income")
                {
                    var queryParams = QueryParametersFactory
                                      .Accountancy
                                      .GrossIncome.Parse(request);
                    var msg = grossIncomeIntentWorker.Do(queryParams);
                    response = ResponseBuilder.Tell(msg);
                }
                else if (intentName == "Net_Income")
                {
                    var queryParams = QueryParametersFactory
                                      .Accountancy
                                      .NetIncome.Parse(request);
                    var msg = netIncomeIntentWorker.Do(queryParams);
                    response = ResponseBuilder.Tell(msg);
                }
                else if (intentName == "Outgoing_Invoice_Payment_Check")
                {
                    var queryParams = QueryParametersFactory
                                      .Accountancy
                                      .OutgoingInvoicePaymentCheck.Parse(request);
                    var msg = outgoingInvoicePaymentCheckWorker.Do(queryParams);
                    response = ResponseBuilder.Tell(msg);
                }
                else
                {
                    var msg = "Alas, I couldn't understand what you asked for.";
                    response = ResponseBuilder.Tell(msg);
                }
            }
            else if (skillRequest.Request is LaunchRequest)
            {
                var request  = (LaunchRequest)skillRequest.Request;
                var msg      = "Hi, I am Martin: how can I help you?";
                var reprompt = "Ground control to major Tom: Martin's still waiting for you here.";
                response = ResponseBuilder.Ask(msg, new Reprompt(reprompt));
            }
            var responseJson = JsonConvert.SerializeObject(response);
            await context.Response.WriteAsync(responseJson);

            await _next(context);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Every conversation turn for our LUIS Bot will call this method.
 /// There are no dialogs used, the sample only uses "single turn" processing,
 /// meaning a single request and response, with no stateful conversation.
 /// </summary>
 /// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
 /// for processing this conversation turn. </param>
 /// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects
 /// or threads to receive notice of cancellation.</param>
 /// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
 public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
 {
     try
     {
         if (turnContext.Activity.Type == ActivityTypes.Message)
         {
             // Check LUIS model
             var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
             var topIntent        = recognizerResult?.GetTopScoringIntent();
             if (topIntent != null && topIntent.HasValue && topIntent.Value.intent != "None")
             {
                 var intentName = topIntent.Value.intent;
                 if (intentName == "Gross_Income")
                 {
                     var queryParams = QueryParametersFactory
                                       .Accountancy
                                       .GrossIncome.Parse(recognizerResult);
                     var msg = _grossIncomeIntentWorker.Do(queryParams);
                     await turnContext.SendActivityAsync(msg);
                 }
                 else if (intentName == "Net_Income")
                 {
                     var queryParams = QueryParametersFactory
                                       .Accountancy
                                       .NetIncome.Parse(recognizerResult);
                     var msg = _netIncomeIntentWorker.Do(queryParams);
                     await turnContext.SendActivityAsync(msg);
                 }
                 else if (intentName == "Outgoing_Invoice_Payment_Check")
                 {
                     var queryParams = QueryParametersFactory
                                       .Accountancy
                                       .OutgoingInvoicePaymentCheck.Parse(recognizerResult);
                     var msg = _outgoingInvoicePaymentCheckWorker.Do(queryParams);
                     await turnContext.SendActivityAsync(msg);
                 }
                 else
                 {
                     await turnContext.SendActivityAsync($"==>LUIS Top Scoring Intent: {topIntent.Value.intent}, Score: {topIntent.Value.score}\n");
                 }
             }
             else
             {
                 var msg = @"No LUIS intents were found.";
                 await turnContext.SendActivityAsync(msg);
             }
         }
         else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
         {
             // Send a welcome message to the user and tell them what actions they may perform to use this bot
             await SendWelcomeMessageAsync(turnContext, cancellationToken);
         }
         else
         {
             await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken : cancellationToken);
         }
     }
     catch (Exception ex)
     {
         ILogger logger = _loggerFactory.CreateLogger <LuisBot>();
         logger.LogError($"\r\nException caught : {ex.StackTrace}\r\n");
     }
 }