Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active August 5, 2022 14:51
Show Gist options
  • Save aahnik/1c8b35f810a060ce497651d059bf730e to your computer and use it in GitHub Desktop.
Save aahnik/1c8b35f810a060ce497651d059bf730e to your computer and use it in GitHub Desktop.
Run multiple bot telegram bot clients using gnu-screen (terminal-multiplexer). Supply bot tokens via bots.yml to this script. [telethon,telegram,bots.gnu-screen,screen,multiples]

First of all you make a bot whose multiple instances are to be run from the same server.

Suppose the bot is run by invoking main.py with the bot_name and token as command line arguments.

Syntax:

[PYTHON] [ENTRY_POINT] [bot_name] [token]

Example:

python main.py mysamplebot "173453434:42jhs894_13234jsjgasjs"

Now the multi_bot.py script runs multiple instances of the bot code in seperate screen sessions/sockets. (each socket is named after the name of the bot running inside it)

The bots.yml file supplies the list of bot accounts with their tokens to multi_bot.py

Syntax of bots.yml

botusername: "bottoken"
anotherbotname: "anothertoken"

Example:

mysamplehorriblebot: "173453434:42jhs894_13234jsjgasjs"
anotherSamplebot: "556453434:42jhs894_13234yuweeerasjs"
# pip install pyyaml
import yaml
import os
PYTHON = 'python3.9'
ENTRY_POINT = 'main.py'
with open('bots.yml') as file:
bots = yaml.full_load(file)
for bot_name, token in bots.items():
command = f'''screen -dmS {bot_name} bash -c '{PYTHON} {ENTRY_POINT} {bot_name} "{token}" ' '''
os.system(command)
print(f'deployed @{bot_name}')
@aahnik
Copy link
Author

aahnik commented Mar 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment