-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ad2372
commit f80976a
Showing
39 changed files
with
310 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__pycache__/ | ||
.venv/ | ||
.venv*/ | ||
.env.development | ||
.env | ||
data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .models import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class SendTelegramMessage(BaseModel): | ||
chat_id: int | ||
text: str | ||
parse_mode: str = "html" | ||
|
||
class SendMediaTelegramMessage(SendTelegramMessage): | ||
files: list[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .router import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from faststream import BaseMiddleware | ||
|
||
from infrastructure.db import CTX_SESSION, get_session | ||
|
||
class DatabaseMiddleware(BaseMiddleware): | ||
async def on_receive(self): | ||
CTX_SESSION.set(get_session()) | ||
return await super().on_receive() | ||
|
||
async def after_processed(self, exc_type, exc_val, exc_tb): | ||
await CTX_SESSION.get().close() | ||
return await super().after_processed(exc_type, exc_val, exc_tb) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from faststream.rabbit.fastapi import RabbitRouter | ||
|
||
from config import RABBIT_URL | ||
from .middlewares import DatabaseMiddleware | ||
|
||
|
||
rabbit = RabbitRouter(RABBIT_URL, include_in_schema=False, middlewares=[DatabaseMiddleware]) | ||
broker = rabbit.broker |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .admin import router as AdminRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from faststream.rabbit import RabbitRouter | ||
|
||
from application.user import UserService | ||
from domain.user import BanUser, BannedUser, UserRepository | ||
from infrastructure.db import CTX_SESSION | ||
|
||
|
||
router = RabbitRouter(prefix="adm_") | ||
|
||
@router.subscriber("ban") | ||
@router.publisher("banned") | ||
async def ban_user(data: BanUser) -> BannedUser: | ||
user = await UserRepository().get(data.user_id) | ||
if not user: | ||
return BannedUser(msg_id = data.msg_id, success = False) | ||
await UserService().ban(user, data.reason) | ||
await CTX_SESSION.get().commit() | ||
return BannedUser(msg_id = data.msg_id, success = True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"python.autoComplete.extraPaths": [ | ||
"./src" | ||
], | ||
"python.analysis.extraPaths": [ | ||
"./src" | ||
], | ||
"python.defaultInterpreterPath": "../.venv-bot/Scripts/python.exe" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.12-alpine | ||
|
||
WORKDIR /bot | ||
COPY ./requirements.txt /app/requirements.txt | ||
RUN pip install --no-cache-dir -r /app/requirements.txt | ||
COPY ./src /bot | ||
|
||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
CMD [ "python", "main.py" ] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os, dotenv | ||
|
||
IS_PROD = os.getenv("IS_PROD", False) | ||
if not IS_PROD: | ||
dotenv.load_dotenv(".env.development") | ||
|
||
TG_TOKEN = os.environ.get("TG_TOKEN") | ||
TG_ADMIN_CHAT = int(os.environ.get("TG_ADMIN_CHAT")) | ||
RABBIT_URL = os.environ.get("RABBIT_URL") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import asyncio | ||
import logging | ||
from rabbit.app import app | ||
from telegram.dispatcher import dp | ||
from telegram.bot import bot | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
async def run(): | ||
asyncio.ensure_future(dp.start_polling(bot)) | ||
await app.run() | ||
|
||
asyncio.run(run()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .telegram import * | ||
from .admin import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class BanUser(BaseModel): | ||
msg_id: int | ||
user_id: int | ||
reason: str | ||
|
||
class BannedUser(BaseModel): | ||
msg_id: int | ||
success: bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Literal | ||
from pydantic import BaseModel | ||
|
||
|
||
class SendMessage(BaseModel): | ||
chat_id: int | ||
text: str | ||
parse_mode: str = "html" | ||
|
||
class SendAdminMessage(SendMessage): | ||
chat_id: Literal[-1] | ||
|
||
class SendMediaMessage(SendMessage): | ||
files: list[str] | ||
|
||
class SendAdminMediaMessage(SendAdminMessage, SendMediaMessage): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from faststream import FastStream | ||
|
||
from .broker import broker | ||
from .routers import TelegramRouter, AdminRouter | ||
|
||
|
||
app = FastStream(broker) | ||
broker.include_routers(TelegramRouter, AdminRouter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from faststream.rabbit import RabbitBroker | ||
|
||
from config import RABBIT_URL | ||
|
||
|
||
broker = RabbitBroker(RABBIT_URL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .telegram import router as TelegramRouter | ||
from .admin import router as AdminRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from faststream.rabbit import RabbitRouter | ||
from aiogram.types import InputMediaPhoto, URLInputFile | ||
|
||
from config import TG_ADMIN_CHAT | ||
from models import BannedUser | ||
from telegram.bot import bot | ||
|
||
|
||
router = RabbitRouter("adm_") | ||
|
||
@router.subscriber("banned") | ||
async def msg(ban: BannedUser) -> None: | ||
await bot.send_message( | ||
chat_id = TG_ADMIN_CHAT, | ||
reply_to_message_id = ban.msg_id, | ||
text = "🚫 Пользователь заблокирован" if ban.success else "❕ Неверный пользователь" | ||
) |
Oops, something went wrong.