WebChat using direct line unable to refresh token or reconnect
See original GitHub issueHi, on our project we’re using webchat that is connecting to our bot through direct line without web sockets so we are able to restore chat history. After 15-30 minutes of inactivity when user tries to send message it immediately shows couldn’t sent. I’ve tried to use reconnect API however looking into source code it seems that it’s working only with web sockets enabled. I’ve tried to implement following options but neither of them is working and token always expires resulting on 403 error on token refresh.
var botConnection = new BotChat.DirectLine({
secret: model.directLineSecret,
webSocket: false,
conversationId: getPersistedConversationId(),
domain: model.chatUrl
});
BotChat.App({
botConnection: botConnection,
user: {
id: model.userId,
name: model.userName,
},
},
window.document.getElementById('chat'));
botConnection.connectionStatus$
.subscribe(function(status){
handleConnection(status)
});
//try to refresh token every 50 seconds
window.setInterval(function (){
console.log("About to refresh token");
$.ajax({
method: "POST",
url: "https://directline.botframework.com/v3/directline/tokens/refresh",
headers: {
"Authorization" : "Bearer " + botConnection.token
}
}).done(function(result){
botConnection = new BotChat.DirectLine({
token: result.token,
conversationId: getPersistedConversationId(),
webSocket: false,
domain: model.chatUrl
});
BotChat.App({
botConnection: botConnection,
user: {
id: model.userId,
name: model.userName,
},
},
window.document.getElementById('chat'));
botConnection.connectionStatus$
.subscribe(function(status){
handleConnection(status)
});
});
},50000);
function handleConnection(connectionStatus){
switch(connectionStatus){
case 0:
console.log("Uninitialized");
break;
case 1:
console.log("Connecting");
break;
case 2:
console.log("Online")
saveConversationId(botConnection.conversationId);
break;
case 3:
console.log("ExpiredToken");
botConnection = new BotChat.DirectLine({
secret: model.directLineSecret,
webSocket: false,
conversationId: getPersistedConversationId(),
domain: model.chatUrl
});
BotChat.App({
botConnection: botConnection,
user: {
id: model.userId,
name: model.userName,
},
},
window.document.getElementById('chat'));
botConnection.connectionStatus$
.subscribe(function(status){
handleConnection(status)
});
break;
case 4:
console.log("FailedToConnect");
break;
case 5:
console.log("Ended");
break;
}
}
Token refresh does not throw any error but after mentioned interval Expired token is triggered and even if new connection is created it fires request to refresh endpoint resulting 403 error - this method can be used only with existing token.
I’ve looked at the issue https://github.com/Microsoft/BotFramework-WebChat/issues/372
and used c# code to return new conversation object and reconnected, after that message seems to be sent even after timeout however no response from the bot is displayed.
Can anyone point me to the right direction on how to properly manage token of direct line?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Secrets don’t expire.
Tokens do expire, but are automatically renewed by DirectLineJS (unless there’s a break in connection). This renewal is enabled by passing
{ token }
when creating DirectLine.@billba
Secrets don't expire. Tokens do expire, but are automatically renewed by DirectLineJS (unless there's a break in connection). This renewal is enabled by passing { token } when creating DirectLine.
Is this DirectLine default behaviour, or will the token only be generated if we are using webchat?
We are building a custom interface using directlinejs and the token is not being refreshed, so thinking that we need to do this manually.
We are not using web-chat, we have built our own react front end and using directlineJs.
We have an API that retrieves a token through https://directline.botframework.com/v3/directline/conversations. Upon API response we connect to DirectLine.
Everything works fine, however the token dies in 30 minutes and the conversation locks up.
What we are now trying to do is send a refresh message to https://directline.botframework.com/v3/directline/tokens/refresh passing in our original token, we get a new token back but are unsure what to do next?