BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

On browser refresh/ chat reload remove chat logs

Open premaarya opened this issue 7 years ago • 4 comments

Version

I am using Webchat version 4.5.

Describe the bug

With Azure bot service authentication set up following this article and details implementaion here .

When a user sign-in chats and refreshes browser the webchat is reinitilized and user is asked with auth prompt. To solve this scenario, we manage existing token across the browser reload and refresh the token if the token is not expired. The issue with this solution is the user is taken to the last point in the conversation. customer don't want that, it should be new conversation with greeting message without login prompt.

@tdurnford or anyone can you help us?

premaarya avatar Aug 13 '19 00:08 premaarya

Direct Line only sends conversation update events for a member being added to the conversation when the conversation is first initialized. This event is intended to trigger the welcome message from the bot, and, evidently, using a refreshed token will not trigger the traditional welcome message. Since you want to maintain the user's logged in state, I would recommend sending a back channel welcome message event when the Direct Line Connection is fulfilled. Then in the bot you can cancel all existing dialogs to restart the conversation and send the welcome message.

Web Chat v4

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
    // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
    dispatch({
      type: 'WEB_CHAT/SEND_EVENT',
      payload: {
        name: 'webchat/join',
        value: { language: window.navigator.language }
      }
    });
  }
  return next(action);
});
window.WebChat.renderWebChat({
  directLine: window.WebChat.createDirectLine({ token }),
  store
}, document.getElementById('webchat'));

Bot Framework SDK v4 (Node)

this.onEvent(async (context, next) => {
  if (context.activity.name === 'webchat/join') {
    const dc = this.dialogSet.createContext(context);
    await dc.cancelAllDialogs();
    await context.sendActivity({ text: 'Backchannel Welcome Message sent from `onEvent`!' });
  }
  await next();
});

For more details, take a look at the Backchannel Welcome Event Web Chat Sample.

In the future, the Bot Framework Development teams prefers how to questions such as this one to be asked on StackOverflow. The support team monitors StackOverflow for the botframework and web-chat tags.

Hope this helps!

tdurnford avatar Aug 13 '19 15:08 tdurnford

Thank you @tdurnford. I am trying the solution you suggested.

premaarya avatar Aug 13 '19 17:08 premaarya

Direct Line only sends conversation update events for a member being added to the conversation when the conversation is first initialized. This event is intended to trigger the welcome message from the bot, and, evidently, using a refreshed token will not trigger the traditional welcome message. Since you want to maintain the user's logged in state, I would recommend sending a back channel welcome message event when the Direct Line Connection is fulfilled. Then in the bot you can cancel all existing dialogs to restart the conversation and send the welcome message.

Web Chat v4

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
    // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
    dispatch({
      type: 'WEB_CHAT/SEND_EVENT',
      payload: {
        name: 'webchat/join',
        value: { language: window.navigator.language }
      }
    });
  }
  return next(action);
});
window.WebChat.renderWebChat({
  directLine: window.WebChat.createDirectLine({ token }),
  store
}, document.getElementById('webchat'));

Bot Framework SDK v4 (Node)

this.onEvent(async (context, next) => {
  if (context.activity.name === 'webchat/join') {
    const dc = this.dialogSet.createContext(context);
    await dc.cancelAllDialogs();
    await context.sendActivity({ text: 'Backchannel Welcome Message sent from `onEvent`!' });
  }
  await next();
});

For more details, take a look at the Backchannel Welcome Event Web Chat Sample.

In the future, the Bot Framework Development teams prefers how to questions such as this one to be asked on StackOverflow. The support team monitors StackOverflow for the botframework and web-chat tags.

Hope this helps!

Can you write the equivalent for C# SDK also?

alokraj68 avatar Oct 10 '19 12:10 alokraj68

@alokraj68 This how to question is better suited for StackOverflow. Please post your question there with the Web Chat and BotFramework tags so the BotFramework Support Team and the community at large can help you work through your issue.

tdurnford avatar Oct 10 '19 16:10 tdurnford