使用botbuilder越区切换时,从DirectLine获取401错误

我正在研究一个新的机器人,并试图实现人工切换。 我正在使用Directline 3.0在BotFramework-WebChat客户端和托pipe在Azure Bot Service上的我的机器人之间进行通信。

我的问题是,当我的机器人尝试从用户发送消息到代理或代理到用户时,我从Directline API中收到401错误。

为了进一步解释这个问题,我们只需要讨论观察状态。 在代码中,客户消息通过中间件被拦截。 如果状态被确定为正在观看 ,则将消息消息路由到机器人和观看代理。

从bot发送给用户的消息,使用session.send('foo')发送,但工作正常,但是从bot到agent的消息发送如下:

 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实现中find了这个标志,它似乎决定了(正如你猜测的那样)消息在发送时是否使用授权的请求。 你可以在我的上面的代码中看到我可以自己设置这个标志,但是这是一个冒险的解决scheme,它似乎是绕过了真正的应用程序逻辑。

这整个事情感觉不对,我觉得我错过了一些东西,但我不知道如何使这些请求工作,而无需直接设置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(); } }