def test_clima_ordering(self): CommandCall(user='******', _value=CommandCall.CLIMA, _datetime=pendulum.yesterday()).save() c2 = CommandCall(user='******', _value=CommandCall.CLIMA, _datetime=pendulum.now()).save() self.assertAlmostEqual(CommandCall.objects.count(), 2) self.assertEqual(CommandCall.last_clima(), c2)
def weather(bot, update, args): """Define weather at certain location""" user = update.message.from_user bot_instance = BotTelegramCore.instance() if bot_instance.is_from_oficial_chat(update): if not CommandCall.allow_call(command=CommandCall.CLIMA): last_call = CommandCall.last_clima() bot.sendMessage(chat_id=user.id, text=COMMAND_THROTTLED.format( segundos=last_call.cooldown_left, comando=last_call.value)) return api_key = config('OPENWEATHERMAP_TOKEN') owm = pyowm.OWM(api_key) text_location = " ".join(args) try: observation = owm.weather_at_place(text_location) _weather = observation.get_weather() humidity = _weather.get_humidity() wind = _weather.get_wind() temp = _weather.get_temperature('celsius') update.message.reply_text(f"🧭 Localização: {text_location}\n" f"🔥️ Temp. Maxima: " f"{temp.get('temp_max')} °C \n" f"❄️ Temp. Minima: " f"{temp.get('temp_min')} °C \n" f"💨 Vel. do Vento: " f"{wind.get('speed')} m/s \n" f"💧 Humidade: " f"{humidity}%") if bot_instance.is_from_oficial_chat(update): CommandCall.clima(user.username) except NotFoundError: update.message.reply_text(f"⚠️ Não consegui localizar a cidade " f"{text_location}!") except APICallError: update.message.reply_text(f"⚠️ Você precisa digitar uma cidade")