pyrogram/docs/source/topics/smart-plugins.rst

306 lines
9.9 KiB
ReStructuredText
Raw Normal View History

2018-10-18 11:04:57 +00:00
Smart Plugins
=============
2022-01-07 09:18:51 +00:00
Pyrogram embeds a smart, lightweight yet powerful plugin system that is meant to further simplify the organization
of large projects and to provide a way for creating pluggable (modular) components that can be easily shared across
different Pyrogram applications with minimal boilerplate code.
2018-11-03 10:00:26 +00:00
.. tip::
Smart Plugins are completely optional and disabled by default.
2020-04-01 18:08:46 +00:00
.. contents:: Contents
:backlinks: none
:depth: 1
2020-04-01 18:08:46 +00:00
:local:
-----
Introduction
------------
2018-10-18 11:04:57 +00:00
Prior to the Smart Plugin system, pluggable handlers were already possible. For example, if you wanted to modularize
2019-04-12 13:52:06 +00:00
your applications, you had to put your function definitions in separate files and register them inside your main script
after importing your modules, like this:
2018-11-03 10:00:26 +00:00
.. note::
This is an example application that replies in private chats with two messages: one containing the same
text message you sent and the other containing the reversed text message.
Example: *"Pyrogram"* replies with *"Pyrogram"* and *"margoryP"*
2018-10-21 07:43:35 +00:00
.. code-block:: text
2018-10-21 07:43:35 +00:00
myproject/
handlers.py
main.py
2019-05-16 19:28:34 +00:00
- ``handlers.py``
2018-10-21 07:43:35 +00:00
.. code-block:: python
2022-04-24 09:56:07 +00:00
async def echo(client, message):
await message.reply(message.text)
2022-04-24 09:56:07 +00:00
async def echo_reversed(client, message):
await message.reply(message.text[::-1])
2019-05-16 19:28:34 +00:00
- ``main.py``
2018-10-21 07:43:35 +00:00
.. code-block:: python
from pyrogram import Client, filters
from pyrogram.handlers import MessageHandler
2018-10-21 07:43:35 +00:00
from handlers import echo, echo_reversed
2018-10-21 07:43:35 +00:00
app = Client("my_account")
2018-10-21 07:43:35 +00:00
app.add_handler(
MessageHandler(
echo,
filters.text & filters.private))
2018-10-21 07:43:35 +00:00
app.add_handler(
MessageHandler(
echo_reversed,
filters.text & filters.private),
2018-10-21 07:43:35 +00:00
group=1)
2018-10-21 07:43:35 +00:00
app.run()
2019-01-17 14:21:50 +00:00
This is already nice and doesn't add *too much* boilerplate code, but things can get boring still; you have to
2019-05-28 14:41:55 +00:00
manually ``import``, manually :meth:`~pyrogram.Client.add_handler` and manually instantiate each
2022-01-07 09:18:51 +00:00
:class:`~pyrogram.handlers.MessageHandler` object because you can't use decorators for your functions.
So, what if you could? Smart Plugins solve this issue by taking care of handlers registration automatically.
2018-10-18 11:04:57 +00:00
Using Smart Plugins
-------------------
2019-04-12 13:52:06 +00:00
Setting up your Pyrogram project to accommodate Smart Plugins is pretty straightforward:
2018-10-21 07:43:35 +00:00
#. Create a new folder to store all the plugins (e.g.: "plugins", "handlers", ...).
#. Put your python files full of plugins inside. Organize them as you wish.
2022-04-24 09:56:07 +00:00
#. Enable plugins in your Client.
2018-10-21 07:43:35 +00:00
.. note::
2018-10-18 11:04:57 +00:00
2019-05-28 14:41:55 +00:00
This is the same example application as shown above, written using the Smart Plugin system.
2018-10-21 07:43:35 +00:00
.. code-block:: text
2018-10-21 07:43:35 +00:00
myproject/
plugins/
handlers.py
main.py
2019-05-16 19:28:34 +00:00
- ``plugins/handlers.py``
2018-10-21 07:43:35 +00:00
.. code-block:: python
from pyrogram import Client, filters
@Client.on_message(filters.text & filters.private)
2022-04-24 09:56:07 +00:00
async def echo(client, message):
await message.reply(message.text)
@Client.on_message(filters.text & filters.private, group=1)
2022-04-24 09:56:07 +00:00
async def echo_reversed(client, message):
await message.reply(message.text[::-1])
2019-01-17 14:21:50 +00:00
2019-05-16 19:28:34 +00:00
- ``main.py``
2019-01-17 14:21:50 +00:00
.. code-block:: python
from pyrogram import Client
2019-04-12 13:52:06 +00:00
plugins = dict(root="plugins")
2019-01-17 14:21:50 +00:00
Client("my_account", plugins=plugins).run()
2019-04-12 13:52:06 +00:00
2019-01-17 14:21:50 +00:00
The first important thing to note is the new ``plugins`` folder. You can put *any python file* in *any subfolder* and
each file can contain *any decorated function* (handlers) with one limitation: within a single module (file) you must
use different names for each decorated function.
2022-04-24 09:56:07 +00:00
The second thing is telling Pyrogram where to look for your plugins: you can use the Client parameter "plugins";
the *root* value must match the name of your plugins root folder. Your Pyrogram Client instance will **automatically**
scan the folder upon starting to search for valid handlers and register them for you.
2018-10-18 11:04:57 +00:00
Then you'll notice you can now use decorators. That's right, you can apply the usual decorators to your callback
functions in a static way, i.e. **without having the Client instance around**: simply use ``@Client`` (Client class)
2019-01-17 14:21:50 +00:00
instead of the usual ``@app`` (Client instance) and things will work just the same.
Specifying the Plugins to include
---------------------------------
By default, if you don't explicitly supply a list of plugins, every valid one found inside your plugins root folder will
be included by following the alphabetical order of the directory structure (files and subfolders); the single handlers
found inside each module will be, instead, loaded in the order they are defined, from top to bottom.
.. note::
Remember: there can be at most one handler, within a group, dealing with a specific update. Plugins with overlapping
2022-01-07 09:18:51 +00:00
filters included a second time will not work, by design. Learn more at :doc:`More on Updates <more-on-updates>`.
2019-01-17 14:21:50 +00:00
This default loading behaviour is usually enough, but sometimes you want to have more control on what to include (or
exclude) and in which exact order to load plugins. The way to do this is to make use of ``include`` and ``exclude``
2022-04-24 09:56:07 +00:00
directives in the dictionary passed as Client argument. Here's how they work:
2019-01-17 14:21:50 +00:00
- If both ``include`` and ``exclude`` are omitted, all plugins are loaded as described above.
- If ``include`` is given, only the specified plugins will be loaded, in the order they are passed.
- If ``exclude`` is given, the plugins specified here will be unloaded.
The ``include`` and ``exclude`` value is a **list of strings**. Each string containing the path of the module relative
to the plugins root folder, in Python notation (dots instead of slashes).
2019-01-25 08:40:37 +00:00
E.g.: ``subfolder.module`` refers to ``plugins/subfolder/module.py``, with ``root="plugins"``.
2019-01-17 14:21:50 +00:00
You can also choose the order in which the single handlers inside a module are loaded, thus overriding the default
top-to-bottom loading policy. You can do this by appending the name of the functions to the module path, each one
separated by a blank space.
E.g.: ``subfolder.module fn2 fn1 fn3`` will load *fn2*, *fn1* and *fn3* from *subfolder.module*, in this order.
Examples
^^^^^^^^
Given this plugins folder structure with three modules, each containing their own handlers (fn1, fn2, etc...), which are
also organized in subfolders:
.. code-block:: text
myproject/
plugins/
subfolder1/
plugins1.py
- fn1
- fn2
- fn3
subfolder2/
plugins2.py
...
plugins0.py
...
...
2019-05-16 19:28:34 +00:00
- Load every handler from every module, namely *plugins0.py*, *plugins1.py* and *plugins2.py* in alphabetical order
(files) and definition order (handlers inside files):
2019-01-17 14:21:50 +00:00
.. code-block:: python
2019-04-12 13:52:06 +00:00
plugins = dict(root="plugins")
2019-01-17 14:21:50 +00:00
Client("my_account", plugins=plugins).run()
2019-05-16 19:28:34 +00:00
- Load only handlers defined inside *plugins2.py* and *plugins0.py*, in this order:
2019-01-17 14:21:50 +00:00
.. code-block:: python
plugins = dict(
root="plugins",
include=[
"subfolder2.plugins2",
"plugins0"
]
)
Client("my_account", plugins=plugins).run()
2019-05-16 19:28:34 +00:00
- Load everything except the handlers inside *plugins2.py*:
2019-01-17 14:21:50 +00:00
.. code-block:: python
plugins = dict(
root="plugins",
exclude=["subfolder2.plugins2"]
)
Client("my_account", plugins=plugins).run()
2019-05-16 19:28:34 +00:00
- Load only *fn3*, *fn1* and *fn2* (in this order) from *plugins1.py*:
2019-01-17 14:21:50 +00:00
.. code-block:: python
plugins = dict(
root="plugins",
include=["subfolder1.plugins1 fn3 fn1 fn2"]
)
Client("my_account", plugins=plugins).run()
Load/Unload Plugins at Runtime
------------------------------
2019-05-28 14:41:55 +00:00
In the previous section we've explained how to specify which plugins to load and which to ignore before your Client
starts. Here we'll show, instead, how to unload and load again a previously registered plugin at runtime.
2019-08-16 20:47:31 +00:00
Each function decorated with the usual ``on_message`` decorator (or any other decorator that deals with Telegram
2022-01-07 09:18:51 +00:00
updates) will be modified in such a way that a special ``handlers`` attribute pointing to a list of tuples of
2019-08-16 20:47:31 +00:00
*(handler: Handler, group: int)* is attached to the function object itself.
2019-05-16 19:28:34 +00:00
- ``plugins/handlers.py``
.. code-block:: python
@Client.on_message(filters.text & filters.private)
2022-04-24 09:56:07 +00:00
async def echo(client, message):
await message.reply(message.text)
print(echo)
2022-01-07 09:18:51 +00:00
print(echo.handlers)
2019-08-16 20:47:31 +00:00
- Printing ``echo`` will show something like ``<function echo at 0x10e3b6598>``.
2022-01-07 09:18:51 +00:00
- Printing ``echo.handlers`` will reveal the handlers, that is, a list of tuples containing the actual handlers and
the groups they were registered on ``[(<MessageHandler object at 0x10e3abc50>, 0)]``.
Unloading
^^^^^^^^^
In order to unload a plugin, all you need to do is obtain a reference to it by importing the relevant module and call
2022-01-07 09:18:51 +00:00
:meth:`~pyrogram.Client.remove_handler` Client's method with your function's *handler* instance:
2019-05-16 19:28:34 +00:00
- ``main.py``
.. code-block:: python
from plugins.handlers import echo
2022-01-07 09:18:51 +00:00
handlers = echo.handlers
2022-01-07 09:18:51 +00:00
for h in handlers:
app.remove_handler(*h)
The star ``*`` operator is used to unpack the tuple into positional arguments so that *remove_handler* will receive
exactly what is needed. The same could have been achieved with:
.. code-block:: python
2022-01-07 09:18:51 +00:00
handlers = echo.handlers
handler, group = handlers[0]
app.remove_handler(handler, group)
Loading
^^^^^^^
Similarly to the unloading process, in order to load again a previously unloaded plugin you do the same, but this time
2019-05-28 14:41:55 +00:00
using :meth:`~pyrogram.Client.add_handler` instead. Example:
2019-05-16 19:28:34 +00:00
- ``main.py``
.. code-block:: python
from plugins.handlers import echo
...
2022-01-07 09:18:51 +00:00
handlers = echo.handlers
for h in handlers:
app.add_handler(*h)