initial commit
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
0
app/handlers/__init__.py
Normal file
0
app/handlers/__init__.py
Normal file
10
app/handlers/events.py
Normal file
10
app/handlers/events.py
Normal file
@ -0,0 +1,10 @@
|
||||
from app.settings import bot, secrets
|
||||
from app import views
|
||||
|
||||
|
||||
async def start_bot():
|
||||
await bot.send_message(secrets.admin_id, views.start_bot_message())
|
||||
|
||||
|
||||
async def stop_bot():
|
||||
await bot.send_message(secrets.admin_id, views.stop_bot_message())
|
9
app/handlers/message.py
Normal file
9
app/handlers/message.py
Normal file
@ -0,0 +1,9 @@
|
||||
from aiogram.types import Message, MessageEntity, PhotoSize, ReactionTypeEmoji
|
||||
|
||||
from app.utils.send_to_notion import send_to_notion
|
||||
|
||||
|
||||
async def parse_message(message: Message):
|
||||
await send_to_notion(message)
|
||||
|
||||
await message.react([ReactionTypeEmoji(emoji="👌")])
|
6
app/handlers/simple.py
Normal file
6
app/handlers/simple.py
Normal file
@ -0,0 +1,6 @@
|
||||
from aiogram.types import Message
|
||||
from app import views
|
||||
|
||||
|
||||
async def start_command(message: Message):
|
||||
await message.answer(views.start_text())
|
0
app/middlewares/__init__.py
Normal file
0
app/middlewares/__init__.py
Normal file
18
app/middlewares/admin_middleware.py
Normal file
18
app/middlewares/admin_middleware.py
Normal file
@ -0,0 +1,18 @@
|
||||
from typing import Callable, Dict, Any, Awaitable
|
||||
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.types import TelegramObject, User
|
||||
|
||||
from app.settings import secrets
|
||||
|
||||
|
||||
class AdminMiddleware(BaseMiddleware):
|
||||
async def __call__(
|
||||
self,
|
||||
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
|
||||
event: TelegramObject,
|
||||
data: Dict[str, Any],
|
||||
) -> Any:
|
||||
user: User = data.get("event_from_user")
|
||||
if user.id == secrets.admin_id:
|
||||
return await handler(event, data)
|
33
app/middlewares/album_middleware.py
Normal file
33
app/middlewares/album_middleware.py
Normal file
@ -0,0 +1,33 @@
|
||||
import asyncio
|
||||
from typing import Dict, List, Union, Callable, Any, Awaitable
|
||||
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.types import Message, TelegramObject
|
||||
|
||||
DEFAULT_DELAY = 0.6
|
||||
|
||||
|
||||
class MediaGroupMiddleware(BaseMiddleware):
|
||||
ALBUM_DATA: Dict[str, List[Message]] = {}
|
||||
|
||||
def __init__(self, delay: Union[int, float] = DEFAULT_DELAY):
|
||||
self.delay = delay
|
||||
|
||||
async def __call__(
|
||||
self,
|
||||
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
|
||||
event: Message,
|
||||
data: Dict[str, Any],
|
||||
) -> Any:
|
||||
if not event.media_group_id:
|
||||
return await handler(event, data)
|
||||
|
||||
try:
|
||||
self.ALBUM_DATA[event.media_group_id].append(event)
|
||||
return # Don't propagate the event
|
||||
except KeyError:
|
||||
self.ALBUM_DATA[event.media_group_id] = [event]
|
||||
await asyncio.sleep(self.delay)
|
||||
data["album"] = self.ALBUM_DATA.pop(event.media_group_id)
|
||||
|
||||
return await handler(event, data)
|
25
app/settings.py
Normal file
25
app/settings.py
Normal file
@ -0,0 +1,25 @@
|
||||
import os
|
||||
from typing import Union
|
||||
|
||||
from aiogram import Bot
|
||||
from notion_client import AsyncClient
|
||||
from pydantic import SecretStr
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Secrets(BaseSettings):
|
||||
token: SecretStr
|
||||
admin_id: Union[SecretStr.get_secret_value, int]
|
||||
notion_token: SecretStr
|
||||
imgur_client_id: SecretStr
|
||||
database_id: SecretStr
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
env_file_encoding = "utf-8"
|
||||
|
||||
|
||||
secrets = Secrets()
|
||||
|
||||
notion = AsyncClient(auth=secrets.notion_token.get_secret_value())
|
||||
bot = Bot(token=secrets.token.get_secret_value())
|
0
app/utils/__init__.py
Normal file
0
app/utils/__init__.py
Normal file
79
app/utils/send_to_notion.py
Normal file
79
app/utils/send_to_notion.py
Normal file
@ -0,0 +1,79 @@
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from os import path
|
||||
|
||||
from imgur_python import Imgur
|
||||
from aiogram.types import Message, MessageEntity, PhotoSize
|
||||
|
||||
from app.settings import notion, bot, secrets
|
||||
|
||||
|
||||
async def send_to_notion(message: Message):
|
||||
pattern = r"(https?://[^\s]+|t\.me/[^\s]+)"
|
||||
image_url = None
|
||||
if message.caption:
|
||||
links2 = re.findall(pattern, message.caption)
|
||||
text: str = message.caption
|
||||
photos: PhotoSize = [photo.file_id for photo in message.photo]
|
||||
links: MessageEntity = [
|
||||
link.url for link in message.caption_entities if link.type == "text_link"
|
||||
]
|
||||
if photos:
|
||||
file_name = f"images/{photos[0]}.jpg"
|
||||
await bot.download(message.photo[-1], destination=file_name)
|
||||
imgur_client = Imgur(
|
||||
{"client_id": secrets.imgur_client_id.get_secret_value()}
|
||||
)
|
||||
image = imgur_client.image_upload(
|
||||
path.realpath(file_name), "Untitled", "My first image upload"
|
||||
)
|
||||
image_url = image["response"]["data"]["link"]
|
||||
os.remove(file_name)
|
||||
else:
|
||||
text: str = message.text
|
||||
links2 = re.findall(pattern, message.text)
|
||||
photos = []
|
||||
links: MessageEntity = (
|
||||
[link.url for link in message.entities if link.type == "text_link"]
|
||||
if message.entities
|
||||
else []
|
||||
)
|
||||
if links2:
|
||||
links.extend(links2)
|
||||
links = set(links)
|
||||
properties = {
|
||||
"Name": {"title": [{"text": {"content": text[: text.index("\n")]}}]},
|
||||
"Text": {"rich_text": [{"text": {"content": text}}]},
|
||||
"Added at": {
|
||||
"date": {
|
||||
"start": datetime.now().astimezone(timezone.utc).isoformat(),
|
||||
"end": None,
|
||||
}
|
||||
},
|
||||
}
|
||||
for i, link in enumerate(links, start=1):
|
||||
if i > 4:
|
||||
break
|
||||
properties[f"Link{i}"] = {"url": link}
|
||||
cover = None
|
||||
if photos:
|
||||
properties["Image"] = {
|
||||
"files": [
|
||||
{
|
||||
"name": "image.jpg",
|
||||
"type": "external",
|
||||
"external": {"url": image_url},
|
||||
}
|
||||
]
|
||||
}
|
||||
cover = {"type": "external", "external": {"url": image_url}}
|
||||
icon = {"type": "emoji", "emoji": "🎉"}
|
||||
parent = {
|
||||
"type": "database_id",
|
||||
"database_id": secrets.database_id.get_secret_value(),
|
||||
}
|
||||
|
||||
return await notion.pages.create(
|
||||
parent=parent, properties=properties, icon=icon, cover=cover
|
||||
)
|
10
app/views.py
Normal file
10
app/views.py
Normal file
@ -0,0 +1,10 @@
|
||||
def start_bot_message():
|
||||
return "Бот запущен"
|
||||
|
||||
|
||||
def stop_bot_message():
|
||||
return "Бот остановлен"
|
||||
|
||||
|
||||
def start_text():
|
||||
return """Дратути"""
|
Reference in New Issue
Block a user