private static Segment HandleAirSegment(JObject segment, JArray airlines, JArray places, JArray vehicles) { var indicativePrices = segment["indicativePrices"] as JArray; var pLow = indicativePrices != null ? (int?)indicativePrices[0]["priceLow"] : 0; var pHigh = indicativePrices != null ? (int?)indicativePrices[0]["priceHigh"] : 0; var vehicleJson = (JObject)vehicles.ElementAt((int)segment["vehicle"]); var kind = (string)vehicleJson["kind"]; var duration = (int)segment["transitDuration"] + (int)segment["transferDuration"]; var distance = (double)segment["distance"]; var tuple = RoutesHandler.FetchAirlineImage(segment, airlines); var image = tuple.Item2; var segName = tuple.Item1; var from = (string)((JObject)places.ElementAt((int)segment["depPlace"]))["shortName"]; var to = (string)((JObject)places.ElementAt((int)segment["arrPlace"]))["shortName"]; pLow = pLow ?? 0; pHigh = pHigh ?? 0; int?price = 0; if (pLow == 0 && pHigh == 0) { price = indicativePrices != null ? (int?)indicativePrices[0]["price"] : 0; } return(new AirSegment(segName, from, to, (int)pLow, (int)pHigh, duration, distance, image, "", kind, price ?? 0)); }
private static string ConstructSegmentJson(IEnumerable <Segment> segments, string currency) { var segmentList = segments.ToList(); var elementsJson = new JArray(); foreach (var segment in segmentList) { var emoji = GetEmojiSegment(segment.Kind); var name = segment.Name + emoji; var priceLow = segment.PriceLow.ToString("C0", new CultureInfo(currency)); var priceHigh = segment.PriceHigh.ToString("C0", new CultureInfo(currency)); var duration = RoutesHandler.FormatDuration(segment.Duration); var image = segment.Image; var priceString = (segment.PriceLow == 0 && segment.PriceHigh == 0) ? segment.Price.ToString("C0", new CultureInfo(currency)) : $"{priceLow} - {priceHigh}"; if (segment is SurfaceSegment) { var buttons = new JArray(); var surSegment = (SurfaceSegment)segment; if (string.IsNullOrEmpty(surSegment.ScheduleUrl) || string.IsNullOrEmpty(surSegment.MapUrl) || string.IsNullOrEmpty(surSegment.PhoneNumber)) { buttons.Add(new JObject( new JProperty("type", "web_url"), new JProperty("url", surSegment.BookUrl), new JProperty("title", "Go To Agency Page") ) ); } if (!string.IsNullOrEmpty(surSegment.ScheduleUrl)) { buttons.Add(new JObject( new JProperty("type", "web_url"), new JProperty("url", surSegment.ScheduleUrl), new JProperty("title", "Go To Booking Page") ) ); } if (!string.IsNullOrEmpty(surSegment.MapUrl)) { dynamic recipient = new JObject(); recipient.id = MessageHandler.SenderId; dynamic payLoad = new JObject(); payLoad.recipient = recipient; payLoad.intent = "SendMap"; payLoad.MapUrl = surSegment.MapUrl; var p = payLoad.ToString(); //var p = $"\"payload\":{{\"recipient\" : {{\"id\" : \"{{{MessageHandler.SenderId}}}\"}},\"intent\":\"SegmentInfo\", \"origin\":\"{origin}\", \"destination\":\"{dest}\", \"routeName\":\"{route.Name}\"}}"; var button = new JObject(new JProperty("type", "postback"), new JProperty("title", "Show Map"), new JProperty("payload", p)); buttons.Add(button); } if (!string.IsNullOrEmpty(surSegment.PhoneNumber)) { buttons.Add(new JObject( new JProperty("type", "phone_number"), new JProperty("payload", surSegment.PhoneNumber), new JProperty("title", "Call Agency 📞") ) ); } var element = new JObject( new JProperty("title", name), new JProperty("image_url", image), new JProperty("subtitle", $"From: {segment.From}\nTo:{segment.To}\nPrice: {priceString}"), //new JProperty("default_action", defaultAction), new JProperty("buttons", buttons) ); elementsJson.Add(element); } else { var airSegment = (AirSegment)segment; var buttons = new JArray(); buttons.Add(new JObject( new JProperty("type", "web_url"), new JProperty("url", "www.google.com"), new JProperty("title", "View More Info") ) ); var element = new JObject( new JProperty("title", name), new JProperty("image_url", image), new JProperty("subtitle", $"From: {segment.From}\nTo:{segment.To}\nPrice: {priceString} \nDuration: {duration}"), //new JProperty("default_action", defaultAction), new JProperty("buttons", buttons) ); elementsJson.Add(element); } } var payload = new JObject( new JProperty("template_type", "generic"), new JProperty("image_aspect_ratio", "square"), new JProperty("elements", elementsJson) ); var attachment = new JObject( new JProperty("type", "template"), new JProperty("payload", payload) ); var returnJson = new JObject( new JProperty("attachment", attachment) ); return(returnJson.ToString(Formatting.None)); }
public static Segment HandleSurfaceSegment(JObject segment, JArray agencies, JArray places, JArray vehicles) { var indicativePrices = segment["indicativePrices"] as JArray; var pLow = indicativePrices != null ? (int?)indicativePrices[0]["priceLow"] : 0; var pHigh = indicativePrices != null ? (int?)indicativePrices[0]["priceHigh"] : 0; var agency = (JObject)((JArray)segment["agencies"]).ElementAt(0); var vehicleJson = (JObject)vehicles.ElementAt((int)segment["vehicle"]); var kind = (string)vehicleJson["kind"]; var agencyJson = agencies.ElementAt((int)agency["agency"]); var links = segment["links"] as JArray; var bookUrl = ""; var scheUrl = ""; if (links != null) { foreach (JObject link in links) { if (((string)link["text"]).StartsWith("Book")) { bookUrl = (string)link["displayUrl"]; } ; if (((string)link["text"]).StartsWith("Schedule")) { scheUrl = (string)link["displayUrl"]; } ; } } var defaultUrl = (string)agencyJson["url"]; bookUrl = string.IsNullOrEmpty(bookUrl) ? defaultUrl : bookUrl; var segName = (string)agencyJson["name"]; var phone = (string)agencyJson["phone"]; var duration = (int)segment["transitDuration"] + (int)segment["transferDuration"]; var image = RoutesHandler.FetchAgencyImage(segment, agencies); var from = (string)((JObject)places.ElementAt((int)segment["depPlace"]))["shortName"]; var to = (string)((JObject)places.ElementAt((int)segment["arrPlace"]))["shortName"]; var latlongs = (string)segment["path"]; var mapUrl = ""; if (!string.IsNullOrEmpty(latlongs)) { mapUrl = string.Format(StaticMapUrl, latlongs); } pLow = pLow ?? 0; pHigh = pHigh ?? 0; int?price = 0; if (pLow == 0 && pHigh == 0) { price = indicativePrices != null ? (int?)indicativePrices[0]["price"] : 0; } return(new SurfaceSegment(segName, from, to, pLow ?? 0, pHigh ?? 0, duration, image.Item2, defaultUrl, bookUrl, scheUrl, phone, mapUrl, kind, price ?? 0)); }