-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (71 loc) · 3.01 KB
/
Copy pathmain.py
File metadata and controls
88 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import asyncio
import datetime
from aiogram import Bot, Dispatcher
from aiogram.filters import Command, StateFilter
from aiogram.fsm.context import FSMContext
from aiogram.fsm.strategy import FSMStrategy
from aiogram.types import Message
from config import settings
from db import DBService
from loggers import logger
from server_status_service import ServiceStatusService
from statuses import States
allowed_users = settings.ALLOWED_USERS
group_chat_id = settings.GROUP_CHAT_ID
check_time = datetime.time(21, 0)
bot = Bot(token=settings.API_TOKEN)
dp = Dispatcher(fsm_strategy=FSMStrategy.CHAT)
async def daily_check():
now = datetime.datetime.now().time()
if now.hour == check_time.hour and now.minute == check_time.minute:
last_event_date = DBService().get_last_event_date()
if not last_event_date:
await bot.send_message(group_chat_id, "There have been no falls yet.")
else:
last_event_date = datetime.datetime.strptime(last_event_date, "%Y-%m-%d %H:%M:%S.%f")
days_since_last_event = (datetime.datetime.now() - last_event_date).days
await bot.send_message(group_chat_id, f"{days_since_last_event} days without fall.")
@dp.message(Command(commands=["start"]), StateFilter(None))
async def start_handler(message: Message, state: FSMContext):
if message.from_user.id not in allowed_users:
await message.answer("You are not allowed to start the bot.")
return
await message.answer("Bot started.")
await state.set_state(States.started)
await state.set_data(data={"state": "started"})
while True:
data = await state.get_data()
if not data.get("state"):
break
is_enabled = await ServiceStatusService().check_server_online()
if not is_enabled:
DBService().insert_failure_event()
await bot.send_message(group_chat_id, "Server is not responding.")
await daily_check()
await asyncio.sleep(60)
@dp.message(Command(commands=["status"]))
async def start_handler(message: Message, state: FSMContext):
if message.from_user.id not in allowed_users:
await message.answer("You are not allowed to check status.")
return
data = await state.get_data()
if not data.get("state"):
await message.answer("Bot stopped.")
else:
await message.answer("Bot started.")
@dp.message(Command(commands=["stop"]), States.started)
async def start_handler(message: Message, state: FSMContext):
if message.from_user.id not in allowed_users:
await message.answer("You are not allowed to stop the bot.")
return
await message.answer("Bot stopped.")
await state.clear()
async def on_startup(dp: Dispatcher):
while True:
try:
await dp.start_polling(bot)
except Exception as e:
await bot.send_message(group_chat_id, "Bot crashed with an exception, check for status.")
logger.exception(f"Exception on running: \n{e}\n")
if __name__ == "__main__":
asyncio.run(on_startup(dp))