Messenger bot中出现PromptDialog错误

时间:2017-07-05 13:28:16

标签: c# .net botframework

我的messenger机器人中有一个PromptDialog问题,每当它到达提示对话框时它会抛出“Bot Code有错误”。我已经尝试在代码中移动它但它仍然抛出它无论我把它放在哪里,我已经尝试将它放入自己的方法并简单地调用方法并将上下文传递给它但是它再次没有帮助

我认为它可能是LocationReceivedAsync中的内容但是我不确定是什么。

[LuisIntent("Stores")]
public async Task Stores(IDialogContext context, LuisResult result)
{
    await StoreSearch(context); 
}
private async Task StoreSearch(IDialogContext context)
{
    var reply = context.MakeMessage();
    reply.ChannelData = new FacebookMessage
    (
        text: "Please share your location with me.",
        quickReplies: new List<FacebookQuickReply>
        {
            new FacebookQuickReply(
                contentType: FacebookQuickReply.ContentTypes.Location,
                title: default(string),
                payload: default(string)
            )
        }
    );
    await context.PostAsync(reply);
    context.Wait(LocationReceivedAsync);
}
public virtual async Task LocationReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
    var reply = context.MakeMessage();
    reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
    reply.Attachments = new List<Attachment>();
    List<CardImage> images = new List<CardImage>();

    InfoClass IC = new InfoClass();
    var msg = await argument;
    var location = msg.Entities?.FirstOrDefault(e => e.Type == "Place");
    if (location != null)
    {
        latitude = location.Properties["geo"]?["latitude"]?.ToString();
        longitude = location.Properties["geo"]?["longitude"]?.ToString();
        LocationObject[] StoreLocations = IC.NearBy(latitude, longitude, Radius, context);
        int count = StoreLocations.Length;
        for (int z = 0; z < count; z++)
        {
            CardImage Ci = new CardImage("https://maps.googleapis.com/maps/api/staticmap?size=764x400&center=" + StoreLocations[z].Latitude + "," + StoreLocations[z].Longitude + "&zoom=15&markers=" + StoreLocations[z].Latitude + "," + StoreLocations[z].Longitude);
            images.Add(Ci);

            HeroCard hc = new HeroCard()
            {
                Title = StoreLocations[z].StoreName,
                Subtitle = StoreLocations[z].Subtitle,
                Images = new List<CardImage> { images[z] },
                Buttons = new List<CardAction>()
            };

            CardAction ca = new CardAction()
            {
                Title = "Show Me",
                Type = "openUrl",
                Value = "https://www.google.co.za/maps/search/" + StoreLocations[z].Latitude + "," + StoreLocations[z].Longitude 
            };
            hc.Buttons.Add(ca);
            reply.Attachments.Add(hc.ToAttachment());
        }
        await context.PostAsync(reply);
        PromptDialog.Confirm(context, promtDecision, "Would You Like To Change The Search Radius ?", attempts: 100);
    }
    context.Done(location);
}
async Task promtDecision(IDialogContext context, IAwaitable<bool> userInput)
{
    bool inputText = await userInput;
    if (inputText)
    {
        RadiusPromt(context);
    }
    else
    {
        await mainMenu(context);
    }
}

1 个答案:

答案 0 :(得分:2)

LocationReceivedAsync的实施中存在错误:当您找到位置时,不应在方法的末尾添加context.Done(location)。它应该在else声明中。

context.Done正在尝试完成当前对话框,而您仍在尝试执行操作(在您的情况下要求更改半径)。

校正:

private async Task LocationReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
    var reply = context.MakeMessage();
    reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
    reply.Attachments = new List<Attachment>();
    List<CardImage> images = new List<CardImage>();

    InfoClass IC = new InfoClass();
    var msg = await argument;
    var location = msg.Entities?.FirstOrDefault(e => e.Type == "Place");
    if (location != null)
    {
        latitude = location.Properties["geo"]?["latitude"]?.ToString();
        longitude = location.Properties["geo"]?["longitude"]?.ToString();
        LocationObject[] StoreLocations = IC.NearBy(latitude, longitude, Radius, context);
        int count = StoreLocations.Length;
        for (int z = 0; z < count; z++)
        {
            CardImage Ci = new CardImage("https://maps.googleapis.com/maps/api/staticmap?size=764x400&center=" + StoreLocations[z].Latitude + "," + StoreLocations[z].Longitude + "&zoom=15&markers=" + StoreLocations[z].Latitude + "," + StoreLocations[z].Longitude);
            images.Add(Ci);

            HeroCard hc = new HeroCard()
            {
                Title = StoreLocations[z].StoreName,
                Subtitle = StoreLocations[z].Subtitle,
                Images = new List<CardImage> { images[z] },
                Buttons = new List<CardAction>()
            };

            CardAction ca = new CardAction()
            {
                Title = "Show Me",
                Type = "openUrl",
                Value = "https://www.google.co.za/maps/search/" + StoreLocations[z].Latitude + "," + StoreLocations[z].Longitude
            };
            hc.Buttons.Add(ca);
            reply.Attachments.Add(hc.ToAttachment());
        }
        await context.PostAsync(reply);
        PromptDialog.Confirm(context, PromtDecision, "Would You Like To Change The Search Radius ?", attempts: 100);
    }
    // Change is here
    else
    {
        context.Done(location);
    }
}

编辑:关于您的实施还有一点。我不明白你的&#34;搜索和结果显示为什么&#34;代码(大约LocationObject[] StoreLocations = IC.NearBy(latitude, longitude, Radius, context); ...)在Facebook提示符(LocationReceivedAsync)的回调中,如果你想在RadiusPrompt之后重用它(实现在这里不可见但是我想它是你的我想不做?)。

也许您应该在此方法中设置latitudelongitude,然后调用也可以从RadiusPrompt调用的新方法