Skip to content

Commit

Permalink
Telegram Forwarder v2 (MrMissx#77)
Browse files Browse the repository at this point in the history
This version is not backward compatible. These needs a different config file to run the bot. Read readme to learn more
  • Loading branch information
MrMissx authored Apr 22, 2023
1 parent e16a104 commit fc028a1
Show file tree
Hide file tree
Showing 23 changed files with 672 additions and 414 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ target/

# Bot config file
# DO NOT REMOVE THIS FILE FROM .gitignore
config.py
.env
chat_list.json
22 changes: 10 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
FROM python:3.8-alpine
FROM python:3.10-slim-bullseye

ENV LANG=C.UTF-8
WORKDIR /app

RUN apk update && apk add --no-cache alpine-sdk \
python3-dev \
libffi-dev \
openssl-dev \
&& rm -rf /var/cache/apk/*
COPY ./ /app/
RUN pip install poetry
RUN poetry config virtualenvs.create false

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r /app/requirements.txt
COPY pyproject.toml poetry.lock ./

WORKDIR /app/
CMD ["python3 -m forwarder"]
RUN poetry install --only main

COPY . .

CMD ["poetry", "run", "forwarder"]
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

113 changes: 56 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,99 @@

A simple Telegram Python bot running on Python3 to automatically forward messages from one chat to another.

## Migration from V1

## Starting The Bot

Once you've setup your database and your configuration (see below) is complete, simply run:

`python3 -m forwarder`
v2 uses a different configuration file format. Please refer to the [Configuration](#configuration) section for more information. The bot will not start if the configuration file is not in the correct format.

or you can host and run this bot on [Heroku](https://github.com/keselekpermen69/Telegram_Forwarder#Host-on-Heroku)
## Starting The Bot

Once you've setup your your configuration (see below) is complete, simply run:

### Host on Heroku
```shell
python -m forwarder
```

<p><a href="https://heroku.com/deploy?template=https://github.com/KeselekPermen69/Telegram_Forwarder/tree/master"><img src="https://www.herokucdn.com/deploy/button.svg" alt="Deploy to Heroku"/></a></p>
or with poetry (recommended)

```shell
poetry run forwarder
```

## Setting Up The Bot (Read Before Trying To Use!):
Please make sure to use the latest Python version. (*Recommended*)
## Setting Up The Bot (Read the instruction bellow before starting the bot!):

Telegram Forwarder only supports Python 3.9 and higher.

### Configuration

There are two possible ways of configuring your bot: a `config.py` file, or ENV variables.
There are two files mandatory for the bot to work `.env` and `chat_list.json`.

The prefered version is to use a `config.py` file, as it makes it easier to see all your settings grouped together.
This file should be placed in `forwarder` folder, alongside the `__main__.py` file .
This is where your bot token will be loaded from, and most of your other settings.
#### `.env`

It is recommended to import `sample_config` and extend the `Config` class, as this will ensure your config contains all
defaults set in the `sample_config`, hence making it easier to upgrade.
Template env may be found in `sample.env`. Rename it to `.env` and fill in the values:

An example `config.py` file could be:
```
from forwarder.sample_config import Config
class Development(Config):
API_KEY = "1234567890:Abcdef1234567890GHIJ" # Your bot API key
OWNER_ID = 1234567890 # Your user id
- `BOT_TOKEN` - Telegram bot token. You can get it from [@BotFather](https://t.me/BotFather)

# Make sure to include the '-' sign in group and channel ids.
FROM_CHATS = [-1001234567890] # List of chat id's to forward messages from.
TO_CHATS = [-1001234567890, -1234567890] # List of chat id's to forward messages to.
REMOVE_TAG = True
WORKERS = 4
```
- `OWNER_ID` - An integer of consisting of your owner ID.

If you can't have a `config.py` file (EG on Heroku), it is also possible to use environment variables.
The following environment variables are supported:
- `REMOVE_TAG` - set to `True` if you want to remove the tag ("Forwarded from xxxxx") from the forwarded message.

- `ENV`: Setting this to `ANYTHING` will enable environment variables.
#### `chat_list.json`

- `API_KEY`: Your bot API key, as a string.
- `OWNER_ID`: An integer of consisting of your owner ID.
Template chat_list may be found in `chat_list.sample.json`. Rename it to `chat_list.json`.

- `FROM_CHATS`: **Space separated** list of chat ID's to forward messages from. Do not forget to include the
minus (-) sign in the chat ID's of groups and channels. You can add ID's of users too, to forward their
messages with the bot.
- `TO_CHATS`: **Space separated** list of chat ID's to forward messages to. Do not forget to include the
minus (-) sign in the chat ID's of groups and channels. You can add ID's of users too, to forward messages to them.
- `REMOVE_TAG`: Wether remove the "Forwarded From ...." tag or not.
This file contains the list of chats to forward messages from and to. The bot expect it to be an Array of objects with the following structure:

- `WEBHOOK`: Setting this to `ANYTHING` will enable webhooks when in env mode messages.
- `URL`: The URL your webhook should connect to (only needed for webhook mode).
- `CERT_PATH`: Path to your webhook certificate.
- `PORT`: Port to use for your webhooks.
```json
[
{
"source": -10012345678,
"destination": [-10011111111, -10022222222]
}
]
```

- `WORKERS`: Number of threads to use. 4 is the recommended (and default) amount, but your experience may vary.
**NOTE:** You may need to use more workers if the number of messages to be forwarded are more.
But going crazy with more threads wont necessarily speed up your bot due to the way python asynchronous calls work.
- `source` - The chat ID of the chat to forward messages from. It can be a group or a channel.
- `destination` - An array of chat IDs to forward messages to. It can be a group or a channel.

You may add as many objects as you want. The bot will forward messages from all the chats in the `source` field to all the chats in the `destination` field. Duplicates are allowed as it already handled by the bot.

### Python dependencies

Install the necessary python dependencies by moving to the project directory and running:

`pip3 install -r requirements.txt`.
```shell
poetry install --only main
```

This will install all necessary python packages.
or with pip

```shell
pip3 install -r requirements.txt
```

This will install all necessary python packages.

### Launch in Docker container

#### Requrements
- Docker
- docker-compose

Before launch you need to copy file ```env_dist``` to file ```.env``` and fill the values in it.
- Docker
- docker-compose

Before launch make sure all configuration are completed (`.env` and `chat_list.json`)!

Then, simple run the command: ```docker-compose up -d```
Then, simply run the command:

You can view the logs by the command: ```docker-compose logs -f```
```shell
docker-compose up -d
```

You can view the logs by the command:

```shell
docker-compose logs -f
```

### Credits

* [saksham2410](https://github.com/saksham2410) - AutoForwarder-TelegramBot
- [saksham2410](https://github.com/saksham2410) - AutoForwarder-TelegramBot
56 changes: 0 additions & 56 deletions app.json

This file was deleted.

16 changes: 16 additions & 0 deletions chat_list.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"source": -10012345678,
"destination": [
-10012345678,
-10012345678
]
},
{
"source": -10012345678,
"destination": [
-10012345678,
-10012345678
]
}
]
19 changes: 5 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
version: "3.7"
version: "3.9"

services:

telegram-forwarder:
image: telegram-forwarder-bot
stdin_open: true
tty: true
restart: always
restart: unless-stopped
build:
context: .
environment:
- TZ=$TZ
- ENV=$ENV
- API_KEY=$BOT_TOKEN
- OWNER_ID=$OWNER_ID
- FROM_CHATS=$FROM_CHATS
- TO_CHATS=$TO_CHATS
- WORKERS=$WORKERS
command: python3 -m forwarder
dockerfile: Dockerfile
env_file:
- .env
10 changes: 0 additions & 10 deletions env_dist

This file was deleted.

Loading

0 comments on commit fc028a1

Please sign in to comment.