1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
Dynamic plugins (advanced)
==========================
Sometimes the list of commands the bot wants to expose is not known at
plugin development time.
For example, you have a remote service with commands that can
be set externally.
This feature allows you to define and update on the fly plugins and their
available commands.
Defining new commands
---------------------
You can create a commands from scratch with :class:`~errbot.Command`. By default it will be a :func:`~errbot.botcmd`.
.. code-block:: python
# from a lambda
my_command1 = Command(lambda plugin, msg, args: 'received %s' % msg, name='my_command', doc='documentation of my_command')
# or from a function
def my_command(plugin, msg, args):
"""
documentation of my_command.
"""
return 'received %s' % msg
my_command2 = Command(my_command)
.. note::
the function will by annotated by a border effect, be sure to use a local function if you want to derive commands
for the same underlying function.
Registering the new plugin
--------------------------
Once you have your series of Commands defined, you can package them in a plugin and expose them on errbot with :func:`~errbot.BotPlugin.create_dynamic_plugin`.
.. code-block:: python
# from activate, another bot command, poll etc.
self.create_dynamic_plugin('my_plugin', (my_command1, my_command2))
Refreshing a plugin
-------------------
You need to detroy and recreate the plugin to refresh its commands.
.. code-block:: python
self.destroy_dynamic_plugin('my_plugin')
self.create_dynamic_plugin('my_plugin', (my_command1, my_command2, my_command3))
Customizing the type of commands and parameters
-----------------------------------------------
You can use other type of commands by specifying cmd_type and pass them parameters with cmd_args and cmd_kwargs.
.. code-block:: python
# for example a botmatch
re1 = Command(lambda plugin, msg, match: 'fffound',
name='ffound',
cmd_type=botmatch,
cmd_args=(r'^.*cheese.*$',))
# or a split_args_with
saw = Command(lambda plugin, msg, args: '+'.join(args),
name='splitme',
cmd_kwargs={'split_args_with': ','})
|