Skip to content

Instantly share code, notes, and snippets.

@CustomIcon
Forked from ColinShark/1.md
Last active July 23, 2023 17:52
Show Gist options
  • Save CustomIcon/96afa69e86725850b2101099461609ed to your computer and use it in GitHub Desktop.
Save CustomIcon/96afa69e86725850b2101099461609ed 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()
@Muhammedshifin
Copy link

Shuper

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