如何在分发中路由对话框?

时间:2019-05-09 16:27:28

标签: node.js azure botframework

我最近将代码更新为最新版本的bot框架。我无法通过我的机器人中的分派处理程序执行beginDialog。

当前,它说this.addDialog不是函数。我认为这可能是因为当前DialogBot类是从Activity Handler扩展的。

如何为特定的LUIS意图使用beginDialog?

const { ActivityHandler } = require("botbuilder");
const { LuisRecognizer, QnAMaker } = require("botbuilder-ai");
const { AuthDilaog, AUTH_DIALOG } = require("../dialogs/authDialog");

class DispatchBot extends ActivityHandler {
  /**
   * @param {any} logger object for logging events, defaults to console if none is provided
   */
  constructor(conversationState, userState, dialog) {
    super();

    this.dialog = dialog;

    this.addDialog(new AuthDialog())


    this.conversationState = conversationState;
    this.dialogState = this.conversationState.createProperty("DialogState");
    const dispatchRecognizer = new LuisRecognizer(
      {
        applicationId: process.env.LuisAppId,
        endpointKey: process.env.LuisAPIKey,
        endpoint: `https://${
          process.env.LuisAPIHostName
        }.api.cognitive.microsoft.com`
      },
      {
        includeAllIntents: true,
        includeInstanceData: true
      },
      true
    );

    const qnaMaker = new QnAMaker({
      knowledgeBaseId: process.env.QnAKnowledgebaseId,
      endpointKey: process.env.QnAAuthKey,
      host: process.env.QnAEndpointHostName
    });

    this.dispatchRecognizer = dispatchRecognizer;
    this.qnaMaker = qnaMaker;

    this.onMessage(async (context, next) => {
      // First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
      const recognizerResult = await dispatchRecognizer.recognize(context);

      // Top intent tell us which cognitive service to use.
      const intent = LuisRecognizer.topIntent(recognizerResult);

      // Next, we call the dispatcher with the top intent.
      await this.dispatchToTopIntentAsync(context, intent, recognizerResult);

      await next();

      //
    });

    this.onMembersAdded(async (context, next) => {
      const welcomeText =
        "Type a greeting or a question about the weather to get started.";
      const membersAdded = context.activity.membersAdded;

      for (let member of membersAdded) {
        if (member.id !== context.activity.recipient.id) {
          await context.sendActivity(
            `Welcome to Dispatch bot ${member.name}. ${welcomeText}`
          );
        }
      }

      // By calling next() you ensure that the next BotHandler is run.
      await next();
    });
  }

  async dispatchToTopIntentAsync(context, intent, recognizerResult) {
    switch (intent) {
      case "blackboard":
        await this.processSample(context, recognizerResult.luisResult);
        break;
      default:
        await context.sendActivity(`Dispatch unrecognized intent: ${intent}.`);
        break;
    }
  }

async processSample(context, luisResult) {
     return await context.beginDialog(AUTH_DIALOG)
}

0 个答案:

没有答案