简体   繁体   中英

How to use FormFlow result at Microsoft BotFramework?

I create simple bot with Microsoft BotFramework and i use FormFlow . So:

[Serializable]
public class Test
{
 public String Name {get;set;}
 public uint Age {get;set; }
}

internal static IDialog<Test> MakeRootDialog()
    {
        return Chain.From(() => FormDialog.FromForm(Test.BuildForm));
    }

And:

 public async Task<Message> Post([FromBody]Message message)
    {
        if (message.Type == "Message")
        {         
         return await Conversation.SendAsync(message, MakeRootDialog);
        }
        else
        {
            return HandleSystemMessage(message);
        }
    }

So, Micrisoft BotEmulator works well (and bot) and ask me Name and Age of Person. But, how to get result of this choise to use it?

And how to know what user type it? Should i use ConversationId?

PS i mean how can i get result from user name and user age? I try to use:

 var name= result.GetBotPerUserInConversationData<Test>("Name");

But it return null;

PPS: i use Bot Emulator: and get json responce like this:

GetBotPerUserInConversationData:DialogState { some binary data }

So, i use

   var name= result.GetBotPerUserInConversationData<Test>("DialogState");

But get an error:

 "exceptionMessage": "Error converting value System.Byte[] to type 'Test'. Path ''.",
"exceptionType": "Newtonsoft.Json.JsonSerializationException"    

Hi since you are building the form, you can get the result in FormFlowComplete callback method as below

private async Task yourFormFlowComplete(IDialogContext context, IAwaitable<yourclass> result)
    {
         var res = await result;//res will contain the result set, if you build the form with a class
    }

You can just 'chain' a Do call to the Chain.From

internal static IDialog<Test> MakeRootDialog()
{
     return Chain.From(() => FormDialog.FromForm(Test.BuildForm))
                  .Do(async (context, formResult) =>
                  {
                        var completed = await formResult;
                        //your logic
                 }
}

'completed' will have the result of the form with the entries from the user.

You can refer to the AnnotatedSandwichBot where they are doing exactly what you need here .

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