Skip to content

Commit f3bbfce

Browse files
authored
Merge pull request #34 from alexeyqu/dev
v1.0 release
2 parents b819711 + f776f76 commit f3bbfce

32 files changed

+2575
-786
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TELEGRAM_TOKEN=
2+
TELEGRAM_ERROR_CHAT_ID=
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Formatting check on pull requests
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- dev
7+
8+
jobs:
9+
pre_commit:
10+
runs-on: ubuntu-latest
11+
environment:
12+
name: testing
13+
14+
steps:
15+
- name: checkout repository
16+
uses: actions/checkout@v2
17+
18+
- name: set up Python
19+
uses: actions/setup-python@v2
20+
21+
- name: install pre-commit
22+
run: python -m pip install pre-commit
23+
shell: bash
24+
25+
- name: pip freeze
26+
run: python -m pip freeze --local
27+
shell: bash
28+
29+
- name: run pre-commit
30+
run: pre-commit run --color=always --show-diff-on-failure
31+
shell: bash

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/psf/black
5+
rev: 23.9.1
6+
hooks:
7+
- id: black
8+
exclude: ^(.+)/migrations/.*\.py
9+

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.6
1+
FROM python:3.9
22

33
RUN pip install "setuptools<46" && pip install pipenv
44

@@ -10,4 +10,4 @@ RUN pipenv install --system
1010
COPY . /app
1111
WORKDIR /app
1212

13-
CMD alembic upgrade head && python wachter/bot.py
13+
CMD alembic upgrade head && python -m app

Pipfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ name = "pypi"
55

66
[packages]
77
"psycopg2-binary" = "*"
8-
python-telegram-bot = "*"
8+
python-telegram-bot = {version = "~=13.11"}
99
sqlalchemy = "*"
1010
alembic = "*"
11+
pre-commit = "*"
12+
pylint = "*"
13+
black = "*"
14+
pyTelegramLogger = "*"
1115

1216
[dev-packages]
1317
"autopep8" = "*"
1418

1519
[requires]
16-
python_version = "3.6"
20+
python_version = "3.9"

Pipfile.lock

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

README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
# Wachter Telegram bot
22

