diff --git a/requirements.txt b/requirements.txt index 0082b9dee..377098d1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ sqlalchemy python-telegram-bot>=10.1.0 psycopg2-binary feedparser +pyowm diff --git a/tg_bot/__init__.py b/tg_bot/__init__.py index fc4e687a5..2b4ea2614 100644 --- a/tg_bot/__init__.py +++ b/tg_bot/__init__.py @@ -57,6 +57,7 @@ WORKERS = int(os.environ.get('WORKERS', 8)) BAN_STICKER = os.environ.get('BAN_STICKER', 'CAADAgADOwADPPEcAXkko5EB3YGYAg') ALLOW_EXCL = os.environ.get('ALLOW_EXCL', False) + API_WEATHER = os.environ.get('API_OPENWEATHER', None) else: from tg_bot.config import Development as Config @@ -98,6 +99,7 @@ WORKERS = Config.WORKERS BAN_STICKER = Config.BAN_STICKER ALLOW_EXCL = Config.ALLOW_EXCL + API_WEATHER = Config.API_OPENWEATHER SUDO_USERS.add(OWNER_ID) diff --git a/tg_bot/modules/weather.py b/tg_bot/modules/weather.py new file mode 100644 index 000000000..ae7b51a23 --- /dev/null +++ b/tg_bot/modules/weather.py @@ -0,0 +1,50 @@ +import pyowm +from pyowm import timeutils, exceptions +from telegram import Message, Chat, Update, Bot +from telegram.ext import run_async + +from tg_bot import dispatcher, updater, API_WEATHER +from tg_bot.modules.disable import DisableAbleCommandHandler + +@run_async +def weather(bot, update, args): + if len(args) == 0: + update.effective_message.reply_text("Write a location to check the weather.") + return + + location = " ".join(args) + if location.lower() == bot.first_name.lower(): + update.effective_message.reply_text("I will keep an eye on both happy and sad times!") + bot.send_sticker(update.effective_chat.id, BAN_STICKER) + return + + try: + owm = pyowm.OWM(API_WEATHER) + observation = owm.weather_at_place(location) + getloc = observation.get_location() + thelocation = getloc.get_name() + if thelocation == None: + thelocation = "Unknown" + theweather = observation.get_weather() + temperature = theweather.get_temperature(unit='celsius').get('temp') + if temperature == None: + temperature = "Unknown" + + status = theweather._detailed_status + + update.message.reply_text("Today in {} is being {}, around {}°C.\n".format(thelocation, + status, temperature)) + + except pyowm.exceptions.not_found_error.NotFoundError: + update.effective_message.reply_text("Sorry, location not found.") + + +__help__ = """ + - /weather : get weather info in a particular place +""" + +__mod_name__ = "Weather" + +WEATHER_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True) + +dispatcher.add_handler(WEATHER_HANDLER) diff --git a/tg_bot/sample_config.py b/tg_bot/sample_config.py index a6b7c2293..4603e6ddf 100644 --- a/tg_bot/sample_config.py +++ b/tg_bot/sample_config.py @@ -18,7 +18,7 @@ class Config(object): SQLALCHEMY_DATABASE_URI = 'sqldbtype://username:pw@hostname:port/db_name' # needed for any database modules MESSAGE_DUMP = None # needed to make sure 'save from' messages persist LOAD = [] - NO_LOAD = ['translation', 'rss'] + NO_LOAD = ['translation', 'rss', 'weather'] WEBHOOK = False URL = None @@ -34,6 +34,7 @@ class Config(object): WORKERS = 8 # Number of subthreads to use. This is the recommended amount - see for yourself what works best! BAN_STICKER = 'CAADAgADOwADPPEcAXkko5EB3YGYAg' # banhammer marie sticker ALLOW_EXCL = False # Allow ! commands as well as / + API_OPENWEATHER = None # OpenWeather API class Production(Config):