Skip to content

Commit 296c869

Browse files
authored
Merge pull request #2 from exmanka/add-poetry
### Added - [`469f3bc`](469f3bc) — add Poetry package manager ### Changed - [`5e9244f`](5e9244f) — update PostgreSQL version to 17.7 - [`5e9244f`](5e9244f) — update PostgreSQL Dockerfile - [`469f3bc`](469f3bc) — update telegram-bot Dockerfile ### Fixed - [`9e94475`](9e94475) — fix telegram_file_id VARCHAR size - [`9e94475`](9e94475) — fix expired subscription renew bug - [`9e94475`](9e94475) — fix sending message to everyone error
2 parents 7e33273 + de6ff88 commit 296c869

File tree

12 files changed

+2569
-35
lines changed

12 files changed

+2569
-35
lines changed

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# GitLab
6+
.gitlab-ci.yml
7+
8+
# Jenkins
9+
Jenkinsfile
10+
11+
# Dotenv
12+
.env

.env

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ POSTGRES_USER=${POSTGRES_USER:-docker}
99
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-XXXXXXXXXXXXXXXXX...}
1010

1111
# Specify any postgres database name, but this one is preferable
12-
POSTGRES_DB=${POSTGRES_DB:-ksivpn-tgbot-postgres-db}
12+
POSTGRES_DB=${POSTGRES_DB:-ksivpn}
1313

1414
# [!] Set dbms country-language, where locale is global language and lc-time is dates
1515
# and times formatting language.
1616
# See more in documentation: https://www.postgresql.org/docs/current/locale.html
17-
POSTGRES_INITDB_ARGS=${POSTGRES_INITDB_ARGS:-'--locale=en_US.UTF-8 --lc-time=ru_RU.UTF-8'}
17+
POSTGRES_INITDB_ARGS=${POSTGRES_INITDB_ARGS:---locale=en_US.UTF-8 --lc-time=ru_RU.UTF-8}
1818

1919
# [!] Set additional country-language to be installed in postgres container.
2020
# HAVE TO BE THE SAME AS xx_XX IN --lc-time OPTION (you need to install language for using it)
@@ -43,9 +43,7 @@ LOCALIZATION_LANGUAGE=${LOCALIZATION_LANGUAGE:-en}
4343

4444
#################### Ports for Docker ports mapping ####################
4545
# Specify Docker Host Machine ports used for port mapping
46-
BOT_HTTP_PORT=${BOT_HTTP_PORT:-8080}
47-
BOT_HTTPS_PORT=${BOT_HTTPS_PORT:-8443}
4846
POSTGRES_PORT=${POSTGRES_PORT:-54320}
4947

5048
#################### Docker Images tags ####################
51-
TAG=${TAG:-latest}
49+
TAG=${TAG:-local}

build/bot/Dockerfile

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1-
FROM python:3.10-slim
1+
FROM python:3.10.19-slim
22

3-
WORKDIR /usr/src/tgbot
3+
# User settings
4+
ARG TGBOT_UID=1666
5+
ARG TGBOT_GID=1666
6+
ARG TGBOT_HOME="/home/ksivpn-tgbot"
47

5-
COPY ./build/bot/requirements.txt ./
6-
RUN pip install --upgrade pip; pip install --no-cache-dir -r requirements.txt
8+
# Create user, group and home directory
9+
RUN groupadd --gid ${TGBOT_UID} ksivpn-tgbot && \
10+
useradd --uid ${TGBOT_UID} -g ksivpn-tgbot --home-dir ${TGBOT_HOME} --create-home --shell /bin/bash ksivpn-tgbot
711

8-
COPY . .
12+
# Poetry environment variables
13+
ENV POETRY_NO_INTERACTION=1 \
14+
POETRY_VIRTUALENVS_CREATE=false \
15+
POETRY_CACHE_DIR=/var/cache/pypoetry \
16+
POETRY_HOME=/opt/poetry
17+
ENV PATH="$POETRY_HOME/bin:$PATH"
918

