howdyai / botkit

Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
MIT License
11.44k stars 2.28k forks source link

Question: How to persist current conversation state while connecting to Botkit using Websockets (botkit-web-adapter) #2131

Closed cloudx9 closed 3 years ago

cloudx9 commented 3 years ago

Hello,

I'm having a question/issue. I'm using botkit web adapter to connect to botkit using Websockets for my apps,

The problem is if the Websocket for some reason closed or had an error. I have to start the conversation from the beginning is there any way that I can persist the current state of the conversation (steps & variables) so when the Websocket reconnects I don't have to start the conversation from the beginning ?

I'm currently sending a user's ID upon beginning a new conversation can we use that to persist the current convo state ?

pgoldweic commented 3 years ago

You will need to implement the botbuilder storage interface (https://docs.microsoft.com/en-us/javascript/api/botbuilder-core/storage?view=botbuilder-ts-latest) to get persistence of the conversation state (and user's context). You can use one of the community implementations at https://github.com/BotBuilderCommunity/botbuilder-community-js#storage . I have used the dynamo implementation myself for my bot. BTW, my understanding is that the storage implementation is independent of whether you use websockets or not.

cloudx9 commented 3 years ago

You will need to implement the botbuilder storage interface (https://docs.microsoft.com/en-us/javascript/api/botbuilder-core/storage?view=botbuilder-ts-latest) to get persistence of the conversation state (and user's context). You can use one of the community implementations at https://github.com/BotBuilderCommunity/botbuilder-community-js#storage . I have used the dynamo implementation myself for my bot. BTW, my understanding is that the storage implementation is independent of whether you use websockets or not.

Thank you so much for your suggestion. I'm not sure how to implement botbuilder storage inside botkit ? if you can give me and example of how to do so I'd really appreciate it.

pgoldweic commented 3 years ago

There is documentation on this in the 'Enable Conversation Persistence' section of this botkit documentation page: https://botkit.ai/docs/v4/core.html including a code example of how to connect it to botkit. Note that you will need to create an external database that will hold this conversation state (at least AWS dynamo, Mongo DB and possibly Redis are some of the databases for which storage implementations already exist and can be found at the URL I shared above), and the storage providers -offered in the community site- manage the access to the database while implementing the required storage protocol. Fortunately there's not much you need to know about the details of these implementations in order to use them and get conversation state persistence in place (one exception would be if you actually wanted to retrieve/save this state directly for any reason in your code, which is not necessary at all to have the library do its job though). Finally, note that this storage is only for conversation state (dialog state + user context) and NOT conversation history.

cloudx9 commented 3 years ago

Thank you @pgoldweic so much, I'v successfully implemented the storage using @botbuildercommunity/storage-mongodb

// Grab a collection handle off the connected client
if (process.env.MONGO_URI) {
  mongoDbStorage = new MongoDbStorage(
    process.env.MONGO_URI || "",
    "botkitdb",
    "testCollection"
  );
}
export const botCtrl = new Botkit({
  webhook_uri: "/api/messages",
  adapter: adapter,
  storage: mongoDbStorage,
});

for later, it is just confusing that https://github.com/howdyai/botkit-storage-mongo doesnt work as documented in the README file so you have to use @botbuildercommunity/storage-mongodb instead. ps: I couldn't make botkit-storage-mongo work so it might be my fault tho I'm not sure.