Forum Discussion

soulglow's avatar
soulglow
Copper Contributor
Nov 14, 2022

Teams bot conversation history confusion

We are developing a teams tab + bot app. All in one. We send adaptive cards a few times a day to the user but are confused how bot conversations are stored. No matter what we remove they show up as we keep installing and iterating new teams app versions.

 

For example, we used to have this code

const adapter = new BotFrameworkAdapter(adapterOptions)

const memoryStorage = new MemoryStorage()

const conversationState = new ConversationState(memoryStorage)

const bot = new TeamsActivityService(conversationState)

 

We thought, oh great, we're using memory storage so if we remove it, reinstall the app for the user, everything will disappear but it doesn't, conversations get reloaded.

 

We now have something like this:

const adapter = new BotFrameworkAdapter(adapterOptions)

const bot = new TeamsActivityService()

...

adapter.continueConversation(conversationReference, async () => {

 bot.sendActivity()

})

 

export class TeamsActivityService extends TeamsActivityHandler {

  constructor() {

    super()

  }

 ...

}

 

We ultimately want to control conversation history and create a custom storage instead of MemoryStorage but we don't even understand how history is stored, controlled and populated. Without memory storage, it works fine, With it, it works fine. How do we control where it's coming from? How do we clear it? Please help

 

  • soulglow -The Bot Framework provides a service for tracking the context of a conversation, called Bot Framework State. It enables you to store and retrieve data associated with a user, conversation or a specific user within the context of a conversation.

    By default, Bot Framework uses the Bot Framework State to store conversation data. It is designed for prototyping, and is useful for development and testing environments. At the time of this writing, it has only a size limit of 32KB. Learn more about data management.

    For production environments, it’s highly recommended to use a NoSQL database to store data as documents, such as Azure Cosmos DB. To customize your bot conversation data storage, you can use the Bot Builder SDK Azure Extensions. If you are developing your bot with Bot Builder SDK in C#, you have to edit the Global.asax file:

     

    protected void Application_Start()
    {
         // Adding DocumentDB endpoint and primary key
         var docDbServiceEndpoint = new Uri("<your documentDB endpoint>");
         var docDbKey = "<your documentDB key>";
    
        // Creating a data store based on DocumentDB
         var store = new DocumentDbBotDataStore(docDbServiceEndpoint, docDbKey);
    
        // Adding Azure dependencies to your bot (documentDB data store and Azure module)
         var builder = new ContainerBuilder();
    
        builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));           
        
         // Key_DataStore is the key for data store register with the container
         builder.Register(c => store)
             .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
             .AsSelf()
             .SingleInstance();
    
        // After adding new dependencies, update the container
         builder.Update(Conversation.Container);
    
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

     

    If you run your bot and open your Cosmos DB service on Azure Portal, you can see all stored documents (clicking on Data Explorer).

    Reference Document-1.Write directly to storage - Bot Service | Microsoft Learn
    2.Bot conversation history with Azure Cosmos DB | Azure Blog and Updates | Microsoft Azure

    • soulglow's avatar
      soulglow
      Copper Contributor

      Sayali-MSFT thank you but we've seen these links before. A few follow-up questions:

      1) we need an example in javascript/typescript if you have one

      2) how come if we remove all traces of doing anything with storage, we still see the conversation history? this piece hasn't been answered. Are you saying it stores it in cosmosdb autotmatically? we don't want to store in cosmos, we want to write a custom one but need to understand how it works if we don't specify anything as we're seeing history without any code as mentioned in the original post. Thanks.

Resources