10-
CMD [ "python", "./main.py"]
19+
# Install curl
20+
RUN apt-get update && \
21+
apt-get install --no-install-recommends -y curl && \
22+
rm -rf /var/lib/apt/lists/*
23+
24+
# Install Poetry
25+
RUN curl -sSL https://install.python-poetry.org | python3 -
26+
27+
# Set working directory
28+
WORKDIR ${TGBOT_HOME}
29+
30+
# Copy and install dependencies
31+
COPY --chown=ksivpn-tgbot:ksivpn-tgbot build/bot/pyproject.toml build/bot/poetry.lock ./
32+
RUN poetry install --no-ansi
33+
34+
# Copy source
35+
COPY --chown=ksivpn-tgbot:ksivpn-tgbot . .
36+
37+
# Set user
38+
USER ksivpn-tgbot
39+
40+
# Set entrypoint and cmd
41+
ENTRYPOINT ["python"]
42+
CMD ["main.py"]

build/bot/poetry.lock

Lines changed: 2354 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/bot/pyproject.toml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[project]
2+
name = "ksivpn-telegram-bot"
3+
version = "1.0.2"
4+
description = "Multifunctional telegram bot for ksiVPN project based on aiogram framework."
5+
authors = [
6+
{name = "exmanka",email = "exmanka@gmail.com"}
7+
]
8+
license = {text = "BSD-3-Clause"}
9+
readme = "README.md"
10+
requires-python = "~=3.10"
11+
dependencies = [
12+
"aiofiles (==23.2.1)",
13+
"aiogram (==2.25.1)",
14+
"aiohttp (==3.8.6)",
15+
"aiosignal (==1.3.1)",
16+
"annotated-types (==0.6.0)",
17+
"anyio (==4.2.0)",
18+
"apscheduler (==3.10.4)",
19+
"asgiref (==3.7.2)",
20+
"async-property (==0.2.2)",
21+
"async-timeout (==4.0.3)",
22+
"asyncpg (==0.29.0)",
23+
"asyncstdlib (==3.12.0)",
24+
"attrs (==23.2.0)",
25+
"babel (==2.9.1)",
26+
"beautifulsoup4 (==4.12.2)",
27+
"blinker (==1.7.0)",
28+
"browser-cookie3 (==0.19.1)",
29+
"bs4 (==0.0.1)",
30+
"certifi (==2023.11.17)",
31+
"cffi (==1.16.0)",
32+
"charset-normalizer (==3.3.2)",
33+
"click (==8.1.7)",
34+
"curl-cffi (==0.6.0b7)",
35+
"duckduckgo-search (==4.1.1)",
36+
"exceptiongroup (==1.2.0)",
37+
"fastapi (==0.108.0)",
38+
"flask (==3.0.0)",
39+
"frozenlist (==1.4.1)",
40+
"g4f (==0.1.9.3)",
41+
"h11 (==0.14.0)",
42+
"idna (==3.6)",
43+
"itsdangerous (==2.1.2)",
44+
"jeepney (==0.8.0)",
45+
"jinja2 (==3.1.2)",
46+
"js2py (==0.74)",
47+
"loguru (==0.7.2)",
48+
"lxml (==5.0.0)",
49+
"lz4 (==4.3.3)",
50+
"magic-filter (==1.0.12)",
51+
"markupsafe (==2.1.3)",
52+
"mmh3 (==4.0.1)",
53+
"multidict (==6.0.4)",
54+
"nest-asyncio (==1.5.8)",
55+
"numpy (==1.26.3)",
56+
"outcome (==1.3.0.post0)",
57+
"pillow (==10.2.0)",
58+
"platformdirs (==4.1.0)",
59+
"py-arkose-generator (==0.0.0.2)",
60+
"pycparser (==2.21)",
61+
"pycryptodome (==3.19.1)",
62+
"pycryptodomex (==3.19.1)",
63+
"pydantic (==2.5.2)",
64+
"pydantic-core (==2.14.5)",
65+
"pyexecjs (==1.5.1)",
66+
"pyjsparser (==2.7.1)",
67+
"pysocks (==1.7.1)",
68+
"pytz (==2023.3.post1)",
69+
"requests (==2.31.0)",
70+
"selenium (==4.16.0)",
71+
"six (==1.16.0)",
72+
"sniffio (==1.3.0)",
73+
"sortedcontainers (==2.4.0)",
74+
"soupsieve (==2.5)",
75+
"starlette (==0.32.0.post1)",
76+
"trio (==0.23.2)",
77+
"trio-websocket (==0.11.1)",
78+
"typing-extensions (==4.9.0)",
79+
"tzlocal (==5.2)",
80+
"undetected-chromedriver (==3.5.4)",
81+
"urllib3 (==2.1.0)",
82+
"uvicorn (==0.25.0)",
83+
"waitress (==2.1.2)",
84+
"websockets (==12.0)",
85+
"werkzeug (==3.0.1)",
86+
"wsproto (==1.2.0)",
87+
"yarl (==1.9.4)"
88+
]
89+
90+
[tool.poetry]
91+
package-mode = false
92+
93+
[project.urls]
94+
homepage = "https://github.com/exmanka"
95+
repository = "https://github.com/exmanka/ksiVPN-telegram-bot"
96+
97+
[build-system]
98+
requires = ["poetry-core>=2.0.0,<3.0.0"]
99+
build-backend = "poetry.core.masonry.api"

build/bot/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# A lot of packages are needed for g4f module to work
21
aiofiles==23.2.1
32
aiogram==2.25.1
43
aiohttp==3.8.6

build/database/Dockerfile

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
FROM postgres:14.9
1+
FROM postgres:17.7
22

3-
COPY init-database.sql /docker-entrypoint-initdb.d/init-database.sql
4-
ARG ADDITIONAL_LANGUAGE
5-
RUN localedef -i $ADDITIONAL_LANGUAGE -c -f UTF-8 -A /usr/share/locale/locale.alias $ADDITIONAL_LANGUAGE.UTF-8
6-
ENV LANG en_US.utf8
3+
# Add new language locale
4+
ARG ADDITIONAL_LANGUAGE="ru_RU"
5+
RUN localedef -i "$ADDITIONAL_LANGUAGE" -c -f UTF-8 -A /usr/share/locale/locale.alias "$ADDITIONAL_LANGUAGE".UTF-8
6+
7+
ENV LANG="en_US.utf8"
78

8-
RUN apt-get update && apt-get install cron -y
9-
COPY init-database-backup-cron.sh /usr/local/bin/
9+
# Install cron
10+
RUN apt-get update && \
11+
apt-get install --no-install-recommends -y cron && \
12+
rm -rf /var/lib/apt/lists/*
13+
14+
# Add initialization SQL script and backup cron script
15+
COPY init-database.sql /docker-entrypoint-initdb.d/init-database.sql
16+
COPY init-database-backup-cron.sh /usr/local/bin/init-database-backup-cron.sh
1017
RUN chmod +x /usr/local/bin/init-database-backup-cron.sh
1118

19+
# Set entrypoint and cmd
1220
ENTRYPOINT [ "init-database-backup-cron.sh" ]
13-
CMD ["docker-entrypoint.sh", "postgres"]
21+
CMD [ "docker-entrypoint.sh", "postgres" ]
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

3+
# Set strict mode for script
4+
set -euo pipefail
35

4-
echo "Running script for creating scheduler for daily database backups:"
5-
echo "- starting cron service"
6+
# Set colors for logging
7+
LOG_COLOR="\e[95m"
8+
LOG_COLOR_CLEAR="\e[0m"
9+
10+
# Start cron service
11+
echo -e "${LOG_COLOR}[init-database-backup-cron.sh] Running script for creating scheduler for daily database backups${LOG_COLOR_CLEAR}"
12+
echo -e "${LOG_COLOR}[init-database-backup-cron.sh] Starting cron service${LOG_COLOR_CLEAR}"
613
service cron start
7-
echo "- adding cron job to schedule database backups"
14+
15+
# Add cron job for daily database backups at 19:55
16+
echo -e "${LOG_COLOR}[init-database-backup-cron.sh] Adding cron job to schedule database backups${LOG_COLOR_CLEAR}"
817
(echo "55 19 * * * pg_dump -U $POSTGRES_USER --no-password --clean --if-exists --format=plain $POSTGRES_DB | gzip > /var/lib/postgresql/backups/db-backup.gz") | crontab -
9-
echo "- executing something important for postgres container integrity"
10-
exec "$@"
18+
echo -e "${LOG_COLOR}[init-database-backup-cron.sh] Executing Postgres entrypoint${LOG_COLOR_CLEAR}"
19+
echo
20+
exec docker-entrypoint.sh "$@"

build/database/init-database.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CREATE TABLE clients_subscriptions (
2929
paid_months_counter INT NOT NULL DEFAULT 0,
3030
expiration_date TIMESTAMP NOT NULL
3131
);
32-
INSERT INTO clients_subscriptions(client_id, sub_id, paid_months_counter, expiration_date) VALUES(1, 1, 10, TIMESTAMP '2024-01-01 00:00'); -- '@exmanka'
32+
INSERT INTO clients_subscriptions(client_id, sub_id, paid_months_counter, expiration_date) VALUES(1, 1, 10, TIMESTAMP '2024-01-01 00:00');
3333

3434

3535
CREATE OR REPLACE FUNCTION create_ref_promocode() RETURNS text
@@ -122,7 +122,7 @@ CREATE TABLE configurations (
122122
location_id SMALLINT NOT NULL REFERENCES configurations_locations(id) ON DELETE CASCADE,
123123
os osEnum NOT NULL,
124124
file_type fileTypeEnum NOT NULL,
125-
telegram_file_id VARCHAR(256) UNIQUE NOT NULL,
125+
telegram_file_id VARCHAR(512) UNIQUE NOT NULL,
126126
date_of_receipt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
127127
);
128128
INSERT INTO configurations(client_id, protocol_id, location_id, os, file_type, telegram_file_id, date_of_receipt) VALUES(1, 1, 1, 'Android', 'link', 'vless://link_or_telegram_file_id_here', 'EPOCH');

docker-compose.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ services:
1010
restart: unless-stopped
1111
env_file:
1212
- .env
13-
ports:
14-
- ${BOT_HTTP_PORT}:80
15-
- ${BOT_HTTPS_PORT}:443
1613
networks:
1714
- ksivpn-net
1815
volumes:

0 commit comments

Comments
 (0)