我正在研究一种新的机器人并尝试实施人工切换 . 我正在使用Directline 3.0在BotFramework-WebChat客户端和我的bot(在Azure Bot服务上托管)之间进行通信 .
我的问题是,当我的机器人试图从用户向代理或代理发送消息给用户时,我从Directline API收到401错误 .
为了进一步解释这个问题,我们只需要讨论观察状态 . 在代码中,客户消息通过中间件拦截 . 如果确定状态为正在观看,则客户消息被路由到机器人和观察代理 .
从机器人到用户的消息,与 session.send('foo')
一起发送的工作正常,但是从机器人到代理的消息,发送方式如下:
address = conversation.agent;
// the commented codes makes the whole thing work...
// address.useAuth = true;
agentMessage = new builder.Message()
.address(address)
.text(message.text);
bot.send(agentMessage, (err) => {
console.log(err);
});
除非我添加一个 useAuth
标志,否则不起作用 . 我在ChatConnector.js实现中找到了这个标志,它似乎决定(正如你猜的那样)消息在发送时是否使用了授权请求 . 您可以在上面的代码中看到我可以自己设置此标志,但这是一个hacky解决方案,它似乎绕过了真正的应用程序逻辑 .
这整件事感觉不对,我觉得如果没有直接设置 useAuth
,我就会明白如何使这些请求工作 .
这里's a bit of the relevant code from my handoff client. I'已从下面删除了一些代码,仅在会话处于等待状态时关注来自客户的消息 .
/* this middleware is used by the bot */
public routingMiddleware(bot: builder.UniversalBot) {
return {
//intercepts messages from user to bot
botbuilder: (session: builder.Session, next: Function) => {
// Pass incoming messages to routing method
if (session.message.type === 'message') {
this.routeMessage(session, bot, next);
} else {
// allow messages of non 'message' type through
next();
}
}
}
}
private routeMessage(session: builder.Session, bot: builder.UniversalBot, next: Function) {
this.routeCustomerMessage(session, bot, next);
}
private async routeCustomerMessage(session: builder.Session, bot:builder.UniversalBot, next: Function) {
const message = session.message;
let agentMessage: builder.Message;
let address: any;
// method will either return existing conversation or a newly created conversation if this is first time we've heard from customer
const conversation = await this.getConversation({ customerConversationId: message.address.conversation.id }, session);
// log the user's message to persistent storage
await this.addToTranscript({ customerConversationId: conversation.customer.conversation.id }, message, session);
switch (conversation.state) {
case ConversationState.Watch:
address = conversation.agent;
// the commented codes makes the whole thing work...
// address.useAuth = true;
agentMessage = new builder.Message()
.address(address)
.text(message.text);
bot.send(agentMessage, (err) => {
console.log(err);
});
return next();
}
}