commit a763de43353906eecfd8fc9049f581c99ceb8ec1 Author: csasq Date: Mon Aug 12 17:25:30 2024 +0300 Разработан набросок чат-бота diff --git a/bot/__init__.py b/bot/__init__.py new file mode 100644 index 0000000..c64e361 --- /dev/null +++ b/bot/__init__.py @@ -0,0 +1 @@ +from .main import dp, bot diff --git a/bot/main.py b/bot/main.py new file mode 100644 index 0000000..e798908 --- /dev/null +++ b/bot/main.py @@ -0,0 +1,143 @@ +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 apscheduler.schedulers.asyncio import AsyncIOScheduler + +import config + + +dp = Dispatcher() +loop = asyncio.get_event_loop() + + +@dp.message(CommandStart()) +async def command_start_handler( + message: Message, +): + print(message.chat.id) + await message.answer('Чат успешно зарегистрирован!') + + +bot = Bot( + token=config.Telegram.token, + default=DefaultBotProperties( + parse_mode=ParseMode.HTML, + ), +) + +scheduler = AsyncIOScheduler() + + +async def send_mood_poll() -> Poll: + await bot.send_poll( + chat_id=-1002246469549, + question='Оцените свое состояние на текущую минуту', + options=[ + InputPollOption( + text='😄', + ), + InputPollOption( + text='🤪', + ), + InputPollOption( + text='🫠', + ), + InputPollOption( + text='☠️', + ), + InputPollOption( + text='🤡', + ), + InputPollOption( + text='😟', + ), + InputPollOption( + text='😩', + ), + InputPollOption( + text='😡', + ), + ], + is_anonymous=False, + allows_multiple_answers=False, + is_closed=False, + disable_notification=True, + ) + + +async def send_dinner_poll() -> Poll: + message = await bot.send_poll( + chat_id=-1002246469549, + question='Какие у вас планы на обед?', + options=[ + InputPollOption( + text='🍽️ Пойду в общепит', + ), + InputPollOption( + text='📦 Хочу заказать в офис', + ), + InputPollOption( + text='🥪 Всё своё ношу с собой', + ), + InputPollOption( + text='😴 Хочу спать', + ), + ], + is_anonymous=False, + allows_multiple_answers=False, + is_closed=False, + disable_notification=True, + ) + return message.poll + + +async def send_dinner_delivery_poll() -> Poll: + await bot.send_poll( + chat_id=-1002246469549, + question='Где будем заказывать?', + options=[ + InputPollOption( + text='Dark Side', + ), + InputPollOption( + text='Самокат', + ), + InputPollOption( + text='...', + ), + InputPollOption( + text='...', + ), + ], + is_anonymous=False, + allows_multiple_answers=False, + is_closed=False, + disable_notification=True, + ) + + +async def on_startup( + dispatcher: Dispatcher, +): + scheduler.add_job( + func=send_mood_poll, + trigger='cron', + day_of_week='mon-fri', + hour=17, + minute=14, + ) + scheduler.add_job( + func=send_dinner_poll, + trigger='cron', + day_of_week='mon-fri', + hour=17, + minute=17, + ) + scheduler.start() + + +dp.startup.register(on_startup) diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..1306899 --- /dev/null +++ b/config/__init__.py @@ -0,0 +1 @@ +from .main import Telegram diff --git a/config/main.py b/config/main.py new file mode 100644 index 0000000..1737336 --- /dev/null +++ b/config/main.py @@ -0,0 +1,19 @@ +from configparser import RawConfigParser +import os + + +cwd = os.getcwd() +config = RawConfigParser() +config.read( + filenames=os.path.join( + cwd, + 'config.ini', + ), +) + + +class Telegram: + token = config.get( + section='Telegram', + option='token', + ) diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..10cec9f --- /dev/null +++ b/database.sql @@ -0,0 +1,9 @@ +create table polls ( + id bigint not null, + date date not null, + is_complete boolean default false not null, + primary key (id) +); + +create table mood_polls () inherits (polls); +create table dinner_polls () inherits (polls); diff --git a/main.py b/main.py new file mode 100644 index 0000000..4f5265b --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +import asyncio + +from bot import bot, dp + + +if __name__ == "__main__": + asyncio.run(dp.run_polling(bot)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b10bb02 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +aiogram +APScheduler +psycopg[binary]