Node.js 使用botbuilder切换时从DirectLine获取401错误
我正在开发一个新的机器人,并试图实现人工切换。我正在使用Directline 3.0在BotFramework WebChat客户端和我的bot(托管在Azure bot服务上)之间进行通信 我的问题是,当我的bot尝试从用户到代理或代理到用户发送消息时,我从Directline API收到401个错误 为了进一步解释这个问题,我们只需要讨论观察状态。在代码中,通过中间件拦截客户消息。如果确定状态为正在监视,则会将客户消息路由到bot和监视代理 从bot发送给用户的消息,使用session.send('foo')发送,工作正常,但从bot发送给代理的消息,发送方式如下:Node.js 使用botbuilder切换时从DirectLine获取401错误,node.js,azure,botframework,direct-line-botframework,Node.js,Azure,Botframework,Direct Line Botframework,我正在开发一个新的机器人,并试图实现人工切换。我正在使用Directline 3.0在BotFramework WebChat客户端和我的bot(托管在Azure bot服务上)之间进行通信 我的问题是,当我的bot尝试从用户到代理或代理到用户发送消息时,我从Directline API收到401个错误 为了进一步解释这个问题,我们只需要讨论观察状态。在代码中,通过中间件拦截客户消息。如果确定状态为正在监视,则会将客户消息路由到bot和监视代理 从bot发送给用户的消息,使用session.se
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实现中发现了这个标志,它似乎决定(正如您所猜测的)消息在发送时是否使用授权请求。您可以在我上面的代码中看到,我可以自己设置这个标志,但这是一个黑客解决方案,它似乎绕过了真正的应用程序逻辑
这整件事让人感觉不对劲,我觉得我遗漏了什么,但如果不直接设置useAuth
,我就不知道如何让这些请求工作
下面是我的切换客户端的一些相关代码。我从下面删除了一些代码,以便在会话处于等待状态时只关注来自客户的消息
我认为您现在将无法使用
useAuth
标志。这里有一些公开的示例,但即使文档中的节点支付机器人也使用标志来验证请求:。希望微软的人很快会来回答更详细的问题。
/* 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();
}
}