| 1 |
Hello, world! |
| 2 |
============= |
| 3 |
|
| 4 |
On the :doc:`homepage </index>`, we showed you the following *"Hello |
| 5 |
world!"* plugin as an example: |
| 6 |
|
| 7 |
.. code-block:: python |
| 8 |
|
| 9 |
from errbot import BotPlugin, botcmd |
| 10 |
|
| 11 |
class HelloWorld(BotPlugin): |
| 12 |
"""Example 'Hello, world!' plugin for Errbot""" |
| 13 |
|
| 14 |
@botcmd |
| 15 |
def hello(self, msg, args): |
| 16 |
"""Say hello to the world""" |
| 17 |
return "Hello, world!" |
| 18 |
|
| 19 |
In this chapter, you will learn exactly how this plugin works. |
| 20 |
|
| 21 |
I will assume you've configured the `BOT_EXTRA_PLUGIN_DIR` as |
| 22 |
described in the previous chapter. To get started, create a new, |
| 23 |
empty directory named `HelloWorld` inside this directory. |
| 24 |
|
| 25 |
Create a new file called `helloworld.py` inside the `HelloWorld` |
| 26 |
directory you just created. This file contains all the logic for your |
| 27 |
plugin, so copy and paste the above example code into it. |
| 28 |
|
| 29 |
Anatomy of a BotPlugin |
| 30 |
---------------------- |
| 31 |
|
| 32 |
Although this plugin is only 9 lines long, there is already a lot of |
| 33 |
interesting stuff going on here. Lets go through it step by step. |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
from errbot import BotPlugin, botcmd |
| 38 |
|
| 39 |
This should be pretty self-explanatory. Here we import the |
| 40 |
:class:`~errbot.botplugin.BotPlugin` class and the |
| 41 |
:func:`~errbot.decorators.botcmd` decorator. These let us build a |
| 42 |
class that can be loaded as a plugin and allow us to mark methods of |
| 43 |
that class as bot commands. |
| 44 |
|
| 45 |
.. code-block:: python |
| 46 |
|
| 47 |
class HelloWorld(BotPlugin): |
| 48 |
"""Example 'Hello, world!' plugin for Errbot""" |
| 49 |
|
| 50 |
Here we define the class that makes up our plugin. The name of your |
| 51 |
class, `HelloWorld` in this case, is what will make up the name of |
| 52 |
your plugin. This name will be used in commands such as `!status`, |
| 53 |
`!plugin load` and `!plugin unload` |
| 54 |
|
| 55 |
The class' docstring is used to automatically populate the built-in |
| 56 |
command documentation. It will be visible when issuing the `!help` |
| 57 |
command. |
| 58 |
|
| 59 |
.. warning:: |
| 60 |
A plugin should only ever contain a single class inheriting from :class:`~errbot.botplugin.BotPlugin` |
| 61 |
|
| 62 |
.. code-block:: python |
| 63 |
|
| 64 |
@botcmd |
| 65 |
def hello(self, msg, args): |
| 66 |
"""Say hello to the world""" |
| 67 |
return "Hello, world!" |
| 68 |
|
| 69 |
This method, `hello`, is turned into a bot command which can be |
| 70 |
executed because it is decorated with the |
| 71 |
:func:`~errbot.decorators.botcmd` decorator. Just as with the class |
| 72 |
docstring above, the docstring here is used to populate the `!help` |
| 73 |
command. |
| 74 |
|
| 75 |
The name of the method, `hello` in this case, will be used as the |
| 76 |
name of the command. That means this method creates the `!hello` |
| 77 |
command. |
| 78 |
|
| 79 |
.. note:: |
| 80 |
The method name must comply with the usual Python naming |
| 81 |
conventions for `identifiers <https://docs.python.org/3/reference/lexical_analysis.html?highlight=identifiers%20keywords#identifiers>`_ |
| 82 |
, that is, they may not begin with a digit (like ``911`` but only with a letter or underscore, so ``_911`` would work) |
| 83 |
and cannot be any of the `reserved keywords <https://docs.python.org/3/reference/lexical_analysis.html?highlight=identifiers%20keywords#keywords>`_ |
| 84 |
such as ``pass`` (instead use ``password``) etc. |
| 85 |
|
| 86 |
.. note:: |
| 87 |
Should multiple plugins define the same command, they will be |
| 88 |
dynamically renamed (by prefixing them with the plugin name) so |
| 89 |
that they no longer clash with each other. |
| 90 |
|
| 91 |
If we look at the function definition, we see it takes two parameters, |
| 92 |
`msg` and `args`. The first is a :class:`~errbot.backends.base.Message` |
| 93 |
object, which represents the full message object received by Errbot. The |
| 94 |
second is a string (or a list, if using the `split_args_with` |
| 95 |
parameter of :func:`~errbot.decorators.botcmd`) with the arguments |
| 96 |
passed to the command. |
| 97 |
|
| 98 |
For example, if a user were to say `!hello Mister Errbot`, `args` would |
| 99 |
be the string `"Mister Errbot"`. |
| 100 |
|
| 101 |
Finally, you can see we return with the string `Hello, world!`. This |
| 102 |
defines the response that Errbot should give. In this case, it makes all |
| 103 |
executions of the `!hello` command return the message *Hello, world!*. |
| 104 |
|
| 105 |
.. note:: |
| 106 |
If you return `None`, Errbot will not respond with any kind of |
| 107 |
message when executing the command. |
| 108 |
|
| 109 |
|
| 110 |
Plugin metadata |
| 111 |
--------------- |
| 112 |
|
| 113 |
We have our plugin itself ready, but if you start the bot now, you'll |
| 114 |
see it still won't load your plugin. What gives? |
| 115 |
|
| 116 |
As it turns out, you need to supply a file with some meta-data |
| 117 |
alongside your actual plugin file. This is a file that ends with the |
| 118 |
extension `.plug` and it is used by Errbot to identify and load plugins. |
| 119 |
|
| 120 |
Lets go ahead and create ours. Place the following in a file called |
| 121 |
`helloworld.plug`: |
| 122 |
|
| 123 |
.. code-block:: ini |
| 124 |
|
| 125 |
[Core] |
| 126 |
Name = HelloWorld |
| 127 |
Module = helloworld |
| 128 |
|
| 129 |
[Python] |
| 130 |
Version = 3 |
| 131 |
|
| 132 |
[Documentation] |
| 133 |
Description = Example "Hello, world!" plugin |
| 134 |
|
| 135 |
.. note:: |
| 136 |
This INI-style file is parsed using the Python `configparser |
| 137 |
<https://docs.python.org/3/library/configparser.html>`_ class. |
| 138 |
Make sure to use a `valid |
| 139 |
<https://docs.python.org/3/library/configparser.html#supported-ini-file-structure>`_ |
| 140 |
file structure. |
| 141 |
|
| 142 |
Lets look at what this does. We see two sections, `[Core]` , |
| 143 |
and `[Documentation]`. The `[Core]` section is what tells |
| 144 |
Errbot where it can actually find the code for this plugin. |
| 145 |
|
| 146 |
The key `Module` should point to a module that Python can find and |
| 147 |
import. Typically, this is the name of the file you placed your code |
| 148 |
in with the `.py` suffix removed. |
| 149 |
|
| 150 |
The key `Name` should be identical to the name you gave to the class |
| 151 |
in your plugin file, which in our case was `HelloWorld`. While these |
| 152 |
names can differ, doing so is not recommended. |
| 153 |
|
| 154 |
.. note:: |
| 155 |
If you're wondering why you have to specify it when it should be |
| 156 |
the same as the class name anyway, this has to do with technical |
| 157 |
limitations that we won't go into here. |
| 158 |
|
| 159 |
The `[Documentation]` section will be explained in more detail |
| 160 |
further on in this guide, but you should make sure to at least have |
| 161 |
the `Description` item here with a short description of your plugin. |
| 162 |
|
| 163 |
Python Submodules |
| 164 |
----------------- |
| 165 |
|
| 166 |
In cases where the plugin code base is large and complex, it may be desirable to break the code |
| 167 |
into submodules to be imported by the plugin. The following directory tree shows a commonly used |
| 168 |
layout for submodules: |
| 169 |
|
| 170 |
.. code-block:: bash |
| 171 |
|
| 172 |
plugins |
| 173 |
├── LICENSE |
| 174 |
├── helloworld.plug |
| 175 |
├── helloworld.py |
| 176 |
├── README.md |
| 177 |
├── requirements.txt |
| 178 |
├── lib |
| 179 |
│ ├── __init__.py |
| 180 |
│ ├── moduleA.py |
| 181 |
│ ├── moduleB.py |
| 182 |
│ ├── moduleC.py |
| 183 |
|
| 184 |
The presence of `__init__.py` indicates `lib` is a Python regular package. Assuming `moduleA` has |
| 185 |
the function `invert_string()`, the `helloworld` plugin can import it and use it with the following syntax: |
| 186 |
|
| 187 |
.. code-block:: python |
| 188 |
|
| 189 |
from lib.moduleA import invert_string |
| 190 |
from errbot import BotPlugin, botcmd |
| 191 |
|
| 192 |
class HelloWorld(BotPlugin): |
| 193 |
"""Example 'Hello, world!' plugin for Errbot""" |
| 194 |
|
| 195 |
@botcmd |
| 196 |
def hello(self, msg, args): |
| 197 |
"""Say hello to the world""" |
| 198 |
return invert_string("Hello, world!") |
| 199 |
|
| 200 |
Packaging |
| 201 |
--------- |
| 202 |
|
| 203 |
A plugin can be packaged and distributed through pypi.org. The errbot plugin system uses entrypoints in setuptools to find available plugins. |
| 204 |
|
| 205 |
The two entrypoint available are |
| 206 |
|
| 207 |
* `errbot.plugins` - normal plugin and flows |
| 208 |
* `errbot.backend_plugins` - backend plugins for collaboration providers |
| 209 |
|
| 210 |
To get this setup, add this block of code to `setup.py`. |
| 211 |
|
| 212 |
.. code-block:: python |
| 213 |
|
| 214 |
entry_points = { |
| 215 |
"errbot.plugins": [ |
| 216 |
"helloworld = helloWorld:HelloWorld", |
| 217 |
] |
| 218 |
} |
| 219 |
|
| 220 |
Optionally, you may need to include a `MANIFEST.in` to include files of the repo |
| 221 |
|
| 222 |
.. code-block:: python |
| 223 |
|
| 224 |
include *.py *.plug |
| 225 |
|
| 226 |
|
| 227 |
Wrapping up |
| 228 |
----------- |
| 229 |
|
| 230 |
If you've followed along so far, you should now have a working |
| 231 |
*Hello, world!* plugin for Errbot. If you start your bot, it should load |
| 232 |
your plugin automatically. |
| 233 |
|
| 234 |
You can verify this by giving the `!status` command, which should |
| 235 |
respond with something like the following:: |
| 236 |
|
| 237 |
Yes I am alive... |
| 238 |
With these plugins (A=Activated, D=Deactivated, B=Blacklisted, C=Needs to be configured): |
| 239 |
[A] ChatRoom |
| 240 |
[A] HelloWorld |
| 241 |
[A] VersionChecker |
| 242 |
[A] Webserver |
| 243 |
|
| 244 |
If you don't see your plugin listed or it shows up as unloaded, make |
| 245 |
sure to start your bot with *DEBUG*-level logging enabled and pay |
| 246 |
close attention to what it reports. You will most likely see an error |
| 247 |
being reported somewhere along the way while Errbot starts up. |
| 248 |
|
| 249 |
|
| 250 |
Next steps |
| 251 |
---------- |
| 252 |
|
| 253 |
You now know enough to create very simple plugins, but we have barely |
| 254 |
scratched the surface of what Errbot can do. The rest of this guide will |
| 255 |
be a recipe-style set of topics that cover all the advanced features |
| 256 |
Errbot has to offer. |