12. 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.
12.1. Defining new commands¶
You can create a commands from scratch with Command
. By default it will be a botcmd()
.
# 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.
12.2. 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 create_dynamic_plugin()
.
# from activate, another bot command, poll etc.
self.create_dynamic_plugin('my_plugin', (my_command1, my_command2))
12.3. Refreshing a plugin¶
You need to detroy and recreate the plugin to refresh its commands.
self.destroy_dynamic_plugin('my_plugin')
self.create_dynamic_plugin('my_plugin', (my_command1, my_command2, my_command3))
12.4. 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.
# 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': ','})