Debian Codegraph @

docs/user_guide/plugin_development/dynaplugs.rst

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