Errbot is a Python chatbot.
This page summarizes what I learned from how to make an Errbot plugin (self-made bot). The content is roughly in line with the official documentation Plugin Development. It should be easy to understand if you have a basic knowledge of Python.
Please refer to Previous article for environment construction.
Basically the plugin in Errbot is
It is managed via git with the built-in ! Repos
command.
However, it is troublesome to upload the source to the git repository during development. Load the plugin from your local directory.
In the config.py
generated by ʻerrbot --init There is a setting item called
BOT_EXTRA_PLUGIN_DIR`.
Enter the path of the directory you want to be the local directory here.
However, the plugins
directory should be specified by default.
You should be able to use it as it is.
To debug the bot without connecting to other servers
You can run in test mode by specifying --text
or -T
at startup.
It's text
instead of test
.
Boot in test mode
$ errbot --text
.plug
file: Includes plugin metadata in INI file format.py
file: Contains Python code that is actually processed by the pluginDetails will be described later.
Hello, World!
In the directory specified by BOT_EXTRA_PLUGIN_DIR
Create the HelloWorld
directory and its configuration files.
plugins/
|~err-example/
| |-example.plug
| `-example.py
`~HelloWorld/
|-helloworld.plug
`-helloworld.py
In helloworld.py, write as follows according to the example.
helloworld.py
from errbot import BotPlugin, botcmd
class HelloWorld(BotPlugin):
"""Example 'Hello, world!' plugin for Errbot"""
@botcmd
def hello(self, msg, args):
"""Say hello to the world"""
return "Hello, world!"
First, with the class that inherits from the plugin Import the decorator to create the function that corresponds to the command.
helloworld.py
from errbot import BotPlugin, botcmd
Inherit the BotPlugin class and generate a class for the plugin.
The docstring part is displayed when you use the ! Help
command.
helloworld.py
class HelloWorld(BotPlugin):
"""Example 'Hello, world!' plugin for Errbot"""
Next, write the function and decorate it with @ botcmd
.
The function name becomes the command name, and the bot will accept the ! hello
command.
The docstring in the function is also displayed when you use the ! Help
command.
helloworld.py
@botcmd
def hello(self, msg, args):
"""Say hello to the world"""
return "Hello, world!"
The hello
function takes multiple arguments, msg
and ʻargs`.
msg
: Whole text entered by the userFor example, when you enter ! hello hoge moge
, the following values are stored.
msg
: '!hello hoge moge'
args
: 'hoge moge'
Both are written just like strings, but msg
is
It looks like an instance of the ʻerrbot.backends.base.Message` class.
In helloworld.plug, write as follows according to the example.
[Core]
Name = HelloWorld
Module = helloworld
[Python]
Version = 2+
[Documentation]
Description = Example "Hello, world!" plugin
Name
: The name of the executable file without .py
Module
: Module that will be the bot class (in the file specified by Name
)Version
: Corresponding Python version (details unknown)Description
: Plugin details (details unknown)With the above description, the bot can be executed.
Execution of hello command
>>> !hello
Hello, World!
Arguments can be specified for the botcmd
decorator.
By specifying split_args_with
as a keyword argument
You can receive the above ʻargs` variables as a list.
Argument split
@botcmd(split_args_with=None)
def action(self, mess, args):
# !When you type action one two three
#args['one', 'two', 'three']To receive
Specifying split_args_with
behaves likestr.split ()
None
indicates white space.
If _
is used in the function name, it will be treated as a subcommand after _
.
This is useful for command grouping.
Subcommand
@botcmd
def basket_add(self, mess, args):
# !Supports input basket add
pass
@botcmd
def basket_remove(self, mess, args):
# !Supports the input basket remove
pass
At this time, ʻadd and
remove are just part of the command. It doesn't seem to be stored as an argument in ʻargs
.
By using the ʻarg_botcmddecorator instead of the
botcmd`
You can define the arguments in the format argparse.
Multiple decorators can be specified by overlapping them.
Argument specification by argparse
@arg_botcmd('first_name', type=str)
@arg_botcmd('--last-name', dest='last_name', type=str)
@arg_botcmd('--favorite', dest='favorite_number', type=int, default=42)
def hello(self, mess, first_name=None, last_name=None, favorite_number=None):
# !hello Err --last-When you type name Bot
# first_name is'Err'
# last_name is'Bot'
# favorite_number is 42 (default value)
I wrote about the basic part of creating a plugin. Because the argument can be received freely to some extent All you have to do is write the process you want to execute in Python.
In addition, use of commands using regular expressions and utilization of templates, etc. It seems to have many functions though it is simple. If you are interested, please read the Documentation.
Recommended Posts