It seems that I will use it in small parts little by little, so I will summarize it a little.
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
pluginexample.py
class PluginExample(BotPlugin):
@botcmd
def example_admins(self, msg, args):
return self.bot_config.BOT_ADMINS
--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
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.
pluginexample.py
MESSAGES = (
'Hello',
'Hi',
)
class PluginExample(BotPlugin):
@botcmd
def example_msg(self, msg, args):
return MESSAGES[0]
--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.)
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.
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
--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.
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.
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)
--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]
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)