-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from flipperdevices/limness/PD-137-138-to-python
[PD-137-138] Pythonic
- Loading branch information
Showing
10 changed files
with
1,179 additions
and
129 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
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,2 +1,2 @@ | ||
.DS_Store | ||
.vscode | ||
.vscode |
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,31 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.4.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: check-docstring-first | ||
- id: check-json | ||
- id: check-toml | ||
- id: debug-statements | ||
|
||
- repo: https://github.com/charliermarsh/ruff-pre-commit | ||
rev: v0.0.254 | ||
hooks: | ||
- id: ruff | ||
|
||
- repo: https://github.com/pycqa/isort | ||
rev: 5.12.0 | ||
hooks: | ||
- id: isort | ||
name: isort (python) | ||
args: [ "--profile", "black" ] | ||
|
||
- repo: https://github.com/ambv/black | ||
rev: 23.3.0 | ||
hooks: | ||
- id: black | ||
language_version: python3.11 | ||
args: | ||
- --skip-string-normalization | ||
- --line-length=120 |
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,16 +1,13 @@ | ||
FROM golang:alpine as builder | ||
FROM python:3.11-alpine | ||
|
||
WORKDIR /app | ||
COPY go.mod go.sum ./ | ||
COPY /pyproject.toml /poetry.lock ./ | ||
|
||
RUN go mod download | ||
COPY . . | ||
|
||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o /go/bin/app . | ||
RUN apk add --no-cache curl gcc libffi-dev musl-dev | ||
|
||
RUN pip install poetry && \ | ||
poetry config virtualenvs.create false && \ | ||
poetry install | ||
|
||
FROM alpine | ||
|
||
COPY --from=builder /go/bin/app /go/bin/app | ||
COPY . . | ||
|
||
ENTRYPOINT ["/go/bin/app"] | ||
ENTRYPOINT ["python", "main.py"] |
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,78 @@ | ||
import asyncio | ||
import os | ||
|
||
from aiogram import types, Dispatcher, Bot | ||
from pygelf import GelfTcpHandler | ||
from loguru import logger | ||
|
||
if os.getenv("KUBERNETES_NAMESPACE") and os.getenv("GELF_HOST") and os.getenv("GELF_PORT"): | ||
handler = GelfTcpHandler( | ||
host=os.getenv("GELF_HOST"), | ||
port=os.getenv("GELF_PORT"), | ||
_kubernetes_namespace_name=os.getenv("KUBERNETES_NAMESPACE"), | ||
_kubernetes_labels_app=os.getenv("KUBERNETES_APP"), | ||
_kubernetes_container_name=os.getenv("KUBERNETES_CONTAINER"), | ||
_kubernetes_pod_name=os.getenv("HOSTNAME"), | ||
) | ||
logger.add(handler) | ||
|
||
|
||
bot = Bot(token=os.getenv("TG_TOKEN"), parse_mode='HTML') | ||
dp = Dispatcher() | ||
|
||
|
||
user_requests = {} | ||
|
||
|
||
@dp.chat_join_request() | ||
async def join(request: types.ChatJoinRequest): | ||
welcome_text_path = os.path.join(os.getenv('MESSAGES_FOLDER'), f"{request.chat.id}.txt") | ||
|
||
if not os.path.exists(welcome_text_path): | ||
logger.error(f'Can\'t load welcome message in path: {welcome_text_path}') | ||
return | ||
|
||
with open(welcome_text_path, '+r') as reader: | ||
welcome_text = reader.read() | ||
|
||
message = await bot.send_message( | ||
chat_id=request.user_chat_id, | ||
text=welcome_text, | ||
) | ||
|
||
# wait a few second before submitting the approval | ||
await asyncio.sleep(int(os.getenv("WAIT_BEFORE_APPROVE", 10))) | ||
|
||
await bot.edit_message_reply_markup( | ||
chat_id=request.user_chat_id, | ||
message_id=message.message_id, | ||
reply_markup=types.InlineKeyboardMarkup( | ||
inline_keyboard=[ | ||
[ | ||
types.InlineKeyboardButton(text='Все понятно!', callback_data="approve_user"), | ||
], | ||
], | ||
resize_keyboard=True, | ||
), | ||
) | ||
user_requests[request.user_chat_id] = request | ||
|
||
|
||
@dp.callback_query(lambda c: c.data == 'approve_user') | ||
async def approve_request(callback_query: types.CallbackQuery): | ||
if (request := user_requests.get(callback_query.from_user.id)) is not None: | ||
await request.approve() | ||
del user_requests[callback_query.from_user.id] | ||
|
||
# hide approve button | ||
await bot.edit_message_reply_markup( | ||
chat_id=callback_query.from_user.id, message_id=callback_query.message.message_id, reply_markup=None | ||
) | ||
|
||
|
||
async def start(): | ||
await dp.start_polling(bot) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.get_event_loop().run_until_complete(start()) |
Oops, something went wrong.