[PYTHON] Around the place where the value of Errbot is stored

It seems that I will use it in small parts little by little, so I will summarize it a little.

Errbot startup configuration file

A configuration file to specify when starting Errbot. (By default config.py) The main thing is to put things that affect the overall behavior of Errbot, such as what to use for the back end and storage. [^ 1] You can refer to it with self.bot_config from the BotPlugin class.

Since the loaded file is treated as a python module as it is, the value can be changed during execution. However, if you restart, it will naturally return to the initial value.

[^ 1]: Of course, other values are OK

Code sample

pluginexample.py


class PluginExample(BotPlugin):
    @botcmd
    def example_admins(self, msg, args):
        return self.bot_config.BOT_ADMINS

Feature summary

--What you always have to put the core settings --Since it is easy to refer to from the plugin, it is suitable for putting a fixed value you want to share. ――It is not suitable for changing when the situation changes

In-module variables of plugins (instance variables and module variables)

Since the file that defines the BotPlugin class is normally imported individually as a module, the values in the same module can be referenced without any problem. However, since I am using Yapsy (?), It is a little unrealistic to refer to it from other plugins. [^ 2] Again, if you restart, it will return to the original value.

[^ 2]: There seems to be nothing I can't do, but I remember it was very troublesome.

Code sample

pluginexample.py


MESSAGES = (
    'Hello',
    'Hi',
)

class PluginExample(BotPlugin):
    @botcmd
    def example_msg(self, msg, args):
        return MESSAGES[0]

Feature summary

--Suitable for having fixed values that can only be used within plugins --URL used by plugin --Not suitable for reuse between multiple plugins --Cannot be persisted as it will be reset to the initial value when restarted --On the contrary, if it is not a problem even if it is not persisted, it is OK to have it in an instance variable (API cache etc.)

Plugin config

By defining get_configuration_template (), it is possible to manage the settings for each plugin. The setting values that can be handled here can be injected with a dedicated command for the bot.

The injected config can be accessed via self.config. In addition, this is stored in storage, so it will be loaded when it is restarted.

Code sample

pluginexample.py


class PluginExample(BotPlugin):
    def get_configuration_template(self):
        return {'ID_TOKEN': '00112233445566778899aabbccddeeff',
                'USERNAME':'changeme'}

    @botcmd
    def example_username(self, msg, args):
        return "Hi! I am " + self.config['USERNAME']

botcmd example [^ 3] [^ 4]

>>> !plugin config PluginExample {'ID_TOKEN' : '00112233445566778899aabbccddeeff', 'USERNAME':'changeme'}

[^ 3]: From http://errbot.io/en/latest/user_guide/plugin_development/configuration.html [^ 4]: Note that the grammar is eval, not json

Feature summary

--Basically, set variable values that are used only within the plugin. --This is good for API keys of plugins that use API --Because it is made persistent, be careful about the storage access right of the save destination [^ 5]

[^ 5]: For example, if you make it persistent with the Firbase plugin, it is json-based management, so anyone who can refer to the database can see the setting value.

Plugin data

As previously written, each plugin instance can have an independent storage area. Unlike config, there are no dedicated commands here, but you can read and write at any time at the appropriate timing of the plugin behavior. It is persisted in real time by storage.

Code sample

pluginexample.py


class PluginExample(BotPlugin):
    @botcmd
    def example_put(self, msg, args):
        put['msg'] = 'Test'
        return "pushed"

    @botcmd
    def example_pop(self, msg, args):
        put['msg'] = ''
        return "poped"

    @botcmd
    def example_see(self, msg, args):
        return put.get('msg', None)

Feature summary

--Basically, set a value according to the user's action --The result of throwing the API --User status --Because it is made persistent, be careful about the storage access right of the save destination [^ 5]

(Bonus) The guy who connected the code samples given above

pluginexample.py


from errbot import BotPlugin, botcmd


MESSAGES = (
    'Hello',
    'Hi',
)

class PluginExample(BotPlugin):
    def get_configuration_template(self):
        return {'ID_TOKEN': '00112233445566778899aabbccddeeff',
                'USERNAME':'changeme'}

    @botcmd
    def example_admins(self, msg, args):
        print(self.bot_config)
        # self.bot_config.BOT_ADMINS = ['None']
        return self.bot_config.BOT_ADMINS

    @botcmd
    def example_msg(self, msg, args):
        return MESSAGES[0]

    @botcmd
    def example_username(self, msg, args):
        return "Hi! I am " + self.config['USERNAME']

    @botcmd
    def example_put(self, msg, args):
        self['msg'] = 'Test'
        return "pushed"

    @botcmd
    def example_pop(self, msg, args):
        self['msg'] = ''
        return "poped"

    @botcmd
    def example_see(self, msg, args):
        return self.get('msg', None)

Recommended Posts

Around the place where the value of Errbot is stored
The value of pyTorch torch.var () is not distributed
I want to get the path of the directory where the running file is stored.
Find the definition of the value of errno
Unfortunately there is no sense of unity in the where method
Is the probability of precipitation correct?
About the return value of pthread_mutex_init ()
About the return value of the histogram.
Science "Is Saito the representative of Saito?"
What is the cause of the following error?
Where is the python instantiation process written?
[python] [meta] Is the type of python a type?
Get the value of the middle layer of NN
The update of conda is not finished.
The backslash of the Japanese keyboard is "ro"
Make the default value of the argument immutable
The answer of "1/2" is different between python2 and 3
The origin of Manjaro Linux is "Mount Kilimanjaro"
Watch out for the return value of __len__
FAQ: Why is the comparison of numbers inconsistent?
Try singular value decomposition of the daimyo matrix
Find the divisor of the value entered in python
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
Search by the value of the instance in the list
This is the only basic review of Python ~ 3 ~
Get the region where AWS Lambda is running
Get the location of the file where the exe is placed when the exe created by PyInstaller is executed
[Python Data Frame] When the value is empty, fill it with the value of another column.
Return value of quit ()-Is there anything returned by the "function that ends everything"?
The timing when the value of the default argument is evaluated is different between Ruby and Python.