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

How to preserve conversation state

Open raghukrishnamoorthy opened this issue 7 years ago • 12 comments

I've looked around there seem to be a lot of confusing answers over a couple of years. I will ask again.

My use case is simple. I have a bot rendered using webchat control. Everything works fine. When i navigate to a new page on my application which also had the webchat control embedded, I want to resume from where the conversation was left off in the previous window. Right now, the conversation starts from the beginning in the webchat window.

Can this be done?

I'm under a very tight deadline and any help would be appreciated.

raghukrishnamoorthy avatar Jan 31 '19 16:01 raghukrishnamoorthy

You can, just use the redux storage that is importable from the botframework and keep it in local storage. Like this:

import { createStore, createDirectLine } from 'botframework-webchat';
...
chatStore = createStore(
      onErrorResumeNext(() => {
        const reduxStore = window.localStorage.getItem(REDUX) ? window.localStorage.getItem(REDUX) : "{}";
        return JSON.parse(reduxStore as string);
      }),
      ({ dispatch }: any) => (next: any) => (action: any) => {
       //ADD ACTION CHECKER IF YOU WANT TO INTERCEPT ANY ACTIONS
          });
        }
        return next(action);
      }
    );

    chatStore.subscribe(() => {
      window.localStorage.setItem(REDUX, JSON.stringify(chatStore.getState()));
    });

Good luck with your deadline ;) if you are really out of time and need fast advice ping me on my mail

ljubomirsinadinovski avatar Jan 31 '19 17:01 ljubomirsinadinovski

Sorry cannot use React / Redux. Just plain JS with Webchat control is what we've got now. :(

raghukrishnamoorthy avatar Jan 31 '19 17:01 raghukrishnamoorthy

Just to be clearer. I need to somehow persist the same conversationId to get related user data from the backend when the user opens the HTML page with the webchat control in multiple tabs.

Also, the webchat control will come from the CDN.

raghukrishnamoorthy avatar Jan 31 '19 17:01 raghukrishnamoorthy

@raghukrishnamoorthy you can refer to this sample for resuming a conversation. The idea is that when the user connects, the bot sends the user back the entire history.

But on the other hand, you will need to provide your own storage for the conversation history. This raises concerns regarding GDPR though, and your team may have to do further research into the ramifications of this.

@ljubomirsinadinovski provided a client-side solution. Handling GDPR issues using this method is more complicated than a server-side solution. For example, when the user clicks sign out on a page without Web Chat, you will need to find a way to remove the conversation history from the browser storage.

corinagum avatar Jan 31 '19 19:01 corinagum

@corinagum Thank you very much. I'll see if this works out for me. Cheers!

raghukrishnamoorthy avatar Feb 04 '19 18:02 raghukrishnamoorthy

@corinagum

The sample does not work for me.

I downloaded the sample and plugged in the blob storage config details. When i run the program I can see that the chat history is saved to the blob storage. However

var pagedTranscript = await _transcriptStore.GetTranscriptActivitiesAsync(activity.ChannelId, activity.Conversation.Id, continuationToken);
                        var activities = pagedTranscript.Items
                            .Where(a => a.Type == ActivityTypes.Message)
                            .Select(ia => (Activity)ia)
                            .ToList();

                        // DirectLine only allows the upload of at most 500 activities at a time. The limit of 1500 below is
                        // arbitrary and up to the Bot author to decide.
                        count += activities.Count();
                        if (activities.Count() > 500 || count > 1500)
                        {
                            throw new InvalidOperationException("Attempt to upload too many activities");
                        }

                        var transcript = new Transcript(activities);

                        await connectorClient.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, transcript, cancellationToken: cancellationToken);


does not seem to do anything. I can see that the activities are stored in the activities variable but the following line does not seem to do anything.

                        await connectorClient.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, transcript, cancellationToken: cancellationToken);

I'm not getting any exceptions either and I'm using Bot emulator v4 to test the bot out.

raghukrishnamoorthy avatar Feb 05 '19 06:02 raghukrishnamoorthy

@corinagum @raghukrishnamoorthy I have the same issue. I need the solution for this. ragkukris did you store the conversation to blobstorage like this? Thanks storeItems["UtteranceLog"].UtteranceList.push(utterance);

https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0&tabs=javascript

khmerforce avatar Feb 06 '19 22:02 khmerforce

@raghukrishnamoorthy sorry for the late reply. If the sample does not work for you, please feel free to file a bug in the BotBuilder-Samples repo. Alternatively, if you need further help with building your bot, please consider asking for help on Stack Overflow, where the community at large can help answer your questions. :)

corinagum avatar Feb 06 '19 23:02 corinagum

@khmerforce I am using the code at https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/22.conversation-history

If you go through the sample, whatever is there already takes care of storing and retrieving the conversation history when you pass in the conversation ID.

The only problem is with the sendconversationhistory method which does not seem to do anything. I'm manually looping through each activity and sending it.

raghukrishnamoorthy avatar Feb 07 '19 05:02 raghukrishnamoorthy

@raghukrishnamoorthy I am using node.js. Do you know where I can find the nodejs version?

khmerforce avatar Feb 11 '19 02:02 khmerforce

@khmerforce Sorry, I cannot find a nodejs sample for the conversation history workflow. I'll tell you what I'm doing. I created a blob storage account on Azure. After that, I configure the middleware like shown in the .NET example I'm using above. This intercepts all incoming and outgoing messages and pushes it to blob storage. When i retrieve it, I use the conversationId to get the relevant history from blob storage. The result is a bunch of activity objects that i manually send to the webchat control.

I'm not sure what is the nodejs equivalent. Your best bet is to file an issue and see if someone can help you out.

Good luck.

raghukrishnamoorthy avatar Feb 11 '19 04:02 raghukrishnamoorthy

You can, just use the redux storage that is importable from the botframework and keep it in local storage. Like this:

import { createStore, createDirectLine } from 'botframework-webchat';
...
chatStore = createStore(
      onErrorResumeNext(() => {
        const reduxStore = window.localStorage.getItem(REDUX) ? window.localStorage.getItem(REDUX) : "{}";
        return JSON.parse(reduxStore as string);
      }),
      ({ dispatch }: any) => (next: any) => (action: any) => {
       //ADD ACTION CHECKER IF YOU WANT TO INTERCEPT ANY ACTIONS
          });
        }
        return next(action);
      }
    );

    chatStore.subscribe(() => {
      window.localStorage.setItem(REDUX, JSON.stringify(chatStore.getState()));
    });

Good luck with your deadline ;) if you are really out of time and need fast advice ping me on my mail

Please provide the full code.

iamkarantalwar avatar Sep 11 '23 18:09 iamkarantalwar