Example #1
0
        /// <summary>
        /// Method for getting particular fields of the weather object
        /// </summary>
        /// <param name="weather"></param>
        /// <returns></returns>
        public List <Object> GetFields(WeatherAC weather)
        {
            List <Object> weatherProperties = new List <Object>()
            {
                new
                {
                    title  = "Weather",
                    value  = weather.Status,
                    @short = true
                },
                new
                {
                    title  = "Temperature",
                    value  = weather.Temperature,
                    @short = true
                },
                new
                {
                    title  = "Humidity",
                    value  = weather.Humidity,
                    @short = true
                },
                new
                {
                    title  = "Wind",
                    value  = weather.Wind,
                    @short = true
                }
            };

            return(weatherProperties);
        }
Example #2
0
        /// <summary>
        /// Main Slack Controller for Receiving Messages
        /// </summary>
        /// <param name="payload"></param>
        /// <returns></returns>
        public async Task Slack(PayLoad payload)
        {
            StringConstant    _stringConstant = new StringConstant();
            WeatherController _weather        = new WeatherController();

            Uri             webhookUrl  = new Uri(_stringConstant.SlackIncomingWebhook);
            SlackController slackClient = new SlackController(webhookUrl);

            string location = payload.text.Remove(0, 8);

            if (location.StartsWith(":"))
            {
                string errorMessageJson = ShowErrorMessage("Please enter a valid location, zip code or an area id.");
                await slackClient.SendMessageAsync(errorMessageJson);
            }
            else
            {
                WeatherAC weather = _weather.GetWeather(location);
                if (weather.Location == null)
                {
                    string errorMessageJson = ShowErrorMessage("The location does not exist.");
                    await slackClient.SendMessageAsync(errorMessageJson);
                }
                else
                {
                    await slackClient.SendMessageAsync(GetOutputFormat(weather));
                }
            }
        }
Example #3
0
        /// <summary>
        /// Method for getting the weather details - RS
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public WeatherAC GetWeather(string location)
        {
            StringConstant _stringConstant = new StringConstant();
            string         appId           = _stringConstant.WeatherApiKey;
            string         api             = _stringConstant.WeatherApi;

            WeatherAC weather = new WeatherAC();

            // Take Input
            api = String.Format(api, location, appId);

            // Connect to http client
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(api);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Get weather details
            HttpResponseMessage response = client.GetAsync(api).Result;

            if (response.IsSuccessStatusCode)
            {
                var weatherObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);

                // Get Coordinates
                Coordinates coord = GetCoOrdinates(weatherObj);
                weather.Latitude  = coord.Latitude;
                weather.Longitude = coord.Longitude;

                // Get Humidity (%)
                weather.Humidity = GetHumidity(weatherObj);

                // Get Location Name
                var countryDetails = new RegionInfo(weatherObj.SelectToken("sys").SelectToken("country").ToString());
                weather.Location = weatherObj.SelectToken("name").ToString() + ", "
                                   + countryDetails.EnglishName + " (" + countryDetails.NativeName + ")";

                // Get Wind Speed (km/h)
                weather.Wind = GetWindSpeed(weatherObj);

                // Get Temperature (°C)
                weather.Temperature = GetTemparature(weatherObj);

                weather.Status = GetStatus(weatherObj);
            }

            return(weather);
        }
Example #4
0
        /// <summary>
        /// Method for formatting output
        /// </summary>
        /// <param name="weather"></param>
        /// <returns></returns>
        public string GetOutputFormat(WeatherAC weather)
        {
            var payload = new
            {
                attachments = new List <Object>()
                {
                    new
                    {
                        fallback = "Required plain-text summary of the attachment.",
                        color    = "#36a64f",
                        title    = weather.Location + " Weather",
                        fields   = GetFields(weather)
                    }
                }
            };

            string result = JsonConvert.SerializeObject(payload);

            return(result);
        }