简体   繁体   中英

How to store in sql server response of bot

I'm creating a bot using botframework , c# . I'm using also qnamaker. My questions is how to store in database the questions and answer of users. For the moment i can store in log table only message send by users as below :

MessagesController.cs :

   //   if (activity.Type == ActivityTypes.Message)
        {
           *************************
            // Log to Database
            // *************************

            // Instantiate the BotData dbContext
            Model.qnamakerbotdataEntities DB = new Model.qnamakerbotdataEntities();
            // 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);

            // Add the UserLog object to UserLogs
            DB.UserLogs.Add(NewUserLog);
            // Save the changes to the database
            DB.SaveChanges();

Do you have an idea how to store message send by users and response of the bot ?

   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<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, 
       IAwaitable<object> result)
    {
        //var activity = await result as Activity;

        //// Prompt text
        //await context.PostAsync("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);
    }

    private async Task QnADialog(IDialogContext context, IAwaitable<object> result)
    {
        var activityResult = await result as Activity;
        var query = activityResult.Text;

        var qnaResult = QnaApi.GetFirstQnaAnswer(query);

        if (qnaResult == null)
        {
            string message = $"Sorry, I did not understand . Please 
        reformulate your question";
        }
        else
        {
            await context.PostAsync(qnaResult.answers[0].answer);
        }


        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<BotDataInfo>(key);
            info.Count++;
        }
        else
            info = new BotDataInfo() { Count = 1 };

        return info;
       }
        }
           }

You could so something like below. Please note I did not have all your code to make this compile, but you should be able to adjust it if needed. the main part is creating a reply like this var reply = activityResult.CreateReply(); and then setting the text in the message variable in your if-else then setting the text of the reply then sending it
NewUserLog.Message = reply.Text; await context.PostAsync(reply);

    private async Task QnADialog(IDialogContext context, IAwaitable<object> 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;                
        }

        reply.Text = message;
        Model.qnamakerbotdataEntities DB = new Model.qnamakerbotdataEntities();
        // Create a new UserLog object
        Model.UserLog NewUserLog = new Model.UserLog();

        // Set the properties on the UserLog object
        NewUserLog.Channel = reply.ChannelId;
        NewUserLog.UserID = reply.From.Id;
        NewUserLog.UserName = reply.From.Name;
        NewUserLog.created = DateTime.UtcNow;

        NewUserLog.Message = reply.Text;
        await context.PostAsync(reply);

        context.Wait(MessageReceivedAsync);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM