Skip to content

Instantly share code, notes, and snippets.

@fluential
Forked from CustomIcon/1.md
Created July 11, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fluential/93379c5a1ee09f14ef9aa8fd97ee426e to your computer and use it in GitHub Desktop.
Save fluential/93379c5a1ee09f14ef9aa8fd97ee426e to your computer and use it in GitHub Desktop.
start(), idle() and stop() multiple Pyrogram Clients at once.

UPDATED

Updated to Pyrogram v1.

If you know what you're doing, feel free to use these as a guide.

For any questions, head to @PyrogramLounge.

"""
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, idle
from pyrogram.handlers import 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()
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, idle
from pyrogram.handlers import 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()
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, idle
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()
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