如何在v4节点SDK BOT上保留对话框堆栈

时间:2019-03-22 14:59:52

标签: node.js botframework

我已经习惯了v3节点botbuilder sdk,所以我有一个中间件,可以在其中查看对话框堆栈并按以下步骤进行所需的操作。

了解对话框堆栈的V3中间件:-

bot.use({
    botbuilder: function (session, next) {

        if (session.dialogStack()&& session.dialogStack().length <= 0 ) {
            // Do something is dialog stack is empty.
        }

    },
    send: function (event, next) {
        if (event.type != "typing" && event.type != "endOfConversation") {
            logUserConversation("Botoutput", event);
        }
        next();
    }
});

我需要使用对话框堆栈执行某些操作的V4中间件。

adapter.use(async (turnContext, next) => {
            // pre-processing of the current incoming activity
            turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
                // console.log(`pre-processing of outgoing activities`);
                await nextSend();

         ***//Need to know the dialog stack here.***


            });
            await next();
        });

我在turnContext对象上查找,但是没有显示是否显示对话框堆栈。我可以看到DialogContext对象具有'stack'属性,但是不确定如何在中间件中使用。

1 个答案:

答案 0 :(得分:0)

您只需要添加activity.filter方法来检索通过的数据,就可以了。

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

adapter.use(async (turnContext, next) => {

    const userActivity = turnContext.activity;
    if (userActivity.from.role === 'user' && turnContext.activity.text.length > 0) {
        console.log('From user: ', userActivity);
    }

    turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
        await nextSend();

        activities.filter(a => a.type !== 'typing' && a.type !== 'endOfConversation').forEach(a => console.log('From bot: ', a));
    });

    await next();
});

希望有帮助!