首页 > 解决方案 > Botframework SDK v.4 - 瀑布对话框中的提示步骤导致无法再调用存储中的对象

问题描述

目前,我正在尝试使用 Bot 框架的 SDK v4 开发聊天机器人,但遇到以下问题: 基于 Microsoft 的 BasicBot 模板,我尝试构建自己的瀑布对话。在瀑布对话框的 promptUserForAttachmentStep 函数中,应要求用户使用 AttachmentPrompt 上传图像。由于此提示,不再可能在以下步骤中使用状态访问器从内存中访问 UserProfile 对象。我得到的只是一个空对象。

不管是什么提示。在提示步骤内,对象仍然可以被调用。但不是在那之后。但是,只要我删除提示,就可以在下一步中调用它。

不幸的是,我不太明白为什么会这样。这在模板中不是问题。

我会很高兴得到帮助

许多问候

import { StatePropertyAccessor } from 'botbuilder';
import { ComponentDialog, PromptValidatorContext, WaterfallDialog, WaterfallStepContext, AttachmentPrompt, TextPrompt } from 'botbuilder-dialogs';
import { FaceAPI } from './API_Files/faceAPI_conn';
import { SQLConntector } from './API_Files/mysql_conn';
import { UserProfile } from './userProfile';

const FACE_DIALOG = 'faceDialog';
const LOGIN_DIALOG = 'loginDialog'
const ATTACH_PROMPT = 'attachPrompt';
const SECRET_PROMPT = 'secretPrompt;'

export class authentDialog extends ComponentDialog {
    private userProfileAccessor: StatePropertyAccessor<UserProfile>;
    private faceAPI;
    private sqlConnector: SQLConntector;

    constructor (dialogId: string, userStateAccessor: StatePropertyAccessor<UserProfile>) {
        super(dialogId);

        // TODO: Daten auslagern
        this.faceAPI =  new FaceAPI (
            "https://northeurope.api.cognitive.microsoft.com/face/v1.0/",
            ""
        );
        this.sqlConnector = new SQLConntector (
            "",
            "",
            "",
            ""
        );

        this.addDialog(new WaterfallDialog<UserProfile>(FACE_DIALOG, [
            this.initializeUserStateStep.bind(this),
            this.promptUserForAttachmentStep.bind(this),
            this.identificateFaceId.bind(this),
            this.detectPersonId.bind(this)
        ]));

        this.addDialog(new AttachmentPrompt(ATTACH_PROMPT));
        this.addDialog(new TextPrompt(SECRET_PROMPT));

        this.addDialog(new WaterfallDialog<UserProfile> (LOGIN_DIALOG, [
            this.getEmployeeName.bind(this)
        ]));


        this.userProfileAccessor = userStateAccessor;
    }

    private async initializeUserStateStep (step: WaterfallStepContext<UserProfile>) {
        const userProfile = await this.userProfileAccessor.get(step.context);
        if (userProfile === undefined) {
            await this.userProfileAccessor.set(step.context, new UserProfile());
        }
        return await step.next();
    }

    private async promptUserForAttachmentStep (step: WaterfallStepContext<UserProfile>) {
        // This writes the userProfile object
        console.log(await this.userProfileAccessor.get(step.context));
        return await step.prompt(ATTACH_PROMPT,'Bitte lade zur Autorisierung ein Foto deines Gesichts hoch.');
    }

    private async identificateFaceId (step: WaterfallStepContext<UserProfile>) {
        // This is now an empty object
        console.log(await this.userProfileAccessor.get(step.context));
        if (!step.result[0].contentUrl) {
            await step.context.sendActivities([
                {type: 'typing'},
                {type: 'delay', value: 2000},
                {type: 'message', text: 'Ich benötige ein Foto :)' }
            ])
            return await step.replaceDialog(FACE_DIALOG);
        } else {
            const faceID = await this.faceAPI.getDetectedFaceId(step.result[0].contentUrl);
            if (!faceID) {
                await step.context.sendActivities([
                    {type: 'typing'},
                    {type: 'delay', value: 2000},
                    {type: 'message', text: 'Ich konnte kein Gesicht erkennen :(.' },
                    {type: 'typing'},
                    {type: 'delay', value: 2000},
                    {type: 'message', text: 'Bitte versuche es nochmal.' }
                ]);
                return await step.replaceDialog(FACE_DIALOG);
            } else {
                return await step.next(faceID);
            }
        }
    }
}

标签: javascriptazurebotframeworkazure-bot-service

解决方案


推荐阅读