Автор: Stez, 1 Год назад, написана на языке Python.
Встраивание на сайт
  1. import pyowm
  2. from pyowm import timeutils, exceptions
  3. from telegram import Message, Chat, Update, Bot
  4. from telegram.ext import run_async
  5.  
  6. from tg_bot import dispatcher, updater, BAN_STICKER, API_WEATHER
  7. from tg_bot.modules.disable import DisableAbleCommandHandler
  8.  
  9.  
  10.  
  11. @run_async
  12. def weather(bot, update, args):
  13.     if len(args) == 0:
  14.         update.effective_message.reply_text("Напишите место, чтобы узнать погоду. \nПример: /weather Чикаго")
  15.         return
  16.  
  17.     location = " ".join(args)
  18.     if location.lower() == bot.first_name.lower():
  19.         update.effective_message.reply_text("Буду следить и за счастливыми, и за печальными моментами!")
  20.         bot.send_sticker(update.effective_chat.id, BAN_STICKER)
  21.         return
  22.  
  23.     try:
  24.         owm = pyowm.OWM(API_WEATHER, language='ru')
  25.         observation = owm.weather_at_place(location)
  26.         getloc = observation.get_location()
  27.         thelocation = getloc.get_name()
  28.         if thelocation == None:
  29.             thelocation = "Неизвестно"
  30.         theweather = observation.get_weather()
  31.         temperature = theweather.get_temperature(unit='celsius').get('temp')
  32.         mini = theweather.get_temperature(unit='celsius').get('temp_min')
  33.         maxi = theweather.get_temperature(unit='celsius').get('temp_max')
  34.         wind = theweather.get_wind().get('speed')
  35.         humidity = theweather.get_humidity()
  36.         if temperature == None:
  37.             temperature = "Неизвестно"
  38.         if mini == None:
  39.             mini = "Неизвестно"
  40.         if maxi == None:
  41.             maxi = "Неизвестно"
  42.         if humidity == None:
  43.             humidity = "Неизвестно"
  44.         if wind == None:
  45.             wind = "Неизвестно"
  46.         status = theweather._detailed_status
  47.         icon = theweather.get_weather_code()
  48.  
  49.         if icon < 232:
  50.             icon = "⛈️ "
  51.         elif icon < 321:
  52.             icon = "?️ "
  53.         elif icon < 504:
  54.             icon = "?️ "
  55.         elif icon < 531:
  56.             icon = "⛈️ "
  57.         elif icon < 622:
  58.             icon = "?️ "
  59.         elif icon < 781:
  60.             icon = "?️ "
  61.         elif icon < 800:
  62.             icon = "?️ "
  63.         elif icon < 801:
  64.             icon = "⛅️ "
  65.         elif icon < 805:
  66.             icon = "☁️ "
  67.         update.message.reply_text("? Погода в городе {}, \n{}Сейчас {},\n? Около: {} °C, \n\
  68. ? Минимальная: {} °C, \n? Максимальная: {} °C,\n\
  69. ? Скорость ветра: {} м/с, \n? Влажность: {}%".format(thelocation, icon,
  70.                 status, temperature, mini, maxi, wind, humidity))
  71.  
  72.     except pyowm.exceptions.api_response_error.NotFoundError:
  73.         update.effective_message.reply_text("Извините, местоположение " + str(location) + " не найдено.")
  74.  
  75. __help__ = """
  76. - /weather <город>: Получить информацию о погоде в определенном месте
  77. """
  78.  
  79. __mod_name__ = "Погода"
  80.  
  81. WEATHER_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True)
  82.  
  83. dispatcher.add_handler(WEATHER_HANDLER)