Reputation: 923
I'm currently busy with a bot that searches an area around the user. The user has the ability to increase the search radius. This is the code I'm using to handle the users input, so far the "Yes" bit of the code works, however I'm not sure how to re-trigger the search.
The search is initially triggered by a luis intent so I thought that would probably be the easiest way to navigate to the different tasks in the bot. However I'm not sure how to send a message to the bot programmatically / trigger a luis intent from code.
[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¢er=" + 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");
}
}
Upvotes: 0
Views: 346
Reputation: 14619
Based on what I understood from your problem, here is an implementation calling a method that I called SearchInArea
, using an int
parameter.
This method is called from 2 places:
when your LuisIntent Stores
is recognized
when you come back avec changing the radius value
Code:
[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¢er=" + 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);
}
Upvotes: 1