Skip to content

Commit

Permalink
Merge pull request #1 from flipperdevices/limness/PD-137-138-to-python
Browse files Browse the repository at this point in the history
[PD-137-138] Pythonic
  • Loading branch information
drunkbatya authored Oct 16, 2024
2 parents aa853c9 + 095d886 commit 22ff04f
Show file tree
Hide file tree
Showing 10 changed files with 1,179 additions and 129 deletions.
73 changes: 56 additions & 17 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,67 @@ on:
push:
branches:
- main
tags:
- '*'
pull_request:

env:
IMAGE_NAME: "ghcr.io/flipperdevices/welcomer-bot"

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
if: ${{ !github.event.pull_request.head.repo.fork }}
steps:
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Login to GHCR
uses: docker/login-action@v1
- name: 'Checkout code'
uses: actions/checkout@v4

- name: 'Set image tag and name'
id: tag
run: |
IMAGE_TAG="0.0.0"
if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
REF=${{ github.ref }};
TAG_FULL=${REF#refs/*/};
IMAGE_TAG=${TAG_FULL//\//_};
fi
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT
- name: 'Login to Docker Hub'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.CR_PAT }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: 'Set up Docker Buildx'
uses: docker/setup-buildx-action@v3

- name: 'Build'
uses: docker/build-push-action@v5
with:
push: false
tags: ${{steps.tag.outputs.image_name}}:${{steps.tag.outputs.image_tag}}
cache-from: type=registry,ref=${{ steps.tag.outputs.image_name }}:buildcache

- name: 'Push'
if: ${{ github.event_name != 'pull_request' }}
uses: docker/build-push-action@v5
with:
push: true
tags: ghcr.io/flipperdevices/welcomer-bot:latest
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
tags: ${{ steps.tag.outputs.image_name }}:${{ steps.tag.outputs.image_tag }}
cache-from: type=registry,ref=${{ steps.tag.outputs.image_name }}:buildcache
cache-to: type=registry,ref=${{ steps.tag.outputs.image_name }}:buildcache,mode=max

- name: 'Trigger k8s to use new image'
if: ${{ (github.event_name != 'pull_request')
&& (steps.tag.outputs.image_tag != '0.0.0')
&& (!endsWith(github.event.client_payload.image_tag, '-rc')) }}
uses: peter-evans/repository-dispatch@v2
with:
repository: ${{ secrets.INFRASTRUCTURE_REPO }}
token: ${{ secrets.K8S_GITHUB_PAT }}
event-type: welcomer-bot-deploy
client-payload: '{"image_tag": "${{steps.tag.outputs.image_tag}}"}'
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.DS_Store
.vscode
.vscode
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
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
19 changes: 8 additions & 11 deletions Dockerfile
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"]
5 changes: 0 additions & 5 deletions go.mod

This file was deleted.

45 changes: 0 additions & 45 deletions go.sum

This file was deleted.

50 changes: 0 additions & 50 deletions main.go

This file was deleted.

78 changes: 78 additions & 0 deletions main.py
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())
Loading

0 comments on commit 22ff04f

Please sign in to comment.