Messenger bot中出现PromptDialog错误

问题描述 投票:1回答:1

我的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);
    }
}
c# .net botframework
1个回答
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);
    }
}

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

也许你应该在这个方法中保持latitudelongitude设置,然后调用一个也可以从RadiusPrompt中调用的新方法

© www.soinside.com 2019 - 2024. All rights reserved.