title: How can i save botframework data like questions and answers
body: <p>In <code>MessagesController.cs</code> in my code I added a part to save userlog data like userid,username, channel, date, messages...I want to save also the answer of the bot ? but I don't know how to do. The database is in azure.</p>
  
  <p>This is the code : MessagesController.cs</p>
  
  <pre><code>    using System.Net.Http;
     using System.Threading.Tasks;
       using System.Web.Http;
    using Microsoft.Bot.Builder.Dialogs;
   using Microsoft.Bot.Connector;
   using System;
   using System.Net;
  
   namespace QnABot
   {
  [BotAuthentication]
  public class MessagesController : ApiController
  {
      /// &lt;summary&gt;
      /// POST: api/Messages
      /// Receive a message from a user and reply to it
      /// &lt;/summary&gt;
      public async Task&lt;HttpResponseMessage&gt; Post([FromBody]Activity activity)
      {
  
          #region Set CurrentBaseURL and ChannelAccount
          // Get the base URL that this service is running at
          // This is used to show images
          string CurrentBaseURL =
  
       this.Url.Request.RequestUri.AbsoluteUri.Replace(@"api/messages", "");
  
          // Create an instance of BotData to store data
          BotData objBotData = new BotData();
  
          // Instantiate a StateClient to save BotData            
          StateClient stateClient = activity.GetStateClient();
  
          // Use stateClient to get current userData
          BotData userData = await stateClient.BotState.GetUserDataAsync(
              activity.ChannelId, activity.From.Id);
  
          // Update userData by setting CurrentBaseURL and Recipient
          userData.SetProperty&lt;string&gt;("CurrentBaseURL", CurrentBaseURL);
  
          // Save changes to userData
          await stateClient.BotState.SetUserDataAsync(
              activity.ChannelId, activity.From.Id, userData);
          #endregion
  
          if (activity.Type == ActivityTypes.Message)
          {
  
  
  
              //*************************
              //Log to Database
              // *************************
  
              //// Instantiate the BotData dbContext
              Model.qnamakerentitiesEntities DB = new 
             Model.qnamakerentitiesEntities();
              // Create a new UserLog object
              Model.UserLog NewUserLog = new Model.UserLog();
  
              // Set the properties on the UserLog object
              NewUserLog.Channel = activity.ChannelId;
              NewUserLog.UserID = activity.From.Id;
              NewUserLog.UserName = activity.From.Name;
              NewUserLog.created = DateTime.UtcNow;
              NewUserLog.Message = activity.Text.Truncate(500);
              //   NewUserLog.Response = activity.AsMessageActivity()?.Text;
  
  
  
              // Add the UserLog object to UserLogs
              DB.UserLogs.Add(NewUserLog);
              // Save the changes to the database
              DB.SaveChanges();
  
  
              await Conversation.SendAsync(activity, () =&gt; new 
          Dialogs.QnADialog());
          }
          else
          {
              await HandleSystemMessage(activity);
          }
          var response = Request.CreateResponse(HttpStatusCode.OK);
          return response;
      }
  
      private async Task&lt;Activity&gt; HandleSystemMessage(Activity message)
      {
          if (message.Type == ActivityTypes.DeleteUserData)
          {
              // Implement user deletion here
              // If we handle user deletion, return a real message
          }
          else if (message.Type == ActivityTypes.ConversationUpdate)
          {
              IConversationUpdateActivity iConversationUpdated = message as 
         IConversationUpdateActivity;
              if (iConversationUpdated != null)
              {
              }
          }
          else if (message.Type == ActivityTypes.ContactRelationUpdate)
          {
              // Handle add/remove from contact lists
              // Activity.From + Activity.Action represent what happened
          }
          else if (message.Type == ActivityTypes.Typing)
          {
              // Handle knowing tha the user is typing
          }
          else if (message.Type == ActivityTypes.Ping)
          {
          }
  
          return null;
           }
         }
       }
  </code></pre>
  
  <p>RootDialog.cs is like this :</p>
  
  <pre><code>  using System;
    using System.Threading.Tasks;
    using Microsoft.Bot.Builder.Dialogs;
    using Microsoft.Bot.Connector;
    using QnABot.API;
    using Microsoft.Bot.Builder.Dialogs.Internals;
  
    namespace QnABot.Dialogs
    {
    [Serializable]
    public class RootDialog : IDialog&lt;object&gt;
     {
      public Task StartAsync(IDialogContext context)
      {
          context.Wait(MessageReceivedAsync);
  
          return Task.CompletedTask;
      }
  
      private async Task MessageReceivedAsync(IDialogContext context, 
       IAwaitable&lt;object&gt; result)
      {
          //var activity = await result as Activity;
  
          //// Prompt text
          //await context.PostAsync("Welcome Feel free to ask me ");
  
          //var privateData = context.PrivateConversationData;
          //var privateConversationInfo = IncrementInfoCount(privateData, 
     BotStoreType.BotPrivateConversationData.ToString());
          //var conversationData = context.ConversationData;
          //var conversationInfo = IncrementInfoCount(conversationData, 
     BotStoreType.BotConversationData.ToString());
          //var userData = context.UserData;
          //var userInfo = IncrementInfoCount(userData, 
       BotStoreType.BotUserData.ToString());
          //context.Wait(QnADialog);
  
  
  
    //privateData.SetValue(BotStoreType.BotPrivateConversationData.ToString(), 
        privateConversationInfo);
  
     //conversationData.SetValue(BotStoreType.BotConversationData.ToString(), conversationInfo);
          //userData.SetValue(BotStoreType.BotUserData.ToString(), userInfo);
          Activity replyToConversation = (Activity)context.MakeMessage();
          replyToConversation.Recipient = replyToConversation.Recipient;
          replyToConversation.Type = "message";
      }
  
      private async Task QnADialog(IDialogContext context, IAwaitable&lt;object&gt; 
      result)
      {
          var activityResult = await result as Activity;
          var query = activityResult.Text;
          var reply = activityResult.CreateReply();
  
          var qnaResult = QnaApi.GetFirstQnaAnswer(query);
          string message = "";
  
          if (qnaResult == null)
          {
               message = $"Sorry, I did not understand . Please reformulate 
          your question";
  
          }
          else
          {
              message = qnaResult.answers[0].answer;
          }
  
  
  
          // *************************
          // Log to Database
          // *************************
          Activity replyToConversation = (Activity)context.MakeMessage();
          // Instantiate the BotData dbContext
          Model.qnamakerentitiesEntities DB = new 
          Model.qnamakerentitiesEntities();
          // Create a new UserLog object
          Model.UserLog NewUserLog = new Model.UserLog();
  
          // Set the properties on the UserLog object
          NewUserLog.Channel = replyToConversation.ChannelId;
          NewUserLog.UserID = replyToConversation.From.Id;
          NewUserLog.UserName = replyToConversation.From.Name;
          NewUserLog.created = DateTime.UtcNow;
          // This logs the message being sent to the user
          NewUserLog.Message = qnaResult.answers[0].answer;
          //  NewUserLog.Response= qnaResult.answers[0].answer;
  
          // Add the UserLog object to UserLogs
          DB.UserLogs.Add(NewUserLog);
          // Save the changes to the database
          DB.SaveChanges();
  
          await context.PostAsync(replyToConversation);
          context.Wait(MessageReceivedAsync);
      }
      public class BotDataInfo
      {
          public int Count { get; set; }
      }
  
      private BotDataInfo IncrementInfoCount(IBotDataBag botdata, string key)
      {
          BotDataInfo info = null;
          if (botdata.ContainsKey(key))
          {
              info = botdata.GetValue&lt;BotDataInfo&gt;(key);
              info.Count++;
          }
          else
              info = new BotDataInfo() { Count = 1 };
  
          return info;
      }
  }
  }
  </code></pre>
  
  <p>I tried to follow your instructions but still have problems. I can store in database Id, UserId, ChannelID, created, message but not the response ( response in null in all my tests ) .</p>
  
  <p>Can you help me what can i do ?</p>