diff --git a/bot/main.py b/bot/main.py index ad09da5..107167b 100644 --- a/bot/main.py +++ b/bot/main.py @@ -1,10 +1,11 @@ +import asyncio + from aiogram import Bot, Dispatcher from aiogram.client.default import DefaultBotProperties from aiogram.enums import ParseMode from aiogram.filters import CommandStart -from aiogram.types import Message, Poll, InputPollOption +from aiogram.types import Message, Poll, PollAnswer, InputPollOption from apscheduler.schedulers.asyncio import AsyncIOScheduler -from redis import Redis import config @@ -16,17 +17,7 @@ dp = Dispatcher() async def command_start_handler( message: Message, ): - with Redis( - db=3, - ) as redis: - redis.set( - name='chat_id', - value=message.chat.id, - ) - chat_id = redis.get( - name='chat_id', - ) - assert int(chat_id) == message.chat.id + config.Dynamic.set('chat_id', message.chat.id) await message.answer('Чат успешно зарегистрирован!') @@ -42,7 +33,7 @@ scheduler = AsyncIOScheduler() async def send_mood_poll() -> Poll: message = await bot.send_poll( - chat_id=-1002246469549, + chat_id=config.Dynamic.get('chat_id'), question='Оцените свое состояние на текущую минуту', options=[ InputPollOption( @@ -78,9 +69,9 @@ async def send_mood_poll() -> Poll: return message.poll -async def send_dinner_poll() -> Poll: +async def send_lunch_poll(): message = await bot.send_poll( - chat_id=-1002246469549, + chat_id=config.Dynamic.get('chat_id'), question='Какие у вас планы на обед?', options=[ InputPollOption( @@ -101,12 +92,23 @@ async def send_dinner_poll() -> Poll: is_closed=False, disable_notification=True, ) - return message.poll + config.Dynamic.set('lunch_poll', message.poll.id) -async def send_dinner_delivery_poll() -> Poll: +@dp.poll_answer() +async def get_lunch_poll_result( + poll_answer: PollAnswer, +): + if poll_answer.poll_id == config.Dynamic.get('lunch_poll'): + await bot.send_message( + chat_id=config.Dynamic.get('chat_id'), + text='%s, %s' % (poll_answer.user.first_name, poll_answer.option_ids), + ) + + +async def send_lunch_delivery_poll() -> Poll: message = await bot.send_poll( - chat_id=-1002246469549, + chat_id=config.Dynamic.get('chat_id'), question='Где будем заказывать?', options=[ InputPollOption( @@ -137,16 +139,23 @@ async def on_startup( func=send_mood_poll, trigger='cron', day_of_week='mon-fri', - hour=17, - minute=32, + hour=22, + minute=35, ) scheduler.add_job( - func=send_dinner_poll, + func=send_lunch_poll, trigger='cron', day_of_week='mon-fri', - hour=17, - minute=17, + hour=23, + minute=55, ) + # scheduler.add_job( + # func=get_lunch_poll_result, + # trigger='cron', + # day_of_week='mon-fri', + # hour=23, + # minute=19, + # ) scheduler.start() diff --git a/config/__init__.py b/config/__init__.py index 1306899..76f014b 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -1 +1 @@ -from .main import Telegram +from .main import Telegram, Dynamic diff --git a/config/main.py b/config/main.py index 1737336..72abd9f 100644 --- a/config/main.py +++ b/config/main.py @@ -1,6 +1,8 @@ from configparser import RawConfigParser import os +from redis import Redis + cwd = os.getcwd() config = RawConfigParser() @@ -17,3 +19,32 @@ class Telegram: section='Telegram', option='token', ) + + +class Dynamic: + @classmethod + def get( + cls, + key: str, + ): + with Redis( + db=3, + ) as redis: + return redis.get( + name=key, + ) + + @classmethod + def set( + cls, + key: str, + value, + ): + with Redis( + db=3, + decode_responses=True, + ) as redis: + redis.set( + name=key, + value=value, + ) diff --git a/database.sql b/database.sql index 10cec9f..967e951 100644 --- a/database.sql +++ b/database.sql @@ -6,4 +6,4 @@ create table polls ( ); create table mood_polls () inherits (polls); -create table dinner_polls () inherits (polls); +create table lunch_polls () inherits (polls);