Messenger bot中发生PromptDialog错误 [英] PromptDialog error occurs in Messenger bot

查看:139
本文介绍了Messenger bot中发生PromptDialog错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Messenger bot中的PromptDialog出现问题,每当到达提示对话框时,都会引发"Bot Code has an Error".我尝试将其移至代码周围,但无论将其放置在何处,它仍会抛出该错误,我尝试将其放入其自己的方法中,只是调用该方法并将上下文传递给它,但再次它没有帮助

I'm having an issue with a PromptDialog in my messenger bot, Whenever it reaches the prompt dialog it throws a "Bot Code has an Error". I've tried moving it around the code but it still throws it no matter where I put it, I've tried to place it into its own method and simply call the method and pass the context to it but again it didn't help.

我认为可能是LocationReceivedAsync中的某物,但是我不确定是什么.

I think it may be something in the LocationReceivedAsync however I'm not sure what.

[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);
    }
}

推荐答案

LocationReceivedAsync的实现中有一个错误:在获得位置时,不应在方法的末尾放置context.Done(location).它应该在else语句上.

There is an error in your implementation of LocationReceivedAsync: you should not put a context.Done(location) at the end of the method when you got a location. It should be on an else statement.

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

This context.Done is trying to complete the current dialog whereas you are still trying to do actions (in your case asking for a radius change).

更正:

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);
    }
}

关于您的实现的另外一点.我不明白为什么要在RadiusPrompt之后重用它(在此处看不到实现,但为什么要在Facebook提示符(LocationReceivedAsync)的回调中使用搜索和结果显示"代码(在LocationObject[] StoreLocations = IC.NearBy(latitude, longitude, Radius, context); ...附近)?我想这就是您不希望做的事?).

one more point about your implementation. I don't understand why your "search and results display" code (around LocationObject[] StoreLocations = IC.NearBy(latitude, longitude, Radius, context);...) is in the callback from Facebook prompt (LocationReceivedAsync) if you want to reuse it after the RadiusPrompt (implementation not visible here but I guess it's what you would like to do no?).

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

Maybe you should keep latitude and longitude setup in this method, and then call a new method that can also be called from your RadiusPrompt

这篇关于Messenger bot中发生PromptDialog错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