从IAwaitable <imessageactivity>获得bool

时间:2017-05-24 19:54:31

标签: c# botframework

确认提示,ParseMessage将文本发送给Luis,然后返回true或false。我在此文件中设置了调试器,并且已验证的响应返回bool。

有问题的代码。当我得到结果时,它会显示Cannot implicitly convert IAwaitable<bool> to bool,这是Convert.ToBoolean()进来的地方;然而,仍然没有运气。

如何检查返回的bool,以便我可以在if语句中验证结果。

在这个当前的代码示例中,它只是在bot模拟器中发回一条消息:

异常:无法将类型为'Microsoft.Bot.Builder.Internals.Fibers.Wait`2 [Microsoft.Bot.Builder.Dialogs.Internals.DialogTask,System.Boolean]'的对象强制转换为'System.IConvertible'

编辑:已更新以获取更多代码

RootDialog.cs

    private async Task SendWelcomeMessageAsync(IDialogContext context)
    {
        await context.PostAsync("Hi, I'm the Basic Multi Dialog bot. Let's get started.");

        context.Call(new ConfirmLuisPrompt(), this.ConfirmLuisPromptAfter);
    }

    private async Task ConfirmLuisPromptAfter(IDialogContext context, IAwaitable<bool> result)
    {
        //var res = Convert.ToBoolean(result);
        var confirm = await result;

        if (confirm) 
        {
            //yes
            context.Call(FormDialog.FromForm(PersonInfo.BuildForm, FormOptions.PromptInStart), this.PersonInfoAfter);
        }else
        {
            //no
            await context.PostAsync($"Ok, let's get you started");

            context.Call(FormDialog.FromForm(PatientInfo.BuildForm, FormOptions.PromptInStart), InHospital);

        }
    }

ConfirmLuisPrompt.cs

 [Serializable]
public class ConfirmLuisPrompt : IDialog<bool>
{
    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Do you have insurance?");

        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {

        dynamic m = await result;
        var message = m.Text.ToString();
        //context.Wait( ParseMessage(context, message));
        bool response = await ParseMessage(message);
        context.Done(response);
        //context.PostAsync(response.toString());
    }


    public bool ParseMessage(string input)
    {
        LuisClient luisClient = new LuisClient("<key1>", "<key2>");
        Task<LuisResult> resultTask = luisClient.Predict(input);
        resultTask.Wait();
        LuisResult result = resultTask.Result;

        if (result.TopScoringIntent.Name == "Yes")
        {
            return true;
        }
        else if (result.TopScoringIntent.Name == "No")
        {
            return false;
        }
        else
        {
            return false;
        }
    }

}

2 个答案:

答案 0 :(得分:3)

您只需在ConfirmLuisPromptAfter方法中执行以下操作:

var confirm = await result;

if (confirm) 
{
   ...
}

答案 1 :(得分:0)

不要只转换:result.GetAwaiter().GetResult()应该返回一个布尔值