add cron job

This commit is contained in:
Dmitry Afanasyev 2022-08-08 18:03:48 +03:00
parent f4ded6ae10
commit 3ae46f8c0e
3 changed files with 19 additions and 15 deletions

27
main.py
View File

@ -1,8 +1,8 @@
from aiogram import Bot, types from aiogram import Bot, types
from aiogram.contrib.middlewares.logging import LoggingMiddleware from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.dispatcher import Dispatcher from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.webhook import SendMessage, get_new_configured_app from aiogram.dispatcher.webhook import SendMessage
from aiogram.utils.executor import start_webhook, Executor from aiogram.utils.executor import start_webhook
from mos_gor import logger, parse_site, download_gecko_driver, configure_firefox_driver from mos_gor import logger, parse_site, download_gecko_driver, configure_firefox_driver
from settings import API_TOKEN, WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT from settings import API_TOKEN, WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT
@ -12,6 +12,15 @@ dp = Dispatcher(bot)
dp.middleware.setup(LoggingMiddleware()) dp.middleware.setup(LoggingMiddleware())
@dp.message_handler(commands=['chatid'])
async def echo(message: types.Message):
# Regular request
# await bot.send_message(message.chat.id, message.text)
# or reply INTO webhook
return SendMessage(message.chat.id, message.chat.id)
@dp.message_handler() @dp.message_handler()
async def echo(message: types.Message): async def echo(message: types.Message):
# Regular request # Regular request
@ -23,6 +32,10 @@ async def echo(message: types.Message):
return SendMessage(message.chat.id, text) return SendMessage(message.chat.id, text)
async def send_message(chat_id: int, text: str):
await bot.send_message(chat_id=chat_id, text=text, parse_mode=types.ParseMode.HTML)
async def on_startup(dp): async def on_startup(dp):
await bot.set_webhook(WEBHOOK_URL) await bot.set_webhook(WEBHOOK_URL)
# insert code here to run it after start # insert code here to run it after start
@ -43,16 +56,6 @@ async def on_shutdown(dp):
logger.warning('Bye!') logger.warning('Bye!')
async def async_app():
app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_PATH)
executor = Executor(dp, skip_updates=True)
executor.on_startup(on_startup)
executor.on_shutdown(on_shutdown)
executor._prepare_webhook(WEBHOOK_PATH, app=app)
await executor._startup_webhook()
return app
if __name__ == '__main__': if __name__ == '__main__':
download_gecko_driver() download_gecko_driver()
driver = configure_firefox_driver() driver = configure_firefox_driver()

View File

@ -68,4 +68,4 @@ def parse_site(driver: WebDriver) -> str:
bus_t19_arrival = element.find_element(by='class name', value='masstransit-prognoses-view__title-text') bus_t19_arrival = element.find_element(by='class name', value='masstransit-prognoses-view__title-text')
except NoSuchElementException: except NoSuchElementException:
pass pass
return f'{bus_300.text} - {bus_300_arrival.text}\n{bus_t19.text} - {bus_t19_arrival.text}' return f'Автобус {bus_300.text} - {bus_300_arrival.text}\nАвтобус {bus_t19.text} - {bus_t19_arrival.text}'

View File

@ -1,6 +1,7 @@
#! /bin/bash #! /bin/bash
set -e
echo "shutting down the bot" echo "shutting down the bot"
killall python; killall firefox
killall firefox; killall python