how to get the turncontext in startup middleware - bot v4

Mahesh C M 151 Reputation points
2020-09-16T04:48:51.217+00:00

I am working with bot framework v4. Is there any way to get ITurncontext in startup.cs to set up the transcriptlogger middleware for saving the bot state.

Below given the sample solution i have tried

        IStorage userDataStore = new 
        Microsoft.Bot.Builder.Azure.AzureBlobStorage
        (Configuration["AzureTableStorageConnectionString"], 
        Configuration["AzureBlobStorageContainerName"]);

        var userstate = new UserState(userDataStore);

        var myPropertyAccessor = userstate.CreateProperty<UserProfile>(nameof(UserProfile));

        services.AddSingleton<IStatePropertyAccessor<UserProfile>>(myPropertyAccessor);

        var logger = new TranscriptLogger(Configuration);
        services.AddTransient<ITranscriptLogger, TranscriptLogger>();
        services.AddBot<DialogAndWelcomeBot<Dialog>>(options =>
        {
            options.CredentialProvider = new ConfigurationCredentialProvider(Configuration);

            options.Middleware.Add(new TranscriptLoggerMiddleware(logger));

        });

Implementation of TranscriptLogger :-

               public class TranscriptLogger : ITranscriptLogger
               {
                           public IConfiguration _configuration;
                          public TranscriptLogger(IConfiguration configuration)
                         {
                              _configuration = configuration;
                          }
                         public async Task LogActivityAsync(IActivity activity)
                         {
                          var isMessage = activity.AsMessageActivity() != null ? true : false;

                           if (isMessage && !string.IsNullOrEmpty(activity.AsMessageActivity().Text))
                           {

                               // saving conversation data
                           }
                      }
                 }

I really appreciate any help :)

Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
770 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,641 Reputation points
    2020-09-17T05:02:03.31+00:00

    @Mahesh C M Thanks, Can you please provide link to the sample that you are trying also please add more details about the use case of ITurnContext in Startup. Are you trying how to get transcript logger middleware working?.

    Also please follow the below link to save user and conversation data and to use state management and storage objects to manage and persist state.

    https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/csharp_dotnetcore/45.state-management/Startup.cs

    1 person found this answer helpful.