NodeJS botbuilder SDKv4

时间:2019-03-10 01:27:31

标签: node.js botframework bots

如何存储用户输入的值?例如,我已经创建了此对话框,并且想要创建电子邮件并将其存储在数据库中,但是我不确定在何处添加功能。

谢谢

构造函数:

constructor(conversationState) {

        this.dialogStateAccessor = conversationState.createProperty(DIALOG_STATE_ACCESSOR);
        this.holidayAccessor = conversationState.createProperty(HOLIDAY_ACCESSOR);
        this.conversationState = conversationState;

        this.dialogSet = new DialogSet(this.dialogStateAccessor);
        this.dialogSet.add(new ChoicePrompt(MONTH_PROMPT));
        this.dialogSet.add(new ChoicePrompt(START_DATE_PROMPT));
        this.dialogSet.add(new ChoicePrompt(END_DATE_PROMPT));

        this.dialogSet.add(new WaterfallDialog(HOLIDAY_DIALOG, [
            this.promptForMonth.bind(this),
            this.promptForstartDate.bind(this),
            this.promptForendDate.bind(this),
        ]));
    }

TurnContext:

case ActivityTypes.Message:
                const holiday = await this.holidayAccessor.get(turnContext, null);

                const dc = await this.dialogSet.createContext(turnContext);

                if (!dc.activeDialog) {
                    if (!holiday) {
                        await dc.beginDialog(HOLIDAY_DIALOG);
                    }
                    else {
                        await turnContext.sendActivity(
                            `An email was sent to your manager for approval`);
                    }
                }

1 个答案:

答案 0 :(得分:1)

要开始,您需要首先在index.js文件中创建并传递userState存储。

const { ConversationState, MemoryStorage, UserState } = require('botbuilder');

[...]

const conversationState = new ConversationState(memoryStorage);
const memoryStorage = new MemoryStorage();
const userState = new UserState(memoryStorage);

[...]

const bot = new ExampleBot(conversationState, userState);

在bot.js文件中,包含并实例化userState并分配用户个人资料:

class ExampleBot {
    constructor(conversationState, userState) {
    [...]

    const USER_PROFILE = 'userProfile';
    this.userProfile = userState.createProperty(USER_PROFILE);
    this.userState = userState;

    [...]
}

现在您可以访问userState。您可以将其作为OnTurn的一部分:

async onTurn(turnContext) {
    if (turnContext.activity.type === ActivityTypes.Message) {
        const userProfile = await this.userProfile.get(turnContext, {});
        const conversationData = await this.conversationData.get(
            turnContext, { promptedForUserName: false });
        if (!userProfile.name) {
            if (conversationData.promptedForUserName) {
                userProfile.name = turnContext.activity.text;
                await turnContext.sendActivity(`Thanks ${userProfile.name}.`);
                conversationData.promptedForUserName = false;
            } else {
                await turnContext.sendActivity('What is your name?');
                conversationData.promptedForUserName = true;
            }
            await this.userProfile.set(turnContext, userProfile);
            await this.userState.saveChanges(turnContext);
        }
    }
}

或作为瀑布/步骤的一部分:

async someWaterfallStep(step) {
        const user = await this.userProfile.get(step.context, {});

        if (!user.name) {
            [do something]
        } else {
            await step.context.sendActivity(`Hello ${user.name}. Nice to meet you!`);
            return step.endDialog()
        }
    }

您可以在此doc中了解有关设置用户状态的更多信息。

希望有帮助!