Skip to content

Commit

Permalink
Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Natalenko committed Jan 4, 2024
1 parent fb67334 commit 968def6
Show file tree
Hide file tree
Showing 245 changed files with 3,377 additions and 2,288 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
.vscode
53 changes: 6 additions & 47 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,18 @@ repos:
types: [python]
- id: trailing-whitespace

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
hooks:
- id: python-check-blanket-noqa
- id: python-check-mock-methods
- id: python-no-eval
- id: python-no-log-warn
- id: python-use-type-annotations
- id: text-unicode-replacement-char

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py311-plus]

- repo: https://github.com/pycqa/autoflake
rev: v2.2.1
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- --remove-unused-variables
- --ignore-init-module-imports

- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
language_version: python3.11

- repo: https://github.com/pycqa/bandit
rev: 1.7.5
hooks:
- id: bandit
args:
- --aggregate=file
- -iii
- -ll
require_serial: true
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
args: [--profile black]
- id: ruff
args: [--fix]
- id: ruff-format

- repo: local
hooks:
- id: mypy
name: mypy
entry: mypy ./inclusive_dance_bot --config-file ./pyproject.toml
entry: mypy ./idb --config-file ./pyproject.toml
language: python
language_version: python3.11
require_serial: true
Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,13 @@ Environment variables are used to configure the bot for connecting to Telegram,

```bash

TELEGRAM_BOT_TOKEN # your bot token
TELEGRAM_BOT_ADMIN_IDS # Superadmin IDs, who can appoint other admins
APP_TELEGRAM_BOT_TOKEN # your bot token
APP_TELEGRAM_BOT_ADMIN_IDS # Superadmin IDs, who can appoint other admins

DEBUG # Flag for debugging (using in sqlalchemy engine for echo)
APP_DEBUG # Flag for debugging (using in sqlalchemy engine for echo)

POSTGRES_HOST # database host
POSTGRES_PORT # database port
POSTGRES_USER # database user
POSTGRES_PASSWORD # database password
POSTGRES_DB # database name

REDIS_HOST # redis host
REDIS_PORT # redis port
REDIS_PASSWORD # redis password
REDIS_DB # redis db
APP_PG_DSN # DSN of your postgresql database
APP_REDIS_DSN # DSN of your redis storage
```

### Docker
Expand Down
18 changes: 4 additions & 14 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,17 @@ services:
redis:
image: redis
restart: unless-stopped
command: redis-server --requirepass ${REDIS_PASSWORD}
command: redis-server --requirepass $REDIS_PASSWORD

bot:
image: andytakker/inclusive_dance_bot:latest
restart: unless-stopped
command: /wait-for-it.sh -t 15 -h db -p 5432 -- /app/start.sh
environment:
POSTGRES_HOST: db
POSTGRES_PORT: 5432
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_DB: $POSTGRES_DB

PG_URL: postgresql+asyncpg://$POSTGRES_USER:$POSTGRES_PASSWORD@db:5432/$POSTGRES_DB

REDIS_HOST: redis
REDIS_PORT: 6379
REDIS_PASSWORD: $REDIS_PASSWORD
REDIS_DB: $REDIS_DB
APP_REDIS_DSN: redis://:$REDIS_PASSWORD@redis:6379/1
APP_PG_DSN: postgresql+asyncpg://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres:5432/$POSTGRES_DB

TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}
APP_TELEGRAM_BOT_TOKEN: $TELEGRAM_BOT_TOKEN

volumes:
postgres_data:
Expand Down
68 changes: 0 additions & 68 deletions gotify.py

This file was deleted.

File renamed without changes.
13 changes: 7 additions & 6 deletions inclusive_dance_bot/__main__.py → idb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
from aiomisc import Service, entrypoint
from aiomisc_log import basic_config

from inclusive_dance_bot.arguments import get_parser
from inclusive_dance_bot.deps import config_deps
from inclusive_dance_bot.services.bot import AiogramBotService
from inclusive_dance_bot.services.periodic import PeriodicMailingService
from idb.arguments import get_parser
from idb.bot.services.bot import AiogramBotService
from idb.bot.services.periodic import PeriodicMailingService
from idb.deps import config_deps

log = logging.getLogger(__name__)


def main() -> None:
parser = get_parser()
arguments = parser.parse_args()

