新手Node.机器人与数据库查找的集成(寻找最佳实践)



OK,Node.js和botframework的新手。 我使用 Azure 站点构建了第一个机器人并下载了代码。 选择"路易斯"集成机器人模板。

我(终于)理解了node的事件驱动模型.js以及回调的概念。

我在下面有代码片段。 当 Luis 找到"帮助"的意图时,它会触发此函数。 反过来,我有数据库调用来查找实体。 在数据库中,我有一个实体,响应(如果实体是瓶子,则回答"回收")。

我也有该代码。

在第一个块下面是一个函数句柄帮助请求者,它是回调函数,我也在工作。 我有点卡住的地方(最佳实践)是在这个回调函数中,我想向会话对象发送一些东西(session.send 朝函数底部)。

由于我不直接创建会话对象,因此我不确定选项。 我是否应该将会话对象传递给数据库函数,然后将其传回?

我是否应该创建一个全局变量并将其设置为会话对象(我担心如果多人使用此机器人,那么这种方法将不起作用)。

我愿意接受建议。

谢谢

.matches('Help', (session, args) => {
var entities = args.entities;
var itype = builder.EntityRecognizer.findEntity(args.entities, 'ItemTypes');

var respondToUser = '';
var msg = 'Initialized';
// if there is an entity provided, perform a lookup of the entity.
if (itype.entity !== null) {
//Use session.sendTyping so the user thinks something is happening rather than being ignored while we do the lookup in SharePoint.
session.sendTyping();
//perform lookup
respondToUser = sp.lookupEntity(itype.entity, handleHelpRequest);
};
})
function handleHelpRequest(err, respondToUser) {
var msg = 'uninitialized';
if (err = 'success') {
console.log('Respond to user from matches:'.concat(respondToUser));
//return from lookup
if (respondToUser === 'No match found') {
msg = 'I think you are asking for help, but I don't understand what you need help with.';
}
else {
msg = 'I can help you with that, '%s'.', respondToUser;
}

console.log(msg);
session.send(msg);
//The following two lines are for debugging
session.send('How may I assist you? ' + JSON.stringify(args));
session.send('Value of entity you said:  '%s'.', itype.entity);
}
else {
console.log('an error occurred');
}
}

如果要访问会话对象,请将其作为参数传递给帮助程序函数。

例:

function handleHelpRequest(session, err, respondToUser) {
// do session stuff
}

最新更新