将身份验证和QNAMAKER添加到机器人

发布于 2025-02-03 11:25:09 字数 3990 浏览 4 评论 0 原文

我正在尝试使用BOT Framework SDK构建一个机器人。

目的: -

  1. 对用户进行身份验证。
  2. AFTE身份验证与QNAMAKER知识库进行通信。
  3. 签名用户如果他输入注销,

我正在使用bot身份验证模板,来自 https://github.com/microsoft/botbuilder-samples/tree/main/main/samples/javascript_nodejs/18.bot-authentication 。我试图在WaterFlowDialog中添加Qnamaker服务。

我面临两个问题: -

  1. 每一个Qnamaker回复,我都会收到消息“您现在已登录”。
  2. 机器人遇到错误。

源代码

maindialog.js

const {
  ConfirmPrompt,
  DialogSet,
  DialogTurnStatus,
  OAuthPrompt,
  WaterfallDialog,
} = require("botbuilder-dialogs");

const { LogoutDialog } = require("./logoutDialog");
const { QnAMakerDialog } = require("botbuilder-ai");
const CONFIRM_PROMPT = "ConfirmPrompt";
const MAIN_DIALOG = "MainDialog";
const MAIN_WATERFALL_DIALOG = "MainWaterfallDialog";
const OAUTH_PROMPT = "OAuthPrompt";
const QNAMAKER_BASE_DIALOG = "qnamaker-base-dialog";

const createQnAMakerDialog = (
  knowledgeBaseId,
  endpointKey,
  endpointHostName,
  defaultAnswer
) => {
  let noAnswerActivity;
  if (typeof defaultAnswer === "string") {
    noAnswerActivity = MessageFactory.text(defaultAnswer);
  }

  const qnaMakerDialog = new QnAMakerDialog(
    knowledgeBaseId,
    endpointKey,
    endpointHostName,
    noAnswerActivity
  );
  qnaMakerDialog.id = QNAMAKER_BASE_DIALOG;

  return qnaMakerDialog;
};

class MainDialog extends LogoutDialog {
  constructor(knowledgeBaseId, endpointKey, endpointHostName, defaultAnswer) {
    super(MAIN_DIALOG, process.env.connectionName);

    this.addDialog(
      new OAuthPrompt(OAUTH_PROMPT, {
        connectionName: process.env.connectionName,
        text: "Please Sign In",
        title: "Sign In",
        timeout: 300000,
      })
    );
    this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
    this.addDialog(
      new WaterfallDialog(MAIN_WATERFALL_DIALOG, [
        this.promptStep.bind(this),
        this.loginStep.bind(this),
        this.qnaMaker.bind(this),
      ])
    );
    this.addDialog(
      createQnAMakerDialog(
        knowledgeBaseId,
        endpointKey,
        endpointHostName,
        defaultAnswer
      )
    );
    this.initialDialogId = MAIN_WATERFALL_DIALOG;
  }

  /**
   * The run method handles the incoming activity (in the form of a DialogContext) and passes it through the dialog system.
   * If no dialog is active, it will start the default dialog.
   * @param {*} dialogContext
   */

  async run(context, accessor) {
    const dialogSet = new DialogSet(accessor);
    dialogSet.add(this);

    const dialogContext = await dialogSet.createContext(context);
    const results = await dialogContext.continueDialog();
    if (results.status === DialogTurnStatus.empty) {
      await dialogContext.beginDialog(this.id);
    }
  }

  async qnaMaker(stepContext) {
    await stepContext.beginDialog(QNAMAKER_BASE_DIALOG);
  }
  async promptStep(stepContext) {
    return await stepContext.beginDialog(OAUTH_PROMPT);
  }

  async loginStep(stepContext) {
    // Get the token from the previous step. Note that we could also have gotten the
    // token directly from the prompt itself. There is an example of this in the next method.
    const tokenResponse = stepContext.result;
    if (tokenResponse) {
      await stepContext.context.sendActivity("You are now logged in");
      return await stepContext.next();
    }
    await stepContext.context.sendActivity(
      "Login was not successful please try again."
    );
    return await stepContext.endDialog();
  }
}

module.exports.MainDialog = MainDialog;

bot ScreenShot github

github link:https://github.com/chandelsumeet/authBot

I am trying to build a bot using bot framework sdk.

Objective:-

  1. To authenticate user.
  2. afte authentication communicate with Qnamaker knowledge base.
  3. signout user if he types logout

I am using bot authentication template from https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/javascript_nodejs/18.bot-authentication. I have tried to add qnamaker service in the waterflowdialog.

I am facing two issues:-

  1. With every Qnamaker reply I am getting message "you are now logged in".
  2. Bot encountered an error.

