Upload/Download Bot Framework Composer Conversation/Chat

If you are working on Bot Framework Composer then challenging works you will find is about storing and retrieving conversation history.

Using Bot framework SDK, it is easy but complexity is there using composer.

While I was implementing, trust me I found very few articles and content to store/retrieve conversation.

Here I will mention all the points that can help you to preserve and download conversation history.

Pre-requisite

  • Azure Portal access or Azurite

Step 1

Create a storage account in portal and create container

Step 2

Now, Follow some changes that are mentioned below,

<PackageReference Include="Microsoft.Bot.Builder.Azure.Blobs" Version="4.16.1" />

Add package to your project file,

public class Startup {
    private readonly BlobsTranscriptStore _blobTranscripts;
    public Startup(IConfiguration configuration) {
        Configuration = configuration;
        _blobTranscripts = new BlobsTranscriptStore(Configuration.GetValue < string > ("StorageConnectionString"), Configuration.GetValue < string > ("ContainerName"));
    }
    public void ConfigureServices(IServiceCollection services) {
        services.AddSingleton < ITranscriptStore > (_blobTranscripts);
        // Create the TranscriptLoggerMiddleware to log all activities to the TranscriptStore
        services.AddSingleton < IMiddleware, TranscriptLoggerMiddleware > (serviceProvider => {
            return new TranscriptLoggerMiddleware(_blobTranscripts);
        });
    }

Please note that add namespaces for above classes, I will leave up to you.

Now using above code, all your bot conversation starts storing on blob container.

Step 3

Now comes, the download of conversation part. Remember bot store JSON file for each of your message. so there will be lots of JSON get stored. To retrieve all transcript files of your conversation, you need channeled and conversationid.

var blobsTranscriptStore = new BlobsTranscriptStore("StorageConnectionString", "ContainerName");
string continutionToken = null;
List <IActivity> activitiesList = new List <IActivity> ();
do {
    var activities = await blobsTranscriptStore.GetTranscriptActivitiesAsync(ChannelId, ConversationId, continutionToken).ConfigureAwait(false);
    continutionToken = activities.ContinuationToken;
    activitiesList.AddRange(activities.Items);
} while (continutionToken != null);

Now you will find all conversation in activitiesList.

Step 4

Now you will find actual message in specific files by filtering using activity type.

foreach(Activity item in activities.Where(x => x.Type == ActivityTypes.Message).Cast<Activity> ()) {
    if (!string.IsNullOrEmpty(item.Text)) {
        Console.WriteLine($ "{item.Timestamp}   {item.From.Id} : {item.Text}");
    }
}

Now I will leave up to you whether you prepare text file or .csv file to store your conversation.

Definitely, you need to implement some of your logic here to get all messages. But I don't think it is too difficult.

Step 5

Now you can store this conversation file to blob storage again and allow it to download or you can directly allow user to download from here, the choice is your as per your requirement.

Hope the above article is helpful to you and you can customize this logic as my intention here is to provide you with core logic for this.

Keep learning, Keep Implementing.


Similar Articles