basic_config(
log_format=arguments.log_level,
level=arguments.log_format,
log_format=arguments.log_format,
level=arguments.log_level,
)
config_deps(arguments=arguments)
services: list[Service] = [
Expand Down
9 changes: 8 additions & 1 deletion inclusive_dance_bot/arguments.py → idb/arguments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import json

from aiogram.enums import ParseMode
from aiomisc_log import LogFormat, LogLevel
from configargparse import ArgumentParser

Expand All @@ -13,7 +14,7 @@ def int_list(s: str) -> list[int]:
if not all(map(lambda x: isinstance(x, int), value)):
raise ValueError
return value
except Exception:
except (json.JSONDecodeError, ValueError):
raise ValueError("This is not list of integers")


Expand Down Expand Up @@ -42,6 +43,12 @@ def get_parser() -> ArgumentParser:
group = parser.add_argument_group("Telegram Bot options")
group.add_argument("--telegram-bot-token", required=True)
group.add_argument("--telegram-bot-admin-ids", type=int_list, default=[])
group.add_argument(
"--telegram-parse-mode",
type=ParseMode,
choices=tuple(ParseMode._member_names_),
default=ParseMode.HTML,
)

group.add_argument("--telegram-periodic-interval", type=int, default=5 * 60)
group.add_argument("--telegram-mailing-gap", type=int, default=2 * 60)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions idb/bot/dialogs/admins/feedbacks/answer/dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from aiogram_dialog import Dialog

from idb.bot.dialogs.admins.feedbacks.answer.windows.confirm import (
window as confirm_window,
)
from idb.bot.dialogs.admins.feedbacks.answer.windows.input_message import (
window as input_message_window,
)

dialog = Dialog(
input_message_window,
confirm_window,
)
44 changes: 44 additions & 0 deletions idb/bot/dialogs/admins/feedbacks/answer/windows/confirm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from datetime import UTC, datetime

from aiogram.types import CallbackQuery
from aiogram_dialog import DialogManager, Window
from aiogram_dialog.widgets.kbd import Button, Row
from aiogram_dialog.widgets.text import Const

from idb.bot.dialogs.admins.states import FeedbackAnswerSG
from idb.bot.dialogs.utils.buttons import CANCEL
from idb.db.uow import UnitOfWork
from idb.logic.answer import create_feedback_answer
from idb.logic.feedback import update_answered_feedback


async def on_click(
c: CallbackQuery, button: Button, dialog_manager: DialogManager
) -> None:
uow: UnitOfWork = dialog_manager.middleware_data["uow"]
feedback_id = int(dialog_manager.start_data["feedback_id"])
text = str(dialog_manager.current_context().widget_data["input_answer"])
answer = await create_feedback_answer(
uow=uow,
feedback_id=feedback_id,
text=text,
from_user_id=dialog_manager.event.from_user.id, # type: ignore[union-attr]
)

await c.bot.send_message( # type: ignore[union-attr]
chat_id=answer.to_user_id,
text=answer.text,
)
await update_answered_feedback(
uow=uow,
feedback_id=feedback_id,
dt=datetime.now(UTC),
)
await dialog_manager.done()


window = Window(
Const("Отправить сообщение?"),
Row(CANCEL, Button(text=Const("📨 Да"), id="send_message", on_click=on_click)),
state=FeedbackAnswerSG.confirm,
)
27 changes: 27 additions & 0 deletions idb/bot/dialogs/admins/feedbacks/answer/windows/input_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from aiogram.types import Message
from aiogram_dialog import DialogManager, Window
from aiogram_dialog.widgets.input import ManagedTextInput, TextInput
from aiogram_dialog.widgets.text import Const

from idb.bot.dialogs.admins.states import FeedbackAnswerSG
from idb.bot.dialogs.utils.buttons import CANCEL


async def on_success_next(
message: Message,
widget: ManagedTextInput[str],
dialog_manager: DialogManager,
value: str,
) -> None:
await dialog_manager.next()


window = Window(
Const("Введите сообщение"),
TextInput(
id="input_answer",
on_success=on_success_next, # type: ignore[arg-type]
),
CANCEL,
state=FeedbackAnswerSG.input_message,
)
15 changes: 15 additions & 0 deletions idb/bot/dialogs/admins/feedbacks/read/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from aiogram_dialog import Dialog

from idb.bot.dialogs.admins.feedbacks.read import (
item,
menu,
new,
viewed,
)

dialog = Dialog(
menu.window,
new.window,
viewed.window,
item.window,
)
Loading

0 comments on commit 968def6

Please sign in to comment.