mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-16 20:59:29 +00:00
Update SmartPlugins.rst page
This commit is contained in:
parent
35e3471961
commit
2febb53d72
@ -11,50 +11,50 @@ Introduction
|
|||||||
Prior to the Smart Plugin system, pluggable handlers were already possible. For example, if you wanted to modularize
|
Prior to the Smart Plugin system, pluggable handlers were already possible. For example, if you wanted to modularize
|
||||||
your applications, you had to do something like this...
|
your applications, you had to do something like this...
|
||||||
|
|
||||||
.. note:: This is an example application that replies in private chats with two messages: one containing the same
|
.. 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 (e.g.: "pyrogram" -> "pyrogram" and
|
text message you sent and the other containing the reversed text message (e.g.: "pyrogram" -> "pyrogram" and
|
||||||
"margoryp"):
|
"margoryp"):
|
||||||
|
|
||||||
.. code-block:: text
|
.. code-block:: text
|
||||||
|
|
||||||
myproject/
|
myproject/
|
||||||
config.ini
|
config.ini
|
||||||
handlers.py
|
handlers.py
|
||||||
main.py
|
main.py
|
||||||
|
|
||||||
- ``handlers.py``
|
- ``handlers.py``
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
def echo(client, message):
|
def echo(client, message):
|
||||||
message.reply(message.text)
|
message.reply(message.text)
|
||||||
|
|
||||||
|
|
||||||
def echo_reversed(client, message):
|
def echo_reversed(client, message):
|
||||||
message.reply(message.text[::-1])
|
message.reply(message.text[::-1])
|
||||||
|
|
||||||
- ``main.py``
|
- ``main.py``
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from pyrogram import Client, MessageHandler, Filters
|
from pyrogram import Client, MessageHandler, Filters
|
||||||
|
|
||||||
from handlers import echo, echo_reversed
|
from handlers import echo, echo_reversed
|
||||||
|
|
||||||
app = Client("my_account")
|
app = Client("my_account")
|
||||||
|
|
||||||
app.add_handler(
|
app.add_handler(
|
||||||
MessageHandler(
|
MessageHandler(
|
||||||
echo,
|
echo,
|
||||||
Filters.text & Filters.private))
|
Filters.text & Filters.private))
|
||||||
|
|
||||||
app.add_handler(
|
app.add_handler(
|
||||||
MessageHandler(
|
MessageHandler(
|
||||||
echo_reversed,
|
echo_reversed,
|
||||||
Filters.text & Filters.private),
|
Filters.text & Filters.private),
|
||||||
group=1)
|
group=1)
|
||||||
|
|
||||||
app.run()
|
app.run()
|
||||||
|
|
||||||
...which is already nice and doesn't add *too much* boilerplate code, but things can get boring still; you have to
|
...which is already nice and doesn't add *too much* boilerplate code, but things can get boring still; you have to
|
||||||
manually ``import``, manually :meth:`add_handler <pyrogram.Client.add_handler>` and manually instantiate each
|
manually ``import``, manually :meth:`add_handler <pyrogram.Client.add_handler>` and manually instantiate each
|
||||||
@ -64,55 +64,56 @@ functions. So... What if you could?
|
|||||||
Using Smart Plugins
|
Using Smart Plugins
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
Setting up your Pyrogram project to accommodate Smart Plugins is as easy as creating a folder and putting your files
|
Setting up your Pyrogram project to accommodate Smart Plugins is pretty straightforward:
|
||||||
full of handlers inside.
|
|
||||||
|
|
||||||
.. note::
|
#. Create a new folder to store all the plugins (e.g.: "plugins").
|
||||||
|
#. Put your files full of plugins inside.
|
||||||
|
#. Enable plugins in your Client.
|
||||||
|
|
||||||
This is the same example application `as shown above <#introduction>`_, written using the Smart Plugin system.
|
.. note::
|
||||||
|
|
||||||
.. code-block:: text
|
This is the same example application `as shown above <#introduction>`_, written using the Smart Plugin system.
|
||||||
:emphasize-lines: 2, 3
|
|
||||||
|
|
||||||
myproject/
|
.. code-block:: text
|
||||||
plugins/
|
:emphasize-lines: 2, 3
|
||||||
handlers.py
|
|
||||||
config.ini
|
|
||||||
main.py
|
|
||||||
|
|
||||||
- ``plugins/handlers.py``
|
myproject/
|
||||||
|
plugins/
|
||||||
|
handlers.py
|
||||||
|
config.ini
|
||||||
|
main.py
|
||||||
|
|
||||||
.. code-block:: python
|
- ``plugins/handlers.py``
|
||||||
:emphasize-lines: 4, 9
|
|
||||||
|
|
||||||
from pyrogram import Client, Filters
|
.. code-block:: python
|
||||||
|
:emphasize-lines: 4, 9
|
||||||
|
|
||||||
|
from pyrogram import Client, Filters
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(Filters.text & Filters.private)
|
@Client.on_message(Filters.text & Filters.private)
|
||||||
def echo(client, message):
|
def echo(client, message):
|
||||||
message.reply(message.text)
|
message.reply(message.text)
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(Filters.text & Filters.private, group=1)
|
@Client.on_message(Filters.text & Filters.private, group=1)
|
||||||
def echo_reversed(client, message):
|
def echo_reversed(client, message):
|
||||||
message.reply(message.text[::-1])
|
message.reply(message.text[::-1])
|
||||||
|
|
||||||
- ``main.py``
|
- ``main.py``
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from pyrogram import Client
|
from pyrogram import Client
|
||||||
|
|
||||||
Client("my_account").run()
|
Client("my_account", plugins_dir="plugins").run()
|
||||||
|
|
||||||
The first important thing to note is the ``plugins`` folder, whose name is default and can be changed easily by setting
|
The first important thing to note is the new ``plugins`` folder, whose name is passed to the the ``plugins_dir``
|
||||||
the ``plugins_dir`` parameter when creating a :obj:`Client <pyrogram.Client>`; you can put *any python file* in there
|
parameter when creating a :obj:`Client <pyrogram.Client>` in the ``main.py`` file — you can put *any python file* in
|
||||||
and each file can contain *any decorated function* (handlers) with only one limitation: within a single plugin file you
|
there and each file can contain *any decorated function* (handlers) with only one limitation: within a single plugin
|
||||||
must use different names for each decorated function. Your Pyrogram Client instance (in the ``main.py`` file) will
|
file you must use different names for each decorated function. Your Pyrogram Client instance will **automatically**
|
||||||
**automatically** scan the folder upon creation to search for valid handlers and register them for you.
|
scan the folder upon creation to search for valid handlers and register them for you.
|
||||||
|
|
||||||
Then you'll notice you can now use decorators. That's right, you can apply the usual decorators to your callback
|
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)
|
functions in a static way, i.e. **without having the Client instance around**: simply use ``@Client`` (Client class)
|
||||||
instead of the usual ``@app`` (Client instance) namespace and things will work just the same.
|
instead of the usual ``@app`` (Client instance) namespace and things will work just the same.
|
||||||
|
|
||||||
The ``main.py`` script is now at its bare minimum and cleanest state.
|
|
||||||
|
Loading…
Reference in New Issue
Block a user