如何使用过期的令牌将WebChat重新连接到旧会话 [英] How to reconnect WebChat to old conversation with an expired token

查看:106
本文介绍了如何使用过期的令牌将WebChat重新连接到旧会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的令牌已过期,我想重新连接到WebChat中的旧机器人对话,如果网络连接丢失或其他原因,可能会发生这种情况.

I want to reconnect to an old bot conversation in WebChat if my token has expired, which could happen if network connection was lost or whatever.

我基本上是这样使用WebChat的:

I am using WebChat essentially like so:

directline = window.WebChat.createDirectLine({ token: token });

window.WebChat.renderWebChat({
   directLine: directline
}, document.getElementById('webchat'));

我使用的令牌

cres = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
   body: JSON.stringify({ User: { Id: userID }, TrustedOrigins: [origin] }),
   headers: {
      authorization: `Bearer ${process.env.DIRECT_LINE_SECRET}`,
      'Content-Type': 'application/json'
   },
   method: 'POST'
});

根据 https://github.com/microsoft/BotFramework-WebChat/blob/master/docs/API.md

createDirectLine带有conversationId参数,所以我尝试传递旧的对话ID

createDirectLine takes a conversationId parameter so I tried to pass the old conversation id

directline = window.WebChat.createDirectLine({ token: token, conversationId: oldConvId });

,其中包含针对同一用户ID的新生成的令牌.但是随后renderWebChat失败并带有无效的令牌,因为新令牌还获得了一个新的conversationId,该新conversationId与directLine调用中请求的不匹配.我怎么解决这个问题?重新连接完全不同,或者为给定的userIdconversationId生成新的令牌.我无法在v3/directline/token/generate API中找到任何东西来为给定的sessionId请求令牌.

with a newly generated token for the same userID. But then renderWebChat fails with invalid token because the new token also got a new conversationId, which doesn't match the one requested in the directLine call. How can I solve this problem? Either do the reconnect completely differently or generate a new token for a given userId and conversationId. I couldn't find anything in the v3/directline/token/generate API to request a token for a given conversationId.

推荐答案

令牌与生成令牌的sessionId紧密相关.不幸的是,令牌失效后(1800秒/30分钟后)便无法刷新它,然后再将原始令牌所代表的令牌重新连接到对话中.

Tokens are closely tied to the conversationId's they are generated with. Unfortunately, once a token expires (after 1800 seconds / 30 mins) there is no option to refresh it to then reconnect to the conversation the original token represented.

如果您希望用户能够重新加入对话,则应该在令牌到期之前通过调用重新连接.这可以通过localStorage/sessionStorage或cookie来完成.

If you want a user to be able to re-join a conversation, then the token should be refreshed before they expire by calling the refresh API /directline/tokens/refresh to obtain a new one. Then you must persist the token and conversationId for use in reconnecting. This can be done via localStorage/sessionStorage or cookies.

在这种情况下,您将设置一个令牌服务器,以便在联系您自己的服务器端点时进行Direct Line API调用.在令牌服务器上设置刷新API后,Web聊天实例将为新令牌调用刷新端点.可以在当前令牌到期之前每25分钟完成一次此操作.然后应保留新令牌.

In this scenario, you will setup a token server to make the Direct Line API calls when your own server endpoints are contacted. Once you have the refresh API setup on your token server, you Web Chat instance will call your refresh endpoint for a new token. This can be done every 25 mins before the current token expires. The new token should then be persisted.

请记住,localStorage仅适用于页面刷新以及浏览和返回Web聊天.如果关闭浏览器,会话将丢失. sessionStorage将在关闭的浏览器会话中持续存在,但是如果闲置时间超过30分钟,则令牌将过期,因为Web聊天未打开以刷新令牌. (如果用户导航超过30分钟,那么对localStorage也可以这样说.)

Bear in mind, localStorage is only good for page refreshes and navigating away from and back to Web Chat. If the browser is closed, the session will be lost. sessionStorage will persist across closed browser sessions, but if the period of inactivity is longer than 30 mins, the token will expire as Web Chat isn't open to refresh the token. (The same can be said about localStorage if the user navigates away for longer than 30 mins.)

如果令牌过期,则另一个选择是检索

If the token is expired, another option is to retrieve the conversation history by use of the expired token and the associated conversationId. You could repopulate Web Chat's transcript with the previous conversation and continue it with a new conversation that, from the user's perspective, looks to be the same. This wouldn't be the easiest method and would require some additional logic to get right, but it is theoretically doable.

最后一个需要考虑的因素,尤其是上述情况,是sessionId仅存储和访问14天,而会话则存储24小时.如果您需要较长时间访问其中任何一个,那么您将需要一种存储解决方案来将它们存储在其中(Cosmos DB,SQL,Mongo等).

One last consideration, in particular to the above, is the conversationId is only stored and accessible for 14 days and the conversation for 24 hours. If you need to access either of these for a longer period, then you will need a storage solution to store them in (Cosmos DB, SQL, Mongo, etc.).

希望有帮助!

这篇关于如何使用过期的令牌将WebChat重新连接到旧会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