首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向bot添加身份验证和Qnamaker

向bot添加身份验证和Qnamaker
EN

Stack Overflow用户
提问于 2022-06-01 01:36:25
回答 1查看 105关注 0票数 0

我正在尝试使用bot框架sdk构建一个机器人。

目标:-

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

我正在使用来自nodejs/18.bot-认证的bot认证模板。我尝试在“流水”对话框中添加qnamaker服务。

我面临两个问题:-

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

源代码

mainDialog.js

代码语言:javascript
复制
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链接:https://github.com/chandelsumeet/authBot

EN

回答 1

Stack Overflow用户

发布于 2022-06-06 08:58:17

代码语言:javascript
复制
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()并检查它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72455602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档