Skip to content

Instantly share code, notes, and snippets.

@ColinShark
Last active December 25, 2021 08:51
Show Gist options
  • Save ColinShark/6d80957bc67d0be663effc044e41818e to your computer and use it in GitHub Desktop.
Save ColinShark/6d80957bc67d0be663effc044e41818e to your computer and use it in GitHub Desktop.
start(), idle() and stop() multiple Pyrogram Clients at once.
"""
This sets up your Clients as a dict and starts them by iterating through all of them.
Useful if you have a set of bots/accounts that all should do the same.
"""
from pyrogram import Client, Filters, MessageHandler
my_apps = {
"app1": Client("app1"),
"app2": Client("app2"),
"app3": Client("app3"),
"app4": Client("app4"),
# and so on
}
def test(a, m):
m.reply("Response")
for _, app in my_apps.items():
# Add a MessageHandler to each Client and start it
app.add_handler(MessageHandler(test, Filters.command("test")))
app.start()
Client.idle()
for _, app in my_apps.items():
app.stop()
"""
This sets up your Clients as a list and starts them by iterating through all of them.
Useful if you have a set of bots/accounts that all should do the same.
Practically the same as with dict, but without having to deal with dicts.
"""
from pyrogram import Client, Filters, MessageHandler
my_apps = [
Client("app1"),
Client("app2"),
Client("app3"),
Client("app4"),
# and so on
]
def test(a, m):
# A single handler for all apps to execute
m.reply("Response")
for app in my_apps:
# Add a MessageHandler to each Client and start it
app.add_handler(MessageHandler(test, Filters.command("test")))
app.start()
Client.idle()
for app in my_apps:
app.stop()
"""
This sets up and starts all defined Clients one after another and keeps them idle,
waiting for input until stopped.
"""
from pyrogram import Client, Filters
app1 = Client("app1")
app2 = Client("app2")
# app_ = Client("app_")
filt = Filters.command("test")
# You can define individual handlers ...
@app1.on_message(filt)
def test1(a, m):
m.reply("App 1 response")
# ... like these two.
@app2.on_message(filt)
def test2(a, m):
m.reply("App 2 response")
# Or stack the decorator to have multiple Clients handle the same update
@app1.on_message(filt)
@app2.on_message(filt)
def test_all(a, m):
m.reply("App response")
# Starting all individually, one after another
app1.start()
app2.start()
# app_.start()
Client.idle()
# Same for stopping, one after another
app1.stop()
app2.stop()
# app_.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment