LUIS is called again with no context revoke.
This commit is contained in:
		
							parent
							
								
									d2a4855702
								
							
						
					
					
						commit
						4cc4e7236a
					
				
					 4 changed files with 153 additions and 135 deletions
				
			
		| 
						 | 
				
			
			@ -56,21 +56,11 @@ export class WelcomeDialog extends IGBDialog {
 | 
			
		|||
            date < 12 ? "bom dia" : date < 18 ? "boa tarde" : "boa noite";
 | 
			
		||||
          
 | 
			
		||||
          let messages = [`Oi, ${msg}.`, `Oi!`, `Olá, ${msg}`, `Olá!`];
 | 
			
		||||
          dc.end(messages);
 | 
			
		||||
          
 | 
			
		||||
          if (dc.context.activity && dc.context.activity.text != "") {
 | 
			
		||||
            await dc.replace("/answer", { query: dc.context.activity.text });
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // V4: if (dc.context.message && dc.message.text != "") {
 | 
			
		||||
        //   dc.replace("/answer", { query: dc.message.text });
 | 
			
		||||
        //   return;
 | 
			
		||||
        // }
 | 
			
		||||
        // let userName = dc.message.user.name;
 | 
			
		||||
        // let displayName = dc.message.user.name;
 | 
			
		||||
 | 
			
		||||
        // if (args) {
 | 
			
		||||
        //   userName = args.userName;
 | 
			
		||||
        //   displayName = args.displayName;
 | 
			
		||||
        // }
 | 
			
		||||
 | 
			
		||||
      }
 | 
			
		||||
    ]);
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,6 +43,7 @@ import { GBError } from "botlib";
 | 
			
		|||
import { GBERROR_TYPE } from "botlib";
 | 
			
		||||
import { GBMinInstance } from "botlib";
 | 
			
		||||
import { LuisRecognizer } from "botbuilder-ai";
 | 
			
		||||
import {MessageFactory} from "botbuilder";
 | 
			
		||||
 | 
			
		||||
export class GBConversationalService implements IGBConversationalService {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -53,11 +54,11 @@ export class GBConversationalService implements IGBConversationalService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    sendEvent(dc: any, name: string, value: any) {
 | 
			
		||||
        var msg = new gBuilder.Message();
 | 
			
		||||
        msg.data.type = "event";
 | 
			
		||||
        msg.data.name = name;
 | 
			
		||||
        msg.data.value = value;
 | 
			
		||||
        dc.context.sendActivity(msg);
 | 
			
		||||
        const msg = MessageFactory.text('');
 | 
			
		||||
        msg.value = value;
 | 
			
		||||
        msg.type = "event";
 | 
			
		||||
        msg.name = name;
 | 
			
		||||
        // TODO: dc.context.sendActivity(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async runNLP(
 | 
			
		||||
| 
						 | 
				
			
			@ -73,7 +74,7 @@ export class GBConversationalService implements IGBConversationalService {
 | 
			
		|||
            serviceEndpoint: min.instance.nlpServerUrl
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        await model.recognize(dc).then(res => {
 | 
			
		||||
        await model.recognize(dc.context).then(res => {
 | 
			
		||||
 | 
			
		||||
            // Resolve intents returned from LUIS
 | 
			
		||||
            let topIntent = LuisRecognizer.topIntent(res);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -223,32 +223,26 @@ export class GBMinService {
 | 
			
		|||
        logger.trace(
 | 
			
		||||
          `GeneralBots(${instance.engineName}) listening on: ${url}.`
 | 
			
		||||
        );
 | 
			
		||||
        server.post('/api/messages/', (req, res) => {
 | 
			
		||||
        server.post(`/api/messages/${instance.botId}`, (req, res) => {
 | 
			
		||||
 | 
			
		||||
          adapter.processActivity(req, res, async (context) => {
 | 
			
		||||
 | 
			
		||||
            if (context.activity.type === 'message') {
 | 
			
		||||
              // Create dialog context and continue executing the "current" dialog, if any.
 | 
			
		||||
              const state = conversationState.get(context);
 | 
			
		||||
              const dc = min.dialogs.createContext(context, state);
 | 
			
		||||
            const state = conversationState.get(context);
 | 
			
		||||
            const dc = min.dialogs.createContext(context, state);
 | 
			
		||||
 | 
			
		||||
            if (context.activity.type === "conversationUpdate" &&
 | 
			
		||||
              context.activity.membersAdded.length > 0) {
 | 
			
		||||
 | 
			
		||||
              // TODO: Something when starting conversation here.
 | 
			
		||||
 | 
			
		||||
              await dc.continue();
 | 
			
		||||
            } else if (context.activity.type === 'message') {
 | 
			
		||||
 | 
			
		||||
              // Check to see if anyone replied. If not then start echo dialog
 | 
			
		||||
 | 
			
		||||
              if (!context.responded) {
 | 
			
		||||
                await dc.begin('echo');
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              //   context.activity.type === "conversationUpdate" &&
 | 
			
		||||
              //   context.activity.membersAdded.length > 0
 | 
			
		||||
 | 
			
		||||
              //   // if (context.activity.address.channelId != "directline") {
 | 
			
		||||
              //   //   dc.begin("/");
 | 
			
		||||
              //   // }
 | 
			
		||||
              //   // else {
 | 
			
		||||
              //   //   next();
 | 
			
		||||
              //   // }
 | 
			
		||||
 | 
			
		||||
              if (context.activity.name === "whoAmI") {
 | 
			
		||||
                await dc.begin('/');
 | 
			
		||||
              }else if (context.activity.name === "whoAmI") {
 | 
			
		||||
                dc.begin("/whoAmI");
 | 
			
		||||
              } else if (context.activity.name === "showSubjects") {
 | 
			
		||||
                dc.begin("/menu");
 | 
			
		||||
| 
						 | 
				
			
			@ -273,16 +267,12 @@ export class GBMinService {
 | 
			
		|||
 | 
			
		||||
              const user = min.userState.get(dc.context);
 | 
			
		||||
              if (!user.loaded) {
 | 
			
		||||
                setTimeout(
 | 
			
		||||
                  () => {
 | 
			
		||||
                    min.conversationalService.sendEvent(
 | 
			
		||||
                      dc,
 | 
			
		||||
                      "loadInstance",
 | 
			
		||||
                      min.instance // TODO: Send a new thiner object.
 | 
			
		||||
                    )
 | 
			
		||||
                  },
 | 
			
		||||
                  500
 | 
			
		||||
                );
 | 
			
		||||
                // min.conversationalService.sendEvent(
 | 
			
		||||
                //   dc,
 | 
			
		||||
                //   "loadInstance",
 | 
			
		||||
                //   min.instance // TODO: Send a new thiner object.
 | 
			
		||||
                // );
 | 
			
		||||
 | 
			
		||||
                user.loaded = true;
 | 
			
		||||
                user.subjects = [];
 | 
			
		||||
              }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -37,6 +37,7 @@ import { AzureText } from "pragmatismo-io-framework";
 | 
			
		|||
import { GBMinInstance } from "botlib";
 | 
			
		||||
import { KBService } from './../services/KBService';
 | 
			
		||||
import { BotAdapter } from "botbuilder";
 | 
			
		||||
import { LuisRecognizer } from "botbuilder-ai";
 | 
			
		||||
 | 
			
		||||
const logger = require("../../../src/logger");
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -45,6 +46,13 @@ export class AskDialog extends IGBDialog {
 | 
			
		|||
 | 
			
		||||
    const service = new KBService();
 | 
			
		||||
 | 
			
		||||
    const model = new LuisRecognizer({
 | 
			
		||||
      appId: min.instance.nlpAppId,
 | 
			
		||||
      subscriptionKey: min.instance.nlpSubscriptionKey,
 | 
			
		||||
      serviceEndpoint: min.instance.nlpServerUrl
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    min.dialogs.add("/answer", [
 | 
			
		||||
      async (dc, args) => {
 | 
			
		||||
        const user = min.userState.get(dc.context);
 | 
			
		||||
| 
						 | 
				
			
			@ -63,102 +71,128 @@ export class AskDialog extends IGBDialog {
 | 
			
		|||
          dc.context.sendActivity(messages[0]); // TODO: Handle rnd.
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (text === "") {
 | 
			
		||||
          dc.replace("/ask");
 | 
			
		||||
        } else {
 | 
			
		||||
          AzureText.getSpelledText(
 | 
			
		||||
            min.instance.spellcheckerKey,
 | 
			
		||||
            text,
 | 
			
		||||
            (data, err) => {
 | 
			
		||||
              if (data != text) {
 | 
			
		||||
                logger.trace("Spelled Text: " + data);
 | 
			
		||||
                text = data;
 | 
			
		||||
              }
 | 
			
		||||
              user.lastQuestion = data;
 | 
			
		||||
      await model.recognize(dc.context).then(res => {
 | 
			
		||||
        console.log(res);
 | 
			
		||||
      }).catch(err => {
 | 
			
		||||
        console.log(err);
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
              service.ask(
 | 
			
		||||
                min.instance,
 | 
			
		||||
                text,
 | 
			
		||||
                min.instance.searchScore,
 | 
			
		||||
                user.subjects,
 | 
			
		||||
                resultsA => {
 | 
			
		||||
                  min.conversationalService.sendEvent(dc, "stop", null);
 | 
			
		||||
        // await min.conversationalService.runNLP(
 | 
			
		||||
        //   dc,
 | 
			
		||||
        //   min,
 | 
			
		||||
        //   text,
 | 
			
		||||
        //   (data, error) => {
 | 
			
		||||
 | 
			
		||||
                  if (resultsA && resultsA.answer) {
 | 
			
		||||
                    user.isAsking = false;
 | 
			
		||||
                    service.sendAnswer(min.conversationalService,
 | 
			
		||||
                      dc,
 | 
			
		||||
                      resultsA.answer
 | 
			
		||||
                    );
 | 
			
		||||
                    user.lastQuestionId = resultsA.questionId;
 | 
			
		||||
        //     if (!data) {
 | 
			
		||||
        //       let messages = [
 | 
			
		||||
        //         "Desculpe-me, não encontrei nada a respeito.",
 | 
			
		||||
        //         "Lamento... Não encontrei nada sobre isso. Vamos tentar novamente?",
 | 
			
		||||
        //         "Desculpe-me, não achei nada parecido. Poderia tentar escrever de outra forma?"
 | 
			
		||||
        //       ];
 | 
			
		||||
 | 
			
		||||
                    dc.replace("/ask", { isReturning: true });
 | 
			
		||||
                  } else {
 | 
			
		||||
                    //if (min.isAsking) {
 | 
			
		||||
                    // Second time with no filter.
 | 
			
		||||
        //       dc.context.sendActivity(messages[0]); // TODO: Handle rnd.
 | 
			
		||||
        //       dc.replace("/ask", { isReturning: true });
 | 
			
		||||
        //     }
 | 
			
		||||
        //   }
 | 
			
		||||
        // );
 | 
			
		||||
 | 
			
		||||
                    service.ask(
 | 
			
		||||
                      min.instance,
 | 
			
		||||
                      text,
 | 
			
		||||
                      min.instance.searchScore,
 | 
			
		||||
                      null,
 | 
			
		||||
                      resultsB => {
 | 
			
		||||
                        if (resultsB && resultsB.answer) {
 | 
			
		||||
                          const user = min.userState.get(dc.context);
 | 
			
		||||
 | 
			
		||||
                          user.isAsking = false;
 | 
			
		||||
        // if (text === "") {
 | 
			
		||||
        //   dc.replace("/ask");
 | 
			
		||||
        // } else {
 | 
			
		||||
        //   // AzureText.getSpelledText(
 | 
			
		||||
        //   //   min.instance.spellcheckerKey,
 | 
			
		||||
        //   //   text,
 | 
			
		||||
        //   //   async (data, err) => {
 | 
			
		||||
        //   var data = text;
 | 
			
		||||
        //   if (data != text) {
 | 
			
		||||
        //     logger.trace("Spelled Text: " + data);
 | 
			
		||||
        //     text = data;
 | 
			
		||||
        //   }
 | 
			
		||||
        //   user.lastQuestion = data;
 | 
			
		||||
 | 
			
		||||
                          if (user.subjects.length > 0) {
 | 
			
		||||
                            let subjectText =
 | 
			
		||||
                              `${KBService.getSubjectItemsSeparatedBySpaces(
 | 
			
		||||
                                user.subjects
 | 
			
		||||
                              )}`;
 | 
			
		||||
        //   service.ask(
 | 
			
		||||
        //     min.instance,
 | 
			
		||||
        //     text,
 | 
			
		||||
        //     min.instance.searchScore,
 | 
			
		||||
        //     user.subjects,
 | 
			
		||||
        //     async resultsA => {
 | 
			
		||||
        //       min.conversationalService.sendEvent(dc, "stop", null);
 | 
			
		||||
 | 
			
		||||
                            let messages = [
 | 
			
		||||
                              `Respondendo nao apenas sobre ${subjectText}... `,
 | 
			
		||||
                              `Respondendo de modo mais abrangente...`,
 | 
			
		||||
                              `Vou te responder de modo mais abrangente... 
 | 
			
		||||
                                Não apenas sobre ${subjectText}`
 | 
			
		||||
                            ];
 | 
			
		||||
                            dc.context.sendActivity(messages[0]); // TODO: Handle rnd.
 | 
			
		||||
                          }
 | 
			
		||||
                          user.isAsking = false;
 | 
			
		||||
                          service.sendAnswer(min.conversationalService,
 | 
			
		||||
                            dc,
 | 
			
		||||
                            resultsB.answer
 | 
			
		||||
                          );
 | 
			
		||||
                          dc.replace("/ask", { isReturning: true });
 | 
			
		||||
        //       if (resultsA && resultsA.answer) {
 | 
			
		||||
        //         user.isAsking = false;
 | 
			
		||||
        //         service.sendAnswer(min.conversationalService,
 | 
			
		||||
        //           dc,
 | 
			
		||||
        //           resultsA.answer
 | 
			
		||||
        //         );
 | 
			
		||||
        //         user.lastQuestionId = resultsA.questionId;
 | 
			
		||||
 | 
			
		||||
                          user.lastQuestionId = resultsB.questionId;
 | 
			
		||||
                        } else {
 | 
			
		||||
        //         dc.replace("/ask", { isReturning: true });
 | 
			
		||||
        //       } else {
 | 
			
		||||
        //         //if (min.isAsking) {
 | 
			
		||||
        //         // Second time with no filter.
 | 
			
		||||
 | 
			
		||||
                          min.conversationalService.runNLP(
 | 
			
		||||
                            dc,
 | 
			
		||||
                            min,
 | 
			
		||||
                            text,
 | 
			
		||||
                            (data, error) => {
 | 
			
		||||
        //         service.ask(
 | 
			
		||||
        //           min.instance,
 | 
			
		||||
        //           text,
 | 
			
		||||
        //           min.instance.searchScore,
 | 
			
		||||
        //           null,
 | 
			
		||||
        //           async resultsB => {
 | 
			
		||||
        //             if (resultsB && resultsB.answer) {
 | 
			
		||||
        //               const user = min.userState.get(dc.context);
 | 
			
		||||
 | 
			
		||||
                              if (!data) {
 | 
			
		||||
                                let messages = [
 | 
			
		||||
                                  "Desculpe-me, não encontrei nada a respeito.",
 | 
			
		||||
                                  "Lamento... Não encontrei nada sobre isso. Vamos tentar novamente?",
 | 
			
		||||
                                  "Desculpe-me, não achei nada parecido. Poderia tentar escrever de outra forma?"
 | 
			
		||||
                                ];
 | 
			
		||||
        //               user.isAsking = false;
 | 
			
		||||
 | 
			
		||||
                                dc.context.sendActivity(messages[0]); // TODO: Handle rnd.
 | 
			
		||||
                                dc.replace("/ask", { isReturning: true });
 | 
			
		||||
                              }
 | 
			
		||||
                            }
 | 
			
		||||
                          );
 | 
			
		||||
                        }
 | 
			
		||||
                      }
 | 
			
		||||
                    );
 | 
			
		||||
                  }
 | 
			
		||||
                }
 | 
			
		||||
              );
 | 
			
		||||
            }
 | 
			
		||||
          );
 | 
			
		||||
        }
 | 
			
		||||
        //               if (user.subjects.length > 0) {
 | 
			
		||||
        //                 let subjectText =
 | 
			
		||||
        //                   `${KBService.getSubjectItemsSeparatedBySpaces(
 | 
			
		||||
        //                     user.subjects
 | 
			
		||||
        //                   )}`;
 | 
			
		||||
 | 
			
		||||
        //                 let messages = [
 | 
			
		||||
        //                   `Respondendo nao apenas sobre ${subjectText}... `,
 | 
			
		||||
        //                   `Respondendo de modo mais abrangente...`,
 | 
			
		||||
        //                   `Vou te responder de modo mais abrangente... 
 | 
			
		||||
        //                         Não apenas sobre ${subjectText}`
 | 
			
		||||
        //                 ];
 | 
			
		||||
        //                 dc.context.sendActivity(messages[0]); // TODO: Handle rnd.
 | 
			
		||||
        //               }
 | 
			
		||||
        //               user.isAsking = false;
 | 
			
		||||
        //               service.sendAnswer(min.conversationalService,
 | 
			
		||||
        //                 dc,
 | 
			
		||||
        //                 resultsB.answer
 | 
			
		||||
        //               );
 | 
			
		||||
        //               dc.replace("/ask", { isReturning: true });
 | 
			
		||||
 | 
			
		||||
        //               user.lastQuestionId = resultsB.questionId;
 | 
			
		||||
        //             } else {
 | 
			
		||||
 | 
			
		||||
        //               await min.conversationalService.runNLP(
 | 
			
		||||
        //                 dc,
 | 
			
		||||
        //                 min,
 | 
			
		||||
        //                 text,
 | 
			
		||||
        //                 (data, error) => {
 | 
			
		||||
 | 
			
		||||
        //                   if (!data) {
 | 
			
		||||
        //                     let messages = [
 | 
			
		||||
        //                       "Desculpe-me, não encontrei nada a respeito.",
 | 
			
		||||
        //                       "Lamento... Não encontrei nada sobre isso. Vamos tentar novamente?",
 | 
			
		||||
        //                       "Desculpe-me, não achei nada parecido. Poderia tentar escrever de outra forma?"
 | 
			
		||||
        //                     ];
 | 
			
		||||
 | 
			
		||||
        //                     dc.context.sendActivity(messages[0]); // TODO: Handle rnd.
 | 
			
		||||
        //                     dc.replace("/ask", { isReturning: true });
 | 
			
		||||
        //                   }
 | 
			
		||||
        //                 }
 | 
			
		||||
        //               );
 | 
			
		||||
        //             }
 | 
			
		||||
        //           }
 | 
			
		||||
        //         );
 | 
			
		||||
        //       }
 | 
			
		||||
        //     }
 | 
			
		||||
        //   );
 | 
			
		||||
        // }
 | 
			
		||||
        //);        }
 | 
			
		||||
      }
 | 
			
		||||
    ]);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -167,6 +201,9 @@ export class AskDialog extends IGBDialog {
 | 
			
		|||
      async (dc, args) => {
 | 
			
		||||
        const user = min.userState.get(dc.context);
 | 
			
		||||
        user.isAsking = true;
 | 
			
		||||
        if (!user.subjects) {
 | 
			
		||||
          user.subjects = [];
 | 
			
		||||
        }
 | 
			
		||||
        let text = [];
 | 
			
		||||
        if (user.subjects.length > 0) {
 | 
			
		||||
          text = [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue