The core of this chatbot is based on [github.com/shawnanastasio/python-matrix-bot-api](https://github.com/shawnanastasio/python-matrix-bot-api/) which was published under GPL-3.0. It is heavily modified to create an API to easily add plugins. This bot is intended to operate on the [Matrix network](https://matrix.org). This code has two purposes. Firstly, and mainly, it serves as virtual assistent in the Matrix based communication platform of members of the Dutch student association [Alcuinus](https://alcuinus.nl). Secondly, the bot's easy to use API makes the development of a simple plugin an approachable first project for new members of Alcuinus' IT working groups during their training. ## Content * [Installation & Requirements](#installation-requirements) * [Plugins](#plugins) * [List of available plugins](#list-of-available-plugins) * [API](#api) * [Mandatory files](#mandatory-files) * [Help template](#help-template) * [Plugin class template](#plugin-class-template) * [Additional tips](#additional-tips) ## Installation & Requirements: ... ## Plugins The bot has different plugins that can be activated by sending `[Keyword in bot's config.json] [Keyword(s) in plugin's config.json]` to a room in which the bot is present. Below, under [List of available plugins](list-of-available-plugins), you can first find a list of already available plugins. Then, under [API](#api), you can find a description on how to develop a new plugin. ### List of available plugins: * [Admidio events plugin](src/branch/master/plugins/events) ### API #### Mandatory files To create a plugin, a few mandatory files must be present. The tree below shows the general structure of a plugin. A new plugin must be placed in a directory with the same name. The main class (more on that later) of the plugin must be defined in a Python file with the same name within that directory. The event may not use bot's main configuration file. All configuration must be placed in a separate `config.json`. A subdirectory `messages` is used to store messages. Within this directory, a file `help` must be present. The content of this file must be returned with a method of the plugin class (more on that later). ``` . └── plugins ├── │ ├── __init__.py │ ├── .py │ ├── config.json │ └── messages │ └── help ├── ╎ ╎ └── ``` #### Help template The help function must be created in [HTML](https://www.w3schools.com/html/). The header must be the string that is used to activate the plugin (``). Every feature that is listed must start with the string that is used to activate that particular feature. The code belows shows an example of such a help function. ```html
  • feature1: Lorem ipsum dolor sit amet,
  • feature2: consectetur adipiscing elit.
  • feature3: Nunc ac volutpat nisi, id hendrerit tellus.

``` #### Plugin class template The code below shows the template for a simple plugin with a few features. The name of the class of every plugin must be `Plugin`. * It has a method `__init__()` which is executed every time the bot is started. Usually, this should load the configuration file, it should set the sensitivity on which it should execute certain callback functions, it should save the parent object, start additional threads, and execute the setup. * It has a method `setup()` which is only executed once (ever). This can be used, for example, to create [SQLite](https://sqlite.org) tables. All data should be saved in subdirectory for that plugin in `data`. See the example below, where `` initiates an SQLite database. ``` . ├── plugins └── data ├── │ └── plugin_data.db ├── ╎ ╎ └── ``` * It has one method `thread_method1()` which is spawned and runs continuously in the background. **Please keep performance in mind when doing this!***. For example, is it actually necessary to continuously run the thread, or is execution only necessary every 5 minutes? * For every sensitivity that is defined, a method must be defined that acts on it. * The Plugin class must contain a method `help()` which only returns the content of `plugin1/messages/help` ```python CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.json') HELP_LOCATION = os.path.join(os.path.dirname(__file__), 'help') class Plugin: """ Description of event plugin """ def __init__(self, bot): # Load the configuration with open(CONFIG_LOCATION) as json_data: self.config = json.load(json_data) # Define sensitivity self.handler = MRegexHandler("Peter ", self.callback1) self.handler = MRegexHandler("Peter ", self.callback2) # Save parent bot self.bot = bot # Start thread to check events self.thread1 = threading.Thread(target=self.thread1_method) self.thread1.start() self.setup() def setup(self): """This function only runs once, ever. It installs the plugin""" try: # Install some stuff, if not already installed except sqlite3.OperationalError: # For example, when trying to initialize an SQLite DB # which already exists, sqlite3.OperationalError is thrown pass def thread1_method(self): """This function continuously loops""" while not self.bot.cancel: # Do something. For example, call other methods. # Sleep time.sleep(self.config['update_time_span'] * 60) def callback1(self, room, event): """Act on first sensitivity""" room.send_text("You typed 'Peter '") def callback2(self, room, event): """Act on second sensitivity""" room.send_text("You typed 'Peter '") def help(self): return open(HELP_LOCATION, mode="r").read() ``` #### Additional tips * Use [f-strings](https://www.python.org/dev/peps/pep-0498/#how-to-denote-f-strings) if possible. ...