-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (37 loc) · 1.69 KB
/
main.py
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
import logging
import sqlite3
import os
from aiogram.dispatcher import FSMContext
from aiogram import Bot, Dispatcher, executor, types
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from aiogram.contrib.fsm_storage.mongo import MongoStorage
from utils import get_last_block, get_chat_ids
from config import DB_PATH, SECONDS, DB_MONGO_NAME, DB_HOST, DB_PORT, TEXT_MSG, API_TOKEN
PREV_BLOCK = ""
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
bot = Bot(token=API_TOKEN)
storage = MongoStorage(host=DB_HOST, port=DB_PORT, db_name=DB_MONGO_NAME)
dp = Dispatcher(bot, storage=storage)
scheduler = AsyncIOScheduler()
@dp.message_handler(commands=['start', 'get'])
async def send_welcome(message: types.Message, state: FSMContext):
global PREV_BLOCK
async with state.proxy() as data:
data['stub'] = "#"
PREV_BLOCK = cur_block = get_last_block(DB_PATH)
logging.info(f'command: [start, get], user: {message.from_user.id}, chat_id: {message.chat.id}, last_block: {cur_block}')
await message.reply(f'{TEXT_MSG}{cur_block}')
async def schedule_last_block(bot: Bot):
global PREV_BLOCK
cur_block = get_last_block(DB_PATH)
if cur_block != PREV_BLOCK:
PREV_BLOCK = cur_block
for chat_id in get_chat_ids():
await bot.send_message(text=f'{TEXT_MSG}{cur_block}', chat_id=chat_id)
logging.info(f'schedule: Chat_ID: {chat_id}, New block is: {cur_block}')
else:
logging.info('schedule: No new blocks')
scheduler.add_job(schedule_last_block, "interval", seconds=SECONDS, args=(bot,))
if __name__ == '__main__':
scheduler.start()
executor.start_polling(dp, skip_updates=True)