3-
## Как запустить
3+
[![Docker prod](https://github.com/alexeyqu/wachter_bot/actions/workflows/publish_production.yml/badge.svg)](https://github.com/alexeyqu/wachter_bot/actions/workflows/publish_production.yml) [![Docker testing](https://github.com/alexeyqu/wachter_bot/actions/workflows/publish_testing.yml/badge.svg)](https://github.com/alexeyqu/wachter_bot/actions/workflows/publish_testing.yml)
44

5-
В корне есть Dockerfile
5+
<img src="https://github.com/alexeyqu/wachter_bot/assets/7394728/75869909-59fa-4d1b-a829-7737613adf87" alt="telegram logo" height="18px"/> [Вахтёр Бот](https://t.me/wachter_bot)
6+
7+
8+
![photo_2023-10-28_15-36-58](https://github.com/alexeyqu/wachter_bot/assets/7394728/dac59c1b-0868-4bcc-aa07-48944c9a15b8)
9+
10+
11+
## Как добавить в свою группу
12+
13+
1. Добавить в группу.
14+
2. Сделать администратором.
15+
3. Опционально, настроить бота в личном чате.
16+
17+
## Local Development
18+
19+
### Prerequisites
20+
21+
We are using black to keep our code looking nice and tidy. To make things easier, there's a pre-commit hook which ensures that files to commit are properly formatted. You need to install and initialize pre-commit. Any installation should suffice, one can find pipenv good choice since we use it in this project. After installation run:
622

723
```bash
8-
$ docker build -t wachter_bot .
9-
$ docker run -e TELEGRAM_TOKEN=<token> wachter_bot
24+
pre-commit install
1025
```
1126

12-
## Как добавить в свой чат
27+
Then will be executed black against changed files in commits.
28+
29+
### Running
30+
31+
1) Set `TELEGRAM_TOKEN` and `TELEGRAM_ERROR_CHAT_ID` environment variable;
1332

14-
1. Добавить в чат
15-
2. Написать whois (минимальная длина 20 символов)
16-
3. Написать боту в лс /start - бот заработает и появятся настройки чата
33+
2) Run:
34+
35+
```bash
36+
docker-compose -f docker-compose.dev.yml build && docker-compose -f docker-compose.dev.yml up
37+
```

app.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from telegram.ext import (
2+
Updater,
3+
CommandHandler,
4+
Filters,
5+
MessageHandler,
6+
CallbackQueryHandler,
7+
ChatMemberHandler,
8+
)
9+
from src.custom_filters import filter_bot_added
10+
from src.logging import tg_logger
11+
from src import handlers
12+
import os
13+
14+
15+
def main():
16+
updater = Updater(os.environ["TELEGRAM_TOKEN"])
17+
dp = updater.dispatcher
18+
19+
dp.add_handler(CommandHandler("help", handlers.help_handler))
20+
21+
# group UX
22+
dp.add_handler(
23+
ChatMemberHandler(
24+
handlers.my_chat_member_handler,
25+
ChatMemberHandler.MY_CHAT_MEMBER,
26+
)
27+
)
28+
dp.add_handler(
29+
MessageHandler(
30+
Filters.entity("hashtag") & Filters.chat_type.groups,
31+
handlers.on_hashtag_message,
32+
pass_job_queue=True,
33+
pass_user_data=True,
34+
)
35+
)
36+
dp.add_handler(
37+
MessageHandler(
38+
Filters.status_update.new_chat_members & filter_bot_added,
39+
handlers.on_new_chat_members,
40+
pass_job_queue=True,
41+
)
42+
)
43+
44+
# admin UX
45+
dp.add_handler(CommandHandler("start", handlers.start_handler, pass_user_data=True))
46+
dp.add_handler(CallbackQueryHandler(handlers.button_handler, pass_user_data=True))
47+
dp.add_handler(
48+
MessageHandler(
49+
(Filters.text | Filters.entity),
50+
handlers.message_handler,
51+
pass_user_data=True,
52+
pass_job_queue=True,
53+
)
54+
)
55+
dp.add_error_handler(handlers.error_handler)
56+
57+
updater.start_polling()
58+
tg_logger.info("Bot has started successfully")
59+
updater.idle()
60+
61+
62+
if __name__ == "__main__":
63+
main()

docker-compose.dev.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
version: "3.9"
2+
3+
services:
4+
wachter-db:
5+
container_name: wachter-db-dev
6+
image: postgres:alpine
7+
restart: always
8+
ports:
9+
- 5433:5432
10+
environment:
11+
POSTGRES_DB: db
12+
POSTGRES_HOST: wachter-db
13+
POSTGRES_USER: user
14+
POSTGRES_PASSWORD: password
15+
volumes:
16+
- volume-db:/data/db
17+
- postgres-data:/var/lib/postgresql/data
18+
- ./share/sql:/docker-entrypoint-initdb.d
19+
healthcheck:
20+
test: pg_isready -U user -d db
21+
interval: 5s
22+
timeout: 2s
23+
retries: 3
24+
25+
wachter:
26+
container_name: wachter-dev
27+
build:
28+
context: .
29+
environment:
30+
- TELEGRAM_TOKEN=${TELEGRAM_TOKEN}
31+
- TELEGRAM_ERROR_CHAT_ID=${TELEGRAM_ERROR_CHAT_ID}
32+
- DATABASE_URL=postgresql://user:password@wachter-db/db
33+
restart: unless-stopped
34+
depends_on:
35+
wachter-db:
36+
condition: service_healthy
37+
38+
volumes:
39+
volume-db:
40+
postgres-data:
41+
42+
networks:
43+
default:
44+
name: network-wachter-dev

migrations/env.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from logging.config import fileConfig
55

66
import os, sys
7-
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
7+
8+
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), "..")))
89

910

1011
# this is the Alembic Config object, which provides
@@ -19,16 +20,19 @@
1920
# for 'autogenerate' support
2021
# from myapp import mymodel
2122
# target_metadata = mymodel.Base.metadata
22-
from wachter import model
23+
from src import model
24+
2325
target_metadata = model.Base.metadata
2426

2527
# other values from the config, defined by the needs of env.py,
2628
# can be acquired:
2729
# my_important_option = config.get_main_option("my_important_option")
2830
# ... etc.
2931

32+
3033
def get_uri():
31-
return os.environ.get('DATABASE_URL', config.get_main_option("sqlalchemy.url"))
34+
return os.environ.get("DATABASE_URL", config.get_main_option("sqlalchemy.url"))
35+
3236

3337
def run_migrations_offline():
3438
"""Run migrations in 'offline' mode.
@@ -43,7 +47,8 @@ def run_migrations_offline():
4347
4448
"""
4549
context.configure(
46-
url=get_uri(), target_metadata=target_metadata, literal_binds=True)
50+
url=get_uri(), target_metadata=target_metadata, literal_binds=True
51+
)
4752

4853
with context.begin_transaction():
4954
context.run_migrations()
@@ -59,14 +64,12 @@ def run_migrations_online():
5964
connectable = create_engine(get_uri())
6065

6166
with connectable.connect() as connection:
62-
context.configure(
63-
connection=connection,
64-
target_metadata=target_metadata
65-
)
67+
context.configure(connection=connection, target_metadata=target_metadata)
6668

6769
with context.begin_transaction():
6870
context.run_migrations()
6971

72+
7073
if context.is_offline_mode():
7174
run_migrations_offline()
7275
else:

0 commit comments

Comments
 (0)