Post to Channel forwarding bot for Telegram.
This is a bot for Telegram written in Golang, backed by a SQL database that can be used to make unique media posts to a Channel on behalf of any specific user(s).
This bot will keep track of any image files submitted using DifferenceHash. This
hashing mechanism allows for unique hashes regardless of the image size or quality.
Additionally, this will download and send "raw" images and does not forward them (using the Telegram-native forward method), making them "unlinked" from the source (prevents deletion via upstream).
To use the service, any media can be sent or forwarded to a bot controlled by the Forwarder service. The bot will handle the rest and responds with the action taken. Any media captions or text added before the message can be used as the caption for the final forwarded post. This will save hashtags for easy sorting.
The Forwarder service can manage multiple bots that can target the same or different Channels. Each bot's posts are treated independently from eachother. (Posts from "BotA" are not compared against posts from "BotB"). Additionally, each "bot" can be given different User access permissions.
Forwarder Telegram Bot
Purple Security (losynth.com/purple) 2021 - 2025
Usage:
-h Print this help menu.
-V Print version string and exit.
-f <file> Configuration file path.
-d Dump the default configuration and exit.
-I <file> Import existing Channel Message data into the database.
This requires using the "import.py" tool.
-clear-all Clear the database of ALL DATA before starting up.
For the bot to post to a Channel, the bot must be an Administrator of the target Channel with post, edit and delete permissions.
The service will keep track of posts with specified media and file hashes, this allows for deleting a post using the raw image (via upload or copy + paste).
To delete a post via media, sending the targeted media with the Caption /delete
will remove the hashes from the duplication tracker AND will delete the post in
the target Channel.
While adding to a "new" Channel is simple, ensuring no duplicates for existing Channel items must be done via an "import".
Importing Channel items is done using the import.py script.
To use the script, you will need a Telegram AppID and AppHash (as the Bot API
cannot use the required APIs). This can be generated by visiting
https://my.telegram.org.
If the configuration file is complete, the script can grab the BotID by logging
in with the bot token, otherwise it must be supplied.
The import script will also print out the Channel ID, if you need it for the config.
# With Config
python import.py -o output.json -c forwarder.conf -i <app_id> -h "<app_hash>" "My Import Channel Name"
# Without Config
python import.py -o output.json -b <bot_id> -i <app_id> -h "<app_hash>" "My Import Channel Name"The import script will generate a JSON output (in the example above "output.json") that contains file and image hashes. This new file can be used in a call to the Forwarder binary to complete the import.
The Forwarder service CANNOT be running while attempting an import!
forwarder -f /etc/forwarder.conf -I output.jsonOnce complete, the process will exit. This can be done multiple times for as many Channels as needed. Additionally, imports can happen at any time and do not need to be done "first".
The default config can be dumped to Stdout using the '-d' command line flag.
{
"db": {
"host": "tcp(localhost:3306)",
"user": "forwarder_user",
"timeout": 180000000000,
"password": "password",
"database": "forwarder_db"
},
"log": {
"file": "forwarder.log",
"level": 2
},
"bots": [
{
"channel_id": 0,
"telegram_key": "",
"authorized_users": [
0,
1
]
}
]
}
The bots section is a list that contains info for each "bot" that is controlled
by the Forwarder service.
channel_idis the target Channel to post in (the bot must be an Administrator of that Channel!).telegram_keyis the Bot key given by BotFather.authorized_usersis an array of User IDs that can submit posts to the Bot. If you do not know the User IDs needed, the service logsTrace(log level 0) messages when an unauthorized user attempts to use the Bot.