Errbot: Python chatbot basics

Overview

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.

Basic knowledge before creation

Read local directory

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 calledBOT_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.

Run in test mode

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

plugin configuration file

Details will be described later.

Hello, World!

File creation

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!"

Code description

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`.

For example, when you enter ! hello hoge moge, the following values are stored.

Both are written just like strings, but msg is It looks like an instance of the ʻerrbot.backends.base.Message` class.

plugin metadata

In helloworld.plug, write as follows according to the example.

[Core]
Name = HelloWorld
Module = helloworld

[Python]
Version = 2+

[Documentation]
Description = Example "Hello, world!" plugin

Run the created bot

With the above description, the bot can be executed.

Execution of hello command


 >>> !hello
Hello, World!

Application in command creation

Automatic splitting of arguments

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.

Subcommand

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.

Argument split by argparse

By using the ʻarg_botcmddecorator instead of thebotcmd` 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)

Summary

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

Errbot: Python chatbot basics
Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Slack chatbot creation Python
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Implement Slack chatbot in Python
Basics of Python × GIS (Part 1)
Make Slack chatbot with Errbot
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
SNS Python basics made with Flask
Python
Getting Started with Python Basics of Python
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
5 Ways to Create a Python Chatbot
About the basics list of Python basics
(python) Deep Learning Library Chainer Basics Basics
Learn the basics of Python ① Beginners
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Statistical basics and Python, graphing, etc. (memo)
[Python3] Understand the basics of Beautiful Soup
Learn the basics while touching python Variables
[JAWS-UG CLI] Lambda Blueprint Explanation: Python2.7 Basics
Python learning basics ~ What is type conversion? ~