public void GivenICallOpenWeatherApiWithLatitudeAndLongitude(Decimal Latitude, Decimal Longitude) { this.Latitude = Latitude.ToString(); this.Longitude = Longitude.ToString(); // weatherApi = new ClientInterface(); WeatherResponseModel response = weatherApi.GetCurrentWeather(this.Latitude, this.Longitude); WeatherApiTemp = Convert.ToInt32(response.current.temp); }
public WeatherResponseModel GetCurrentWeather(string lat, string lon) { Client client = new Client(new HttpClient()); var response = client.CurrentWeatherDataAsync(q: "", id: "", lat: lat, lon: lon, zip: "", units: Units.Metric, lang: Lang.En, mode: Mode.Json, AppId: AppId).Result; weather = new WeatherResponseModel() { current = new Current() { temp = response.Main.Temp } }; return(weather); }
// GET JSON Response public WeatherResponseModel GET(string url) { WeatherResponseModel model = new WeatherResponseModel(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); model = JsonConvert.DeserializeObject <WeatherResponseModel>(reader.ReadToEnd()); UIManager.uiManager.countryName.text = string.Format("Country Name is: {0} ", model.name); UIManager.uiManager.countryTemperature.text = weatherUnityType == UnitType.imperial ? string.Format("Country Temperature is: {0} °F", model.main.temp.ToString()) : string.Format("Country Temperature is: {0} °C", model.main.temp.ToString()); UIManager.uiManager.countryLatitude.text = string.Format("Country latitude: {0}", model.coord.lat.ToString()); UIManager.uiManager.countryLongitude.text = string.Format("Country Longitude {0} ", model.coord.lon.ToString()); //country Code UIManager.uiManager.countryCode.text = string.Format("Country Code : {0} ", model.sys.country); UIManager.uiManager.countrySunrise.text = string.Format("Country Sunrise : {0}", UnixTimeStampToDateTime(model.sys.sunrise)); UIManager.uiManager.countrySunset.text = string.Format("Country Sunset: {0}", UnixTimeStampToDateTime(model.sys.sunset)); UIManager.uiManager.countryWeatherDescription.text = string.Format("Country Sky Status: {0} ", model.weather[0].description); UIManager.uiManager.countryHumidity.text = string.Format("Country Humidity: {0}% ", model.main.humidity); } } catch (WebException ex) { WebResponse errorResponse = ex.Response; using (Stream responseStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); string errorText = reader.ReadToEnd(); // log errorText } throw; } return(model); }
private static async void Bot_OnMessage(object sender, MessageEventArgs e) { Town = e?.Message?.Text; if (Town == null) { return; } Console.WriteLine($"recieved message {Town} in chat {e.Message.Chat.Id}"); try{ WebRequest httpWebRequest = WebRequest.Create($"http://api.openweathermap.org/data/2.5/weather?q={ Town }&units=metric&lang=ru&appid={ ApiKey }"); WebResponse httpWebResponse = await httpWebRequest.GetResponseAsync(); string response; using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream())){ response = streamReader.ReadToEnd(); } WeatherResponseModel weatherResponseModel = JsonConvert.DeserializeObject <WeatherResponseModel>(response); await botClient.SendTextMessageAsync( chatId : e.Message.Chat.Id, //text: $"/nПогода в городе { weatherResponseModel.Name } : { Math.Round(weatherResponseModel.Main.Temp) } градусовd { weatherResponseModel.Weather.Description }" text : $"Погода в городе { weatherResponseModel.Name } : { Math.Round(weatherResponseModel.Main.Temp) } градусов \n{ weatherResponseModel.Weather[0].Main } :: { weatherResponseModel.Weather[0].Description }" ); } catch (Exception ex) { await botClient.SendTextMessageAsync( chatId : e.Message.Chat.Id, text : $"В списке 200 000 городов, а ты всеравно выбрал(а) несуществующий, браво!" ); Console.WriteLine($"MISTAKE recieved message {ex.Message} in chat {e.Message.Chat.Id}"); } finally{ } }