Upload/Download Bot Framework Composer Conversation/Chat

Introduction

Using the Microsoft.Bot.Builder.Azure.Blobs NuGet package, we can store and retrieve conversation data for your Bot Framework Composer bot in Azure Blob Storage. This can enable you to achieve upload and download functionality for conversations/chats. Below are the general steps to implement this in your bot:

Step 1: Install NuGet Package

In your Bot Framework Composer bot project, install the Microsoft.Bot.Builder.Azure.Blobs NuGet package:

dotnet add package Microsoft.Bot.Builder.Azure.Blobs

Step 2: Configure Azure Blob Storage

  1. Create an Azure Blob Storage account if you don’t have one.
  2. Obtain the connection string for your Azure Blob Storage account.

Step 3: Configure the Bot with Blob Storage

In your bot’s Startup.cs or wherever you’re configuring your bot, set up the BlobStorage as part of the bot’s storage:

using Microsoft.Bot.Builder.Azure.Blobs;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Extensions.Configuration;

// ...

public void ConfigureServices(IServiceCollection services)
{
    // ...

    var blobStorageOptions = new BlobStorageOptions
    {
        ConnectionString = Configuration.GetConnectionString("BlobStorageConnectionString"),
        ContainerName = "your-container-name" // Replace with your container name
    };

    services.AddSingleton<IStorage>(new BlobsStorage(blobStorageOptions));

    // ...
}

Step 4: Implement Upload/Download Logic

Now you can implement your own logic to upload and download conversation data. Here’s an example:

using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;

public class YourBot : ActivityHandler
{
    private readonly ILogger<YourBot> _logger;
    private readonly IStorage _storage;

    public YourBot(ILogger<YourBot> logger, IStorage storage)
    {
        _logger = logger;
        _storage = storage;
    }

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        var conversationReference = turnContext.Activity.GetConversationReference();

        // Upload conversation data
        using (var convoState = new ConversationState(_storage))
        {
            var convoData = convoState.CreateProperty<YourConversationData>("yourConvoDataKey");
            await convoData.SetAsync(turnContext, new YourConversationData { /* ... your data ... */ });
            await convoState.SaveChangesAsync(turnContext, false, cancellationToken);
        }

        // ... Handle message and other logic ...

        // Download conversation data
        using (var convoState = new ConversationState(_storage))
        {
            var convoData = convoState.CreateProperty<YourConversationData>("yourConvoDataKey");
            var data = await convoData.GetAsync(turnContext, () => new YourConversationData(), cancellationToken);
            // Use the downloaded data...
        }
    }
}

In this example, replace YourConversationData it with your actual data model. The ConversationState class helps you manage conversation-specific state data.

Remember that handling upload and download can involve more complex logic based on your use case, and you’ll need to adapt this example to your specific requirements.

Always ensure that you handle sensitive data securely and follow best practices for data protection when using Azure Blob Storage or any other data storage solution.

Leave a Reply

Your email address will not be published. Required fields are marked *