Source Code

mainDialog.js

const {
  ConfirmPrompt,
  DialogSet,
  DialogTurnStatus,
  OAuthPrompt,
  WaterfallDialog,
} = require("botbuilder-dialogs");

const { LogoutDialog } = require("./logoutDialog");
const { QnAMakerDialog } = require("botbuilder-ai");
const CONFIRM_PROMPT = "ConfirmPrompt";
const MAIN_DIALOG = "MainDialog";
const MAIN_WATERFALL_DIALOG = "MainWaterfallDialog";
const OAUTH_PROMPT = "OAuthPrompt";
const QNAMAKER_BASE_DIALOG = "qnamaker-base-dialog";

const createQnAMakerDialog = (
  knowledgeBaseId,
  endpointKey,
  endpointHostName,
  defaultAnswer
) => {
  let noAnswerActivity;
  if (typeof defaultAnswer === "string") {
    noAnswerActivity = MessageFactory.text(defaultAnswer);
  }

  const qnaMakerDialog = new QnAMakerDialog(
    knowledgeBaseId,
    endpointKey,
    endpointHostName,
    noAnswerActivity
  );
  qnaMakerDialog.id = QNAMAKER_BASE_DIALOG;

  return qnaMakerDialog;
};

class MainDialog extends LogoutDialog {
  constructor(knowledgeBaseId, endpointKey, endpointHostName, defaultAnswer) {
    super(MAIN_DIALOG, process.env.connectionName);

    this.addDialog(
      new OAuthPrompt(OAUTH_PROMPT, {
        connectionName: process.env.connectionName,
        text: "Please Sign In",
        title: "Sign In",
        timeout: 300000,
      })
    );
    this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
    this.addDialog(
      new WaterfallDialog(MAIN_WATERFALL_DIALOG, [
        this.promptStep.bind(this),
        this.loginStep.bind(this),
        this.qnaMaker.bind(this),
      ])
    );
    this.addDialog(
      createQnAMakerDialog(
        knowledgeBaseId,
        endpointKey,
        endpointHostName,
        defaultAnswer
      )
    );
    this.initialDialogId = MAIN_WATERFALL_DIALOG;
  }

  /**
   * The run method handles the incoming activity (in the form of a DialogContext) and passes it through the dialog system.
   * If no dialog is active, it will start the default dialog.
   * @param {*} dialogContext
   */

  async run(context, accessor) {
    const dialogSet = new DialogSet(accessor);
    dialogSet.add(this);

    const dialogContext = await dialogSet.createContext(context);
    const results = await dialogContext.continueDialog();
    if (results.status === DialogTurnStatus.empty) {
      await dialogContext.beginDialog(this.id);
    }
  }

  async qnaMaker(stepContext) {
    await stepContext.beginDialog(QNAMAKER_BASE_DIALOG);
  }
  async promptStep(stepContext) {
    return await stepContext.beginDialog(OAUTH_PROMPT);
  }

  async loginStep(stepContext) {
    // Get the token from the previous step. Note that we could also have gotten the
    // token directly from the prompt itself. There is an example of this in the next method.
    const tokenResponse = stepContext.result;
    if (tokenResponse) {
      await stepContext.context.sendActivity("You are now logged in");
      return await stepContext.next();
    }
    await stepContext.context.sendActivity(
      "Login was not successful please try again."
    );
    return await stepContext.endDialog();
  }
}

module.exports.MainDialog = MainDialog;

Bot ScreenShot

github link : https://github.com/chandelsumeet/authBot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-02-10 11:25:10
async loginStep(stepContext) {
    // Get the token from the previous step. Note that we could also have gotten the
    // token directly from the prompt itself. There is an example of this in the next method.
    const tokenResponse = stepContext.result;
    if (tokenResponse) {
      await stepContext.context.sendActivity('You are now logged in.');
      return await stepContext.prompt(CONFIRM_PROMPT, 'Would you like to view your token?');
    }
    await stepContext.context.sendActivity('Login was not successful please try again.');
    return await stepContext.endDialog();
  }

用上述代码块替换 loginstep()并进行检查。

async loginStep(stepContext) {
    // Get the token from the previous step. Note that we could also have gotten the
    // token directly from the prompt itself. There is an example of this in the next method.
    const tokenResponse = stepContext.result;
    if (tokenResponse) {
      await stepContext.context.sendActivity('You are now logged in.');
      return await stepContext.prompt(CONFIRM_PROMPT, 'Would you like to view your token?');
    }
    await stepContext.context.sendActivity('Login was not successful please try again.');
    return await stepContext.endDialog();
  }

Replace the loginStep() with above code block and check it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文