Skip to content

Commit 7502b64

Browse files
committed
Code re-written from scratch
1 parent 3d8faf8 commit 7502b64

33 files changed

+451
-490
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
/venv/
2-
/.idea/
3-
/__pycache__/
4-
5-
/.env
2+
.idea/

Dockerfile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
# Using separate image as to reduce final image size
2-
FROM python:3.8-slim-buster as compile-image
3-
WORKDIR /app
1+
# Separate build image
2+
FROM python:3.9-slim-buster as compile-image
43
RUN python -m venv /opt/venv
54
ENV PATH="/opt/venv/bin:$PATH"
65
COPY requirements.txt .
76
RUN pip install --no-cache-dir --upgrade pip \
87
&& pip install --no-cache-dir -r requirements.txt
98

10-
# "production" image
11-
FROM python:3.8-slim-buster
9+
# Final image
10+
FROM python:3.9-slim-buster
1211
COPY --from=compile-image /opt/venv /opt/venv
13-
WORKDIR /app
1412
ENV PATH="/opt/venv/bin:$PATH"
15-
COPY handlers /app/handlers
16-
COPY *.py /app/
17-
CMD ["python", "bot.py"]
13+
WORKDIR /app
14+
COPY bot /app/bot
15+
CMD ["python", "-m", "bot"]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019-2020 Groosha
3+
Copyright (c) 2019-2021 Groosha
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
# Report bot for Telegram
22

