Lanna
Lanna

Reputation: 115

How to use ITranscriptLogger and TranscriptLoggerMiddleware to store chat transcript in cosmos DB

I want to store the entire chat history in cosmos DB using ITranscriptLogger and TranscriptMiddelWare, but I am struggling to do so. I have read this MS article, but I want to store it in Cosmos DB and not Blob storage. Also I am trying to instantiate the transcript log in Startup.cs not in Bot.cs, and I have tried to implement it according to this answer without any luck. That is, the transcript is not stored and there's no container in my Azure cosmos DB. I appreciate any help and feedback.

Code:

I have created the TranscriptStore class and created and added the middleware as instructed in the referenced SO answer:

CosmosTranscriptStore.cs

public class CosmosTranscriptStore : ITranscriptLogger
    {
        private CosmosDbStorage _storage;

        public CosmosTranscriptStore(CosmosDbStorageOptions config)
        {
            _storage = new CosmosDbStorage(config);
        }
        public async Task LogActivityAsync(IActivity activity)
        {
            // activity only contains Text if this is a message
            var isMessage = activity.AsMessageActivity() != null ? true : false;
            if (isMessage)
            {
                // Customize this to save whatever data you want
                var data = new
                {
                    From = activity.From,
                    To = activity.Recipient,
                    Text = activity.AsMessageActivity().Text,
                };
                var document = new Dictionary<string, object>();
                // activity.Id is being used as the Cosmos Document Id
                document.Add(activity.Id, data);
                await _storage.WriteAsync(document, new CancellationToken());
            }
        }
    }

Startup.cs

 public class Startup
    {

        private const string CosmosServiceEndpoint = "MyCosmosServiceEndpoint";
        private const string CosmosDBKey = "MyCosmosDBKey";
        private const string CosmosDBDatabaseName = "MyCosmosDBDatabaseName";
        private const string CosmosDBCollectionName = "Transcript-storage";
      
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;    
        }
        
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var config = new CosmosDbStorageOptions
            {
                AuthKey = CosmosDBKey,
                CollectionId = CosmosDBCollectionName,
                CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
                DatabaseId = CosmosDBDatabaseName,
            };

            var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Create the Bot Framework Adapter.
            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
            
            // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
            services.AddSingleton<MainDialog>();
            services.AddTransient<IBot, WelcomeBot<MainDialog>>();

            services.AddBot<WelcomeBot<MainDialog>>(options =>
            {
                var middleware = options.Middleware;
                middleware.Add(transcriptMiddleware);
            });

        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseMvc();
        }
    }
}

Upvotes: 5

Views: 1509

Answers (1)

Lanna
Lanna

Reputation: 115

I managed to solve this by adding the transcript store middleware to the adapter, which I probably should have done from the beginning before asking this question, but I am very new to bot framework and this type of programming all together. This is how I solved it:

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            ...
            
            var config = new CosmosDbStorageOptions
            {
                AuthKey = CosmosDBKey,
                CollectionId = CosmosDBAntoherCollectionName,
                CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
                DatabaseId = CosmosDBDatabaseName,
            };

            var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));

            services.AddSingleton(transcriptMiddleware);
            
            ...

        }

AdapterWithErrorHandler.cs

public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
    {
        public AdapterWithErrorHandler(TranscriptLoggerMiddleware transcriptMiddlewareStore, IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger, ConversationState conversationState = null)
            : base(configuration, logger)
        {
            Use(transcriptMiddlewareStore);
            
            OnTurnError = async (turnContext, exception) =>
            {
               ...
            };
        }
    }

In addition, if one wants to store the entire chat transcript in one document/item I would highly recommend storing the data in the CosmosTranscriptStore class by conversation ID instead of activity ID. The reason is that every activity has its own ID thus creating a new item in Cosmos DB for every activity.

 public class CosmosTranscriptStore : ITranscriptLogger
    {
      ...

        public async Task LogActivityAsync(IActivity activity)
        {
            ...

                document.Add(activity.Conversation.Id, data);
                await chatStorage.WriteAsync(document, new CancellationToken());
            
        }
    }

Upvotes: 4

Related Questions