/tgchannel-discord-bridge

Unidirectional bridge from a broadcast telegram channel to discord

Primary LanguagePythonGNU Affero General Public License v3.0AGPL-3.0

The bridge bot posts messages from telegram broadcast channels over to discord channels. This is something I worked on in my free time, it's meant for private use but you can use it for yourself with a little figuring out. It was made public in good faith.

Get this shit running

  • Copy login_copy.json to login.json
  • Run python3 generate_login_string.py
  • Copy the generated login string.
  • Open login.json and modify as specified in how to fill data
  • After you have generated a functional login.json, simply run main.py
  • python3 main.py

login.json

  • api_id - This field is obtained from my.telegram.org. This is the only non-quoted (") field, i.e an integer value

  • api_hash - This field is obtained from my.telegram.org.

  • telegram_login_string - This field is generated by running generate_login_string.py.

  • discord_token - This field is obtained from discord.

  • media_download - The location where downloaded media is put if it is larger than 7.5 Mega Bytes.

  • media_url - The url part which will be posted after moving to media_download location. The file name is appended to this URL and posted. Typically the media_download is some location which is accessible via the web through media_url and so it makes sense for me to use them in my case.

  • discord_admins - This is a comma-seperated list of role-ids or role-names which are allowed to access commands.

Telegram side

You should be joined in a public/private channel. This works with user accounts only as it was intended for scraping purposes. Channel owners/admins which can add bridge bots should use those as they're more polished. However they require setup from both discord and telegram admins. In this case, only discord admins are needed which can add the bot to the server.

Discord commands

The prefix for the discord bot is u!. Only the bot owner and server Administrators can execute these commands.

u!add

This command allows you to associate a discord channel with one or more telegram channels

See example usage:

# meta (telegram_ids can be separated with spaces for multiple channels)
u!add discord_id telegram_id(s)
# post media from multiple channels to that single discord channel
u!add 1084482756345614438 -1001699258222 -1001983464181
# post media from single telegram channel to single discord channel
u!add 1084482806064885882 -1001759446243

u!begin

Start watching for updates in the specified telegram channels and post them on discord

u!begin

u!dump

Dump the saved json data in the discord chat. This is useful when you want to keep a record of channels which were added in case of a bot restart.

u!dump

u!ping

Acknowledgement command that bot is able to receive and send messages.

u!ping

u!save

Save the temporary actions of the u!add command. This creates a physical file in the bot command issuing directory on the server. It contains data in json format.

u!save

u!stop

Clear the internal channels list from where to fetch updates from. This is equivalent to a reset but channels from pervious u!save are still present. To start fresh, simply begin by using u!add and so on. If you wish to restart from previous configuration, simply issue the u!begin command again.

u!toggle_direct_large_uploads

Toggles b/w keeping files on server and direct uploads for media larger than the free limit specified by discord, i.e 8 MB. (Actual limit enforced in code is 7.9 MB). This option is disabled by default. The default is that the bot will only post media smaller than 8 MB.

u!toggle_direct_large_uploads

This command also disables small_uploads when direct large uploads are turned on.

u!toggle_small_uploads

Toggles the switch to only allow uploads for media smaller than 8 MB (7.9 MB in code). Small uploads are enabled by default.

u!toggle_small_uploads

Typical Usage

  • Working login.json
  • Run src/main.py
  • Make sure you have joined the channels on your telegram account
  • Get correct telegram channel id(s), they start with - sign.
  • If you get an id that starts with - but doesn't start like this -100 then put -100 before it when you issue u!add
  • On discord, issue u!add
  • After that save it using u!save
  • Start seeing the magic with u!begin

Use examples in a discord server

  1. Using for the first time, associate a single discord channel with a single telegram channel only. Server does not have boosts or web server directory.
u!add 1084482806064885882 -1001759446243
u!save
u!begin
  1. Bot is restarted or say a configuration already exists. Rest paramters are same as 1.
u!begin
  1. Using for the first time, associate a single discord channel with a single telegram channel only. Server has a level 2 or level 3 boost
u!add 1084482806064885882 -1001759446243
u!save
u!toggle_direct_large_uploads
u!begin

The bot will now upload media upto 100 MB to the discord server directly. If the server has reached Level 2, it will only upload upto 50 MB. For Level 3, it is 100 MB.

Disabling small uploads unlocks the limit placed. Media files smaller than 8 MB will still be uploaded.

  1. Using for the first time, associate single discord channel with multiple channels. Server doesn't have any boosts.
u!add 1084482756345614438 -1001699258222 -1001983464181
u!save
u!begin
  1. Using for the first time, associate different discord channels with different telegram channels. Server doesn't have any boosts.
u!add 1084482756345614438 -1001699258222 
u!add 1084482776826380440 -1001983464181
u!add 1084482806064885882 -1001699124902 -1001981249892
u!save
u!begin
  1. Using for the first time, you have setup an active public directory on some webserver that is pointed to by a domain set in media_url and you have setup login.json . The parameters for discord channels and server boosts are same as 5.
u!add 1084482756345614438 -1001699258222 
u!add 1084482776826380440 -1001983464181
u!add 1084482806064885882 -1001699124902 -1001981249892
u!save
u!toggle_small_uploads
u!begin

This turns ofo default small uploads option and the default behavior becomes: copy file to media_location and append the file name to media_url. The bot will then post that URL on discord.

Semantics

The bot will directly download and upload the media to discord if it is less than 7.5 Mega Bytes. Otherwise, the bot will download it locally and then move it to media_location as specified in login_copy.json. Then it will append the downlaoded file name to media_url and post a direct link to there.

Note

This is the first time I've written python in 2 years, so pardon me if the code quality is terrible or not upto standards.

Thank you.