3-
This repository contains sources of a small yet rather powerful bot for Telegram, which handles reports from users and passes them to admins. Uses [aiogram](https://github.com/aiogram/aiogram) framework.
4-
The main goal was to build a bot with no external database needed. Thus, it may lack some features, but hey, it's open source!
3+
This repository contains source code of a small yet rather powerful bot for Telegram, which handles reports from users and passes them to admins.
4+
Uses [aiogram](https://github.com/aiogram/aiogram) framework.
5+
The main goal is to build a bot with no external database needed. Thus, it may lack some features, but hey, it's open source!
56

6-
#### Screenshots
7-
User reports a message:
8-
![User reports a message in regular chat](screenshots/users_view.jpg)
7+
#### Screenshot
98

10-
Admins take action:
11-
![Admins get report in a special chat](screenshots/admin_view.jpg)
9+
![Left - main group. Right - group for admins only](screenshots/cover.png)
1210

1311
#### Features
14-
* Handles `/report` command to gather reports from users;
15-
* Handles `/ro` command to set user "Read-only" and `/textonly` to allow text messages only;
12+
* `/report` command to gather reports from users;
13+
* `/ro` command to set user "read-only" and `/nomedia` to allow text messages only;
1614
* Removes "user joined" messages;
17-
* Reacts to short "greetings" messages like "Hey everyone", kindly asking user to proceed to their question or problem directly;
18-
* Provides a simple interface for admins to choose one of actions on reported message.
15+
* If text message starts with `@admin`, admins are notified;
16+
* A simple interface for admins to choose one of actions on reported message;
17+
* English and Russian languages are built-in.
1918

2019
#### Requirements
21-
* Python 3.7 and above;
22-
* Linux is mentioned in the following installation guide, but bot should also work on Windows: no platform-specific code is used;
20+
* Python 3.9 and above;
21+
* Tested on Linux, should work on Windows, no platform-specific code is used;
2322
* Systemd (you can use it to enable autostart and autorestart) or Docker.
2423

2524
#### Installation
@@ -32,7 +31,7 @@ and **make bot an admin**. You also need to give it "Delete messages" permission
3231
4. Now choose installation method: **systemd** or **Docker**
3332

3433
##### systemd
35-
1. Create a venv (virtual environment): `python3.7 -m venv venv` (or any other Python 3.7+ version);
34+
1. Create a venv (virtual environment): `python3.9 -m venv venv` (or any other Python 3.7+ version);
3635
2. `source venv/bin/python && pip install -r requirements.txt`;
3736
3. `chmod +x bot.py`;
3837
4. Copy `env_dist` to `.env` (with dot). **Warning**: files starting with dot are usually hidden in Linux,

bot.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

bot/__init__.py

Whitespace-only changes.

bot/__main__.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import asyncio
2+
import logging
3+
4+
from aiogram import Bot, Dispatcher
5+
from aiogram.types import BotCommand
6+
7+
from bot.config_reader import load_config
8+
from bot.middlewares.config import ConfigMiddleware
9+
from bot.handlers.main_group_admin import register_main_group_admin
10+
from bot.handlers.main_group_user import register_main_group_user
11+
from bot.handlers.main_group_events import register_group_events
12+
from bot.handlers.callbacks_reports import register_callbacks_reports
13+
14+
logger = logging.getLogger(__name__)
15+
16+
17+
async def set_bot_commands(bot: Bot):
18+
commands = [
19+
BotCommand(command="report", description="Report message to group admins"),
20+
]
21+
await bot.set_my_commands(commands)
22+
23+
24+
def get_handled_updates_list(dp: Dispatcher) -> list:
25+
"""
26+
Here we collect only the needed updates for bot based on already registered handlers types.
27+
This way Telegram doesn't send unwanted updates and bot doesn't have to proceed them.
28+
29+
:param dp: Dispatcher
30+
:return: a list of registered handlers types
31+
"""
32+
available_updates = (
33+
"callback_query_handlers", "channel_post_handlers", "chat_member_handlers",
34+
"chosen_inline_result_handlers", "edited_channel_post_handlers", "edited_message_handlers",
35+
"inline_query_handlers", "message_handlers", "my_chat_member_handlers", "poll_answer_handlers",
36+
"poll_handlers", "pre_checkout_query_handlers", "shipping_query_handlers"
37+
)
38+
return [item.replace("_handlers", "") for item in available_updates
39+
if len(dp.__getattribute__(item).handlers) > 0]
40+
41+
42+
async def main():
43+
logging.basicConfig(
44+
level=logging.INFO,
45+
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
46+
)
47+
48+
# Reading config from env vars
49+
config = load_config()
50+
51+
bot = Bot(token=config.token)
52+
dp = Dispatcher(bot)
53+
54+
# Register handlers
55+
register_main_group_admin(dp, main_group_id=config.group.main)
56+
register_main_group_user(dp, main_group_id=config.group.main)
57+
register_group_events(dp, main_group_id=config.group.main)
58+
register_callbacks_reports(dp)
59+
60+
# Register middlewares
61+
dp.middleware.setup(ConfigMiddleware(config))
62+
63+
# Register /-commands in UI
64+
await set_bot_commands(bot)
65+
66+
logger.info("Starting bot")
67+
68+
# Start polling
69+
# await dp.skip_updates() # skip pending updates (optional)
70+
try:
71+
await dp.start_polling(allowed_updates=get_handled_updates_list(dp))
72+
finally:
73+
await bot.close()
74+
75+
76+
if __name__ == '__main__':
77+
asyncio.run(main())

bot/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from aiogram.utils.callback_data import CallbackData
2+
3+
report_msg_cb = CallbackData("delmsg", "option", "user_id", "message_ids")

bot/config_reader.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from dataclasses import dataclass
2+
from os import getenv
3+
4+
5+
@dataclass
6+
class Group:
7+
main: int
8+
reports: int
9+
10+
11+
@dataclass
12+
class Config:
13+
token: str
14+
lang: str
15+
group: Group
16+
17+
18+
def load_config():
19+
return Config(
20+
token=getenv("BOT_TOKEN"),
21+
lang=getenv("BOT_LANGUAGE"),
22+
group=Group(
23+
main=int(getenv("GROUP_MAIN")),
24+
reports=int(getenv("GROUP_REPORTS"))
25+
)
26+
)

bot/handlers/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)