private IEnumerable <Hotel> GetHotels(HotelsQuery searchQuery) { var hotels = new List <Hotel>(); // Filling the hotels results manually just for demo purposes for (int i = 1; i <= 6; i++) { var random = new Random(i); Hotel hotel = new Hotel() { Name = $"Hotel {i}", Location = searchQuery.Destination, Rating = random.Next(1, 5), NumberOfReviews = random.Next(0, 5000), PriceStarting = random.Next(80, 450), Image = $"https://placeholdit.imgix.net/~text?txtsize=35&txt=Hotel+{i}&w=500&h=260", MoreImages = new List <string>() { "https://placeholdit.imgix.net/~text?txtsize=65&txt=Pic+1&w=450&h=300", "https://placeholdit.imgix.net/~text?txtsize=65&txt=Pic+2&w=450&h=300", "https://placeholdit.imgix.net/~text?txtsize=65&txt=Pic+3&w=450&h=300", "https://placeholdit.imgix.net/~text?txtsize=65&txt=Pic+4&w=450&h=300" } }; hotels.Add(hotel); } hotels.Sort((h1, h2) => h1.PriceStarting.CompareTo(h2.PriceStarting)); return(hotels); }
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result) { var message = await result; if (message.Value != null) { // Got an Action Submit dynamic value = message.Value; string submitType = value.Type.ToString(); switch (submitType) { case "HotelSearch": HotelsQuery query; try { query = HotelsQuery.Parse(value); // Trigger validation using Data Annotations attributes from the HotelsQuery model List <ValidationResult> results = new List <ValidationResult>(); bool valid = Validator.TryValidateObject(query, new ValidationContext(query, null, null), results, true); if (!valid) { // Some field in the Hotel Query are not valid var errors = string.Join("\n", results.Select(o => " - " + o.ErrorMessage)); await context.PostAsync("Please complete all the search parameters:\n" + errors); return; } } catch (InvalidCastException) { // Hotel Query could not be parsed await context.PostAsync("Please complete all the search parameters"); return; } // Proceed with hotels search await context.Forward(new HotelsDialog(), this.ResumeAfterOptionDialog, message, CancellationToken.None); return; case "HotelSelection": await SendHotelSelectionAsync(context, (Hotel)JsonConvert.DeserializeObject <Hotel>(value.ToString())); context.Wait(MessageReceivedAsync); return; } } if (message.Text != null && (message.Text.ToLower().Contains("help") || message.Text.ToLower().Contains("support") || message.Text.ToLower().Contains("problem"))) { await context.Forward(new SupportDialog(), this.ResumeAfterSupportDialog, message, CancellationToken.None); } else { await ShowOptionsAsync(context); } }
public async Task StartAsync(IDialogContext context) { var message = context.Activity as IMessageActivity; var query = HotelsQuery.Parse(message.Value); await context.PostAsync($"Ok. Searching for Hotels in {query.Destination} from {query.Checkin.Value.ToString("MM/dd")} to {query.Checkin.Value.AddDays(query.Nights.Value).ToString("MM/dd")}..."); try { await SearchHotels(context, query); } catch (FormCanceledException ex) { await context.PostAsync($"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}"); } }
private async Task SearchHotels(IDialogContext context, HotelsQuery searchQuery) { var hotels = this.GetHotels(searchQuery); // Result count var title = $"I found in total {hotels.Count()} hotels for your dates:"; var intro = new List <CardElement>() { new TextBlock() { Text = title, Size = TextSize.ExtraLarge, Speak = $"<s>{title}</s>" } }; // Hotels in rows of three var rows = Split(hotels, 3) .Select(group => new ColumnSet() { Columns = new List <Column>(group.Select(AsHotelItem)) }); var card = new AdaptiveCard() { Body = intro.Union(rows).ToList() }; Attachment attachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = card }; var reply = context.MakeMessage(); reply.Attachments.Add(attachment); await context.PostAsync(reply); }