如何在V4节点SDK bot上保留对话框堆栈



我已经习惯了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对象具有"堆栈"属性,但不确定如何在中间件中使用。

您只需要添加活动。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();
});

希望!

最新更新