【发布时间】:2019-11-28 07:55:27
【问题描述】:
我有一个要主动调用的通知对话框...
例如
Bot: Hi, you have an event scheduled in next 15 mts... blah blah
Bot: would you want me to email the details?
User input: yes/no
Bot: Great!
这是一个简单的瀑布对话框... step1 通知并提示确认。 第2步。处理用户输入..
现在这个对话是主动发起的。 第 1 步有效。 但是,dialogContext / dialogStack 没有被保存,当用户说是时,它将进入主 dailog,而不是应该在堆栈顶部的这个主动对话框。
基本上,activityHandler 上的 onDialog 事件之类的 activityHandler 方法都不会被调用来进行主动对话。
问题是如何让来自主动 Dialog 的消息通过 activityHandler 方法以使 dialogStack 持久化?
我使用 nodejs。
使用下面的代码示例进行更新 // 中间件
const { ActivityTypes } = require('botbuilder');
class MyMiddleware {
async onTurn(context, next) {
await context.onSendActivities(async (context, activities, nextSend) => {
console.log(`messages: ${activities.map( a => a.text).join(',')}`)
return await nextSend();
});
// By calling next() you ensure that the next Middleware is run.
return await next();
};
}
module.exports.MyMiddleware = MyMiddleware;
// 主机器人。
const { ActivityHandler, TurnContext } = require('botbuilder');
class ProactiveBot extends ActivityHandler {
constructor(conversationReferences) {
super();
this.conversationReferences = conversationReferences;
this.onConversationUpdate(async (context, next) => {
this.addConversationReference(context.activity);
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
const welcomeMessage = 'Welcome to the Proactive Bot sample. Navigate to http://localhost:3978/api/notify to proactively message everyone who has previously messaged this bot.';
await context.sendActivity(welcomeMessage);
}
}
await next();
});
this.onMessage(async (context, next) => {
this.addConversationReference(context.activity);
await context.sendActivity(`You sent '${ context.activity.text }'`);
await next();
});
this.onDialog(async (context, next) =>{
console.log(`I am called`)
})
}
addConversationReference(activity) {
const conversationReference = TurnContext.getConversationReference(activity);
this.conversationReferences[conversationReference.conversation.id] = conversationReference;
}
}
module.exports.ProactiveBot = ProactiveBot;
//索引
const bot = new ProactiveBot(conversationReferences);
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Route to main dialog.
await bot.run(context);
});
});
server.get('/api/notify', async (req, res) => {
for (const conversationReference of Object.values(conversationReferences)) {
await adapter.continueConversation(conversationReference, async turnContext => {
await turnContext.sendActivity('proactive hello');
});
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
res.end();
});
当我调用通知 api 时,我希望调用 onDialog 事件...并打印“我被调用”。但这不会打印到控制台。
【问题讨论】:
-
尝试做 conversationState.saveChanges(context, false) 但没有保存 dialogState.. 注意:dialogState 是 conversationState 的一个属性。
-
解决方法:对于每个提示步骤,在提示步骤之后立即执行 conversationState.saveChanges(context, true) 保存...
标签: node.js botframework