public static void UpdateUserLocation(string id, string location) { MessageHandler.SenderId = id; string address; using (var client = new WebClient()) { var json = JObject.Parse(client.DownloadString(string.Format(MessageEndPoint, location))); var results = (JArray)json["results"]; address = (string)results[0]["formatted_address"]; } var user = db.GetUser(id); if (user == null) { var newUser = new FBUser { SessionId = id, Location = location, LocationString = address, Currency = "USD", }; db._db.FBUsers.InsertOnSubmit(newUser); db._db.SubmitChanges(); } else { user.LocationString = address; user.Location = location; db._db.SubmitChanges(); } MessageHandler.SendTextMessage($"Location updated to {location}. Although, you may better know it as {address} :D"); }
public static void EntryPoint(string id, string attr) { var isCurrency = attr.Equals("currency", StringComparison.InvariantCultureIgnoreCase); var isLocation = attr.Equals("location", StringComparison.InvariantCultureIgnoreCase); if (!isCurrency && !isLocation) { MessageHandler.SendTextMessage($"I could not identify what you were asking for. Please ask for either location or currency"); return; } var name = isLocation ? "location" : "currency"; var user = db.GetUser(id); var toShow = isLocation ? user.LocationString : user.Currency + $" ({Currency.CurrencyNames[user.Currency]})"; if (string.IsNullOrEmpty(toShow)) { MessageHandler.SendTextMessage($"Your {name} is not currently set."); return; } MessageHandler.SendTextMessage($"Your {name} is currently set as {toShow} ;)"); }
public static async Task <string> EntryPoint(string origin, string dest, string currency) { bool oPos = false; bool dPos = false; if (origin.Equals("here", StringComparison.InvariantCultureIgnoreCase) || origin.Equals("my location", StringComparison.InvariantCultureIgnoreCase)) { oPos = true; } if (dest.Equals("here", StringComparison.InvariantCultureIgnoreCase) || dest.Equals("my location", StringComparison.InvariantCultureIgnoreCase)) { dPos = true; } if (oPos && dPos) { MessageHandler.SendTextMessage("You are searching between the same two points!"); return(string.Empty); } var routes = ConstructRouteObjects(origin, dest, oPos, dPos, currency); if (routes != null) { var message = ConstructJsonFromRoutes(routes, currency, origin, dest, oPos || dPos); //var message = GetDescription(origin, dest); await MessageHandler.SendCustomMessage(message); } else { if (!oPos && !dPos) { await MessageHandler.SendTextMessage("I am sorry I could not resolve your search query. Please check your response and try again"); } } return(string.Empty); }
public static IEnumerable <Segment> ConstructSegmentObjects(string origin, string dest, string routeName, bool oPos, bool dPos, string location, string currency) { var geolocation = ""; var url = ""; if (oPos || dPos) { var user = new UserHandler().GetUser(MessageHandler.SenderId); if (string.IsNullOrEmpty(location)) { MessageHandler.SendTextMessage( $"I am sorry there has been an error!"); return(null); } if (oPos) { url = $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oPos={location}&dName={dest}¤cyCode={currency}"; } if (dPos) { url = $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oName={origin}&dPos={location}¤cyCode={currency}"; } } url = string.IsNullOrEmpty(url) ? $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oName={origin}&dName={dest}¤cyCode={currency}" : url; JObject responseJson; using (var client = new HttpClient()) { var response = client.GetAsync(url); if (response.Result.IsSuccessStatusCode) { responseJson = JObject.Parse(response.Result.Content.ReadAsStringAsync().Result); } else { return(null); } } var routes = (JArray)responseJson["routes"]; var places = (JArray)responseJson["places"]; var agencies = (JArray)responseJson["agencies"]; var airlines = (JArray)responseJson["airlines"]; var vehicles = (JArray)responseJson["vehicles"]; var originLongName = places[0]["longName"]; var destLongName = places[1]["longName"]; var segmentList = new List <Segment>(); foreach (JObject route in routes) { var name = (string)route["name"]; if (name != routeName) { continue; } foreach (JObject segment in route["segments"]) { var kind = (string)segment["segmentKind"]; segmentList.Add(kind == "surface" ? HandleSurfaceSegment(segment, agencies, places, vehicles) : HandleAirSegment(segment, airlines, places, vehicles)); } } return(segmentList); }
public static IEnumerable <Route> ConstructRouteObjects(string origin, string dest, bool oPos, bool dPos, string currency = "USD") { var geolocation = ""; var address = ""; var url = ""; if (oPos || dPos) { var user = new UserHandler().GetUser(MessageHandler.SenderId); geolocation = user.Location; address = user.LocationString; if (string.IsNullOrEmpty(geolocation) || string.IsNullOrEmpty(address)) { MessageHandler.SendTextMessage( $"I am sorry, I do not know your current location. Please send it via Messenger"); return(null); } if (oPos) { url = $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oPos={geolocation}&dName={dest}¤cyCode={currency}"; } if (dPos) { url = $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oName={origin}&dPos={geolocation}¤cyCode={currency}"; } } url = string.IsNullOrEmpty(url) ? $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oName={origin}&dName={dest}¤cyCode={currency}" : url; var routesList = new List <Route>(); JObject responseJson; using (var client = new HttpClient()) { var response = client.GetAsync(url); if (response.Result.IsSuccessStatusCode) { responseJson = JObject.Parse(response.Result.Content.ReadAsStringAsync().Result); } else { return(null); } } var routes = (JArray)responseJson["routes"]; var places = (JArray)responseJson["places"]; var originLongName = places[0]["longName"]; var destLongName = places[1]["longName"]; foreach (JObject route in routes) { var indicativePrices = route["indicativePrices"] as JArray; var majorSegment = (JObject)((JArray)route["segments"]).OrderByDescending(s => (int)s["distance"]).First(); var name = (string)route["name"]; var pLow = indicativePrices != null ? (int?)indicativePrices[0]["priceLow"] : 0; var pHigh = indicativePrices != null ? (int?)indicativePrices[0]["priceHigh"] : 0; var duration = (int)route["totalDuration"]; var agencyTuple = ((string)majorSegment["segmentKind"]).Equals("surface") ? FetchAgencyImage(majorSegment, responseJson["agencies"] as JArray) : FetchAirlineImage(majorSegment, responseJson["airlines"] as JArray); var agencyName = agencyTuple.Item1; var agencyImage = agencyTuple.Item2; pLow = pLow ?? 0; pHigh = pHigh ?? 0; int?price = 0; if (pLow == 0 && pHigh == 0) { price = indicativePrices != null ? (int?)indicativePrices[0]["price"] : 0; } var routeObject = new Route(name, pLow ?? 0, pHigh ?? 0, duration, string.IsNullOrEmpty(agencyImage) && name.Contains("Drive") ? "http://i.imgur.com/PfC7OYk.jpg" : agencyImage, string.IsNullOrEmpty(agencyName) && name.Contains("Drive") ? "Self-Drive" : agencyName, !((string)majorSegment["segmentKind"]).Equals("surface"), price ?? 0); routesList.Add(routeObject); } var routeList = routesList.OrderBy(r => r.Duration).Take(Math.Min(routesList.Count, 6)).ToList(); var cheapestRoute = routesList.MinBy(x => x.Price); cheapestRoute.IsCheapest = true; var fastestRoute = routesList.MinBy(x => x.Duration); fastestRoute.IsFastest = true; if (!routeList.Contains(cheapestRoute)) { routeList.Add(cheapestRoute); } if (oPos) { MessageHandler.SendTextMessage( $"I have found {routeList.Count} routes from {address} to {destLongName}"); } else if (dPos) { MessageHandler.SendTextMessage( $"I have found {routeList.Count} routes from {originLongName} to {address}"); } else { MessageHandler.SendTextMessage( $"I have found {routeList.Count} routes from {originLongName} to {destLongName}"); } return(routeList); // Returns maximum of 7 routes. }