Example #1
0
        public async Task HourlyForecastWeather(params string[] inputArray)
        {
            if (inputArray.Length != 2)
            {
                await ReplyAsync("Wrong input try again +.+");

                return;
            }
            Weather             weather         = new Weather();
            WeatherForecastInfo weatherForecast = null;

            string city        = inputArray[0];
            string countryCode = inputArray[1];

            weatherForecast = await weather.GetForecastWeatherAsync(city, countryCode);

            string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\");

            if (weatherForecast == null) // if getting weatherInfo method getting exception than weatherinfo would be null
            {
                await ReplyAsync("Error Occur Try again ^오^");
            }
            else
            {
                await ReplyAsync($"Weather Forecast 5days 3hours each " +
                                 $"On {weatherForecast.city.name} Country: {weatherForecast.city.country}\n");


                for (int i = 0; i < weatherForecast.list.Count; i++)
                {
                    var embed = new EmbedBuilder();
                    embed.ThumbnailUrl = $"attachment://{weatherForecast.list[i].weather[0].icon}.png";
                    embed.Title        = $"\n{weatherForecast.list[i].weather[0].main}\n";
                    embed.Description  = $"\n{weatherForecast.list[i].dt_txt}\n" +
                                         $"Temperature(Celsius): {weatherForecast.list[i].main.temp} \nMin: {weatherForecast.list[i].main.temp_min} Max: {weatherForecast.list[i].main.temp_max}\n" +
                                         $"Clouds(%): {weatherForecast.list[i].clouds.all}\n" +
                                         $"Weather Forecast: {weatherForecast.list[i].weather[0].description}\n" +
                                         ((weatherForecast.list[i].rain == null) ? "" : $"rain: {weatherForecast.list[i].rain.h} mm");


                    await Context.Channel.SendFileAsync($"{filepath}{weatherForecast.list[i].weather[0].icon}.png", "", false, embed.Build());

                    //await ReplyAsync($"\n{weatherForecast.list[i].dt_txt}\n" +
                    //   $"Temperature(Celsius): {weatherForecast.list[i].main.temp} Min: {weatherForecast.list[i].main.temp_min} Max: {weatherForecast.list[i].main.temp_max}\n" +
                    //   $"Clouds(%): {weatherForecast.list[i].clouds.all}\n" +
                    //   $"Weather Forecast: {weatherForecast.list[i].weather[0].description}\n" +
                    //   ((weatherForecast.list[i].rain == null) ? "" : $"rain: {weatherForecast.list[i].rain.h} mm")
                    //   + "\n\n");
                }
            }
        }
        public async Task <WeatherForecastInfo> GetForecastWeatherAsync(string city, string countryCode)
        {
            string url = $"http://api.openweathermap.org/data/2.5/forecast?q={city},{countryCode}&units=metric&appid={AppID}";
            WeatherForecastInfo result = null;

            try
            {
                result = await Task.Run(() => DownloadForecastInfo(url));
            }
            catch (Exception e)
            {
                Debug.WriteLine($"{e} ERROR {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            }

            return(result);
        }