├── runtime.txt ├── Procfile ├── README.md ├── requirements.txt ├── .gitignore └── bot.py /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.8.10 -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | worker: python bot.py -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eugene_telebot 2 | Telegram bot for weather - Update today 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yauheni-Ivashkevich/eugene_telebot/HEAD/requirements.txt -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | config.py 2 | Pipfile.lock 3 | .idea/ 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | *$py.class 9 | 10 | # C extensions 11 | *.so 12 | 13 | # Distribution / packaging 14 | .Python 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | wheels/ 27 | pip-wheel-metadata/ 28 | share/python-wheels/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | MANIFEST 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .nox/ 48 | .coverage 49 | .coverage.* 50 | .cache 51 | nosetests.xml 52 | coverage.xml 53 | *.cover 54 | *.py,cover 55 | .hypothesis/ 56 | .pytest_cache/ 57 | 58 | # Translations 59 | *.mo 60 | *.pot 61 | 62 | # Django stuff: 63 | *.log 64 | local_settings.py 65 | db.sqlite3 66 | db.sqlite3-journal 67 | 68 | # Flask stuff: 69 | instance/ 70 | .webassets-cache 71 | 72 | # Scrapy stuff: 73 | .scrapy 74 | 75 | # Sphinx documentation 76 | docs/_build/ 77 | 78 | # PyBuilder 79 | target/ 80 | 81 | # Jupyter Notebook 82 | .ipynb_checkpoints 83 | 84 | # IPython 85 | profile_default/ 86 | ipython_config.py 87 | 88 | # pyenv 89 | .python-version 90 | 91 | # pipenv 92 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 93 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 94 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 95 | # install all needed dependencies. 96 | #Pipfile.lock 97 | 98 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 99 | __pypackages__/ 100 | 101 | # Celery stuff 102 | celerybeat-schedule 103 | celerybeat.pid 104 | 105 | # SageMath parsed files 106 | *.sage.py 107 | 108 | # Environments 109 | .venv 110 | env/ 111 | venv/ 112 | ENV/ 113 | env.bak/ 114 | venv.bak/ 115 | .env 116 | 117 | # Spyder project settings 118 | .spyderproject 119 | .spyproject 120 | 121 | # Rope project settings 122 | .ropeproject 123 | 124 | # mkdocs documentation 125 | /site 126 | 127 | # mypy 128 | .mypy_cache/ 129 | .dmypy.json 130 | dmypy.json 131 | 132 | # Pyre type checker 133 | .pyre/ 134 | .static/ 135 | .secrets.* -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dotenv import load_dotenv 3 | import telebot 4 | from pyowm import OWM 5 | from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton 6 | 7 | load_dotenv() 8 | 9 | TOKEN = os.getenv("TOKEN") 10 | API_KEY = os.getenv("API_KEY") 11 | 12 | bot = telebot.TeleBot(TOKEN) 13 | 14 | 15 | @bot.message_handler(content_types=['text']) 16 | def get_text_messages(message): 17 | if message.text == '/weather': 18 | bot.send_message(message.from_user.id, 'Введите название города') 19 | bot.register_next_step_handler(message, get_weather) 20 | else: 21 | bot.send_message(message.from_user.id, "Напиши /weather чтобы узнать погоду") 22 | 23 | 24 | @bot.message_handler(commands=['url']) 25 | def url(message): 26 | markup = InlineKeyboardMarkup() 27 | btn_my_site = InlineKeyboardButton(text='Погода в Минске', url='https://yandex.by/pogoda/minsk') 28 | markup.add(btn_my_site) 29 | bot.send_message(message.chat.id, "Всегда актуальный прогноз для Минска", reply_markup=markup) 30 | 31 | 32 | # def gen_markup(): 33 | # markup = InlineKeyboardMarkup() 34 | # markup.row_width = 2 35 | # markup.add(InlineKeyboardButton("Yes", callback_data="cb_yes"), 36 | # InlineKeyboardButton("No", callback_data="cb_no")) 37 | # 38 | # 39 | # @bot.callback_query_handler(func=lambda call: True) 40 | # def callback_query(call): 41 | # if call.data == "cb_yes": 42 | # bot.answer_callback_query(call.id, "Answer is Yes") 43 | # elif call.data == "cb_no": 44 | # bot.answer_callback_query(call.id, "Answer is No") 45 | # 46 | # 47 | # @bot.message_handler(func=lambda message: True) 48 | # def message_handler(message): 49 | # bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup()) 50 | 51 | 52 | # Обработчик команд '/start' и '/help'. 53 | @bot.message_handler(commands=['start', 'help']) 54 | def handle_start_help(message): 55 | pass 56 | 57 | 58 | # Обработчик для документов и аудиофайлов 59 | # @bot.message_handler(content_types=['document', 'audio']) 60 | # def handle_document_audio(message): 61 | # pass 62 | 63 | 64 | def get_weather(message): 65 | city = message.text 66 | try: 67 | w = weather(city) 68 | bot.send_message(message.from_user.id, f'В городе {city} сейчас {round(w[0]["temp"])} градусов,' 69 | f' чувствуется как {round(w[0]["feels_like"])} градусов') 70 | bot.send_message(message.from_user.id, w[1]) 71 | bot.send_message(message.from_user.id, 'Доброго времен суток! Введите название города') 72 | bot.register_next_step_handler(message, get_weather) 73 | except Exception: 74 | bot.send_message(message.from_user.id, 'Упс... такого города нет в базе, попробуйте ещё раз') 75 | bot.send_message(message.from_user.id, 'Введите название города') 76 | bot.register_next_step_handler(message, get_weather) 77 | 78 | 79 | def get_location(lat, lon): 80 | url = f"https://yandex.ru/pogoda/maps/nowcast?lat={lat}&lon={lon}&via=hnav&le_Lighting=1" 81 | return url 82 | 83 | 84 | def weather(city: str): 85 | owm = OWM(API_KEY) 86 | mgr = owm.weather_manager() 87 | observation = mgr.weather_at_place(city) 88 | weather = observation.weather 89 | location = get_location(observation.location.lat, observation.location.lon) 90 | temperature = weather.temperature("celsius") 91 | return temperature, location 92 | 93 | 94 | bot.polling(none_stop=True, interval=0) 95 | --------------------------------------------------------------------------------