响应用户输入

Reacting to user input

我目前正忙于搜索用户周围区域的机器人。用户有能力增加搜索半径。这是我用来处理用户输入的代码,到目前为止代码的 "Yes" 位有效,但我不确定如何重新触发搜索。

搜索最初是由 luis 意图触发的,所以我认为这可能是导航到机器人中不同任务的最简单方法。但是我不确定如何以编程方式向机器人发送消息/从代码触发 luis 意图。

[LuisIntent("Stores")]
        public async Task Stores(IDialogContext context, LuisResult result)
        {
            var reply = context.MakeMessage();
            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            reply.Attachments = new List<Attachment>();
            List<CardImage> images = new List<CardImage>();

            InfoClass IC = new InfoClass();
            latitude = "-29.794618";
            longitude = "30.823497";
            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);
        }
    async Task promtDecision(IDialogContext context, IAwaitable<string> userInput)
    {
        string inputText = await userInput;
        if (inputText.Equals("yes") || inputText.Equals("y"))
        {
            RadiusPromt(context);
        }
        else
        {
            StartUp(context, null).Start();
        }
    }
    void RadiusPromt(IDialogContext context)
    {
        PromptDialog.Number(context, AfterPromptMethod, "Please Enter Search Radius (In Kilometers)", attempts: 100);
    }
    async Task AfterPromptMethod(IDialogContext context, IAwaitable<long> userInput)
    {
        int Radius = Convert.ToInt32(await userInput);
        await context.PostAsync("Store Locations");
    }
}

根据我从您的问题中了解到的情况,这是一个使用 int 参数调用我称为 SearchInArea 的方法的实现。

从 2 个地方调用此方法:

  • 当您的 LuisIntent Stores 被识别时

  • 当你回来时改变半径值

代码:

[LuisIntent("Stores")]
public async Task Stores(IDialogContext context, LuisResult result)
{
    var defaultRadius = 10;
    await SearchInArea(context, defaultRadius);
}

private async Task SearchInArea(IDialogContext context, int radius)
{
    var reply = context.MakeMessage();
    reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
    reply.Attachments = new List<Attachment>();
    List<CardImage> images = new List<CardImage>();

    InfoClass IC = new InfoClass();
    latitude = "-29.794618";
    longitude = "30.823497";
    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, PromptDecision, "Would You Like To Change The Search Radius ?", attempts: 100);
}

private async Task PromptDecision(IDialogContext context, IAwaitable<bool> userInput)
{
    var userBoolChoice = await userInput;
    if (userBoolChoice)
    {
        RadiusPromt(context);
    }
    else
    {
        StartUp(context, null).Start();
    }
}

private void RadiusPromt(IDialogContext context)
{
    PromptDialog.Number(context, AfterPromptMethod, "Please Enter Search Radius (In Kilometers)", attempts: 100);
}

private async Task AfterPromptMethod(IDialogContext context, IAwaitable<long> userInput)
{
    int radius = Convert.ToInt32(await userInput);
    await SearchInArea(context, radius);
}