Пример #1
0
        private static string CreateOpenWeatherAnswer(GeoOpenWeatherModel model)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(localization.Current.WeatherResultStart);
            sb.AppendLine($"{localization.Current.WeatherResultPlace}: {model.name}");
            sb.AppendLine($"{localization.Current.WeatherResultOnStreet}: {model.weather.FirstOrDefault().description}");
            sb.AppendLine($"{localization.Current.WeatherResultTemperature}: " +
                          $"{model.main.temp} {localization.Current.WeatherResultGradus}, " +
                          $"{(model.main.temp * 1.8f) + 32} {localization.Current.WeatherResultFarengeit}");
            sb.AppendLine($"{localization.Current.WeatherResultFeelsLike}: " +
                          $"{model.main.feels_like} {localization.Current.WeatherResultGradus}, " +
                          $"{(model.main.feels_like * 1.8f) + 32} {localization.Current.WeatherResultFarengeit}");
            sb.AppendLine($"{localization.Current.WeatherResultWindDescription}: " +
                          $"{model.wind.speed} {localization.Current.WeatherResultKMHour}, " +
                          $"{model.wind.speed * 3.6f} {localization.Current.WeatherResultMeterSecond}");
            return(sb.ToString());
        }
Пример #2
0
        /// <summary>
        /// Обработка сообщения "Погода"
        /// </summary>
        /// <param name="args">Аргументы</param>
        /// <param name="IsWeatherBegin">Ведется ли проверка погоды сейчас</param>
        /// <param name="chat">Чат ИД</param>
        public static bool WeatherMessage(MessageEventArgs args, bool IsWeatherBegin, ChatId chat = null, string UsingApiMethod = null)
        {
            TelegramBotClient Bot = SimpleTBot.GetBot();

            if (!IsWeatherBegin)
            {
                var keyboard = KeyboardCore.ConfigureWeatherKeyboard();

                WithKeyboardSending(localization.Current.WeatherBeginedMsg, keyboard, args);
                return(true);
            }
            else
            {
                if (args.Message.Text == localization.Current.WeatherStopCommandMsg)
                {
                    var keyboard = KeyboardCore.ConfigureStandartKeyboard();

                    WithKeyboardSending(localization.Current.WeatherStopCommandAnswer, keyboard, args);
                    return(false);
                }
                else
                {
                    var keyboard = KeyboardCore.ConfigureStandartKeyboard();
                    if (args.Message.Type == MessageType.Text)
                    {
                        if (UsingApiMethod == ConstantStrings.WeatherToken)
                        {
                            string result = WebApiCore.ExecuteHttpRequest(WeatherCore.CreateApiStringWithCity(args.Message.Text));
                            List <FindedAddressModel> models = JsonConvert.DeserializeObject <List <FindedAddressModel> >(result);

                            WithVariantsSending(localization.Current.WeatherResultFindedMsg,
                                                KeyboardCore.ConfigureWeatherKeyboardWithVariants(models), args);
                            return(false);
                        }
                        if (UsingApiMethod == ConstantStrings.OpenWeatherMapToken)
                        {
                            try
                            {
                                string result             = WebApiCore.ExecuteHttpRequest(OpenWeatherCore.CreateApiStringWithCity(args.Message.Text));
                                GeoOpenWeatherModel model = JsonConvert.DeserializeObject <GeoOpenWeatherModel>(result);

                                WithKeyboardSending(CreateOpenWeatherAnswer(model), keyboard, args);
                            }
                            catch (Exception)
                            {
                                SendBadWeatherResponse(chat ?? args.Message.Chat.Id);
                            }
                        }
                    }
                    else if (args.Message.Type == MessageType.Location || args.Message.Type == MessageType.Venue)
                    {
                        if (UsingApiMethod == ConstantStrings.WeatherToken)
                        {
                            SendWeatherAnswer(new WeatherLocationModel()
                            {
                                lat = args.Message.Location.Latitude,
                                lon = args.Message.Location.Longitude
                            }, args.Message.Chat.Id);
                        }
                        if (UsingApiMethod == ConstantStrings.OpenWeatherMapToken)
                        {
                            try
                            {
                                string result             = WebApiCore.ExecuteHttpRequest(OpenWeatherCore.CreateApiStringWithLocation(args.Message.Location));
                                GeoOpenWeatherModel model = JsonConvert.DeserializeObject <GeoOpenWeatherModel>(result);

                                WithKeyboardSending(CreateOpenWeatherAnswer(model), keyboard, args);
                            }
                            catch (Exception)
                            {
                                SendBadWeatherResponse(chat ?? args.Message.Chat.Id);
                            }
                        }
                    }
                    return(false);
                }
            }
        }