Made sensitivity configurable and added aliases

Before this commit, the name of the bot had to be hard coded into every
plugin. Now, the name of the bot to which it should be sensitive can be
set in the global config.hjson.

Furthermore, the default sensitivity of a plugin is only recognized at
the beginning of a sentence and is case insensitive.

Finally, the possibility to add aliases for plugins is added.
This commit is contained in:
Dennis Potter 2019-02-27 19:08:09 +01:00
parent c8b5926d78
commit ca86ade0fa
Signed by: Dennis
GPG Key ID: 186A8AD440942BAF
10 changed files with 74 additions and 33 deletions

11
aliases.hjson.template Normal file
View File

@ -0,0 +1,11 @@
##################################################
# This file contains an easy way to create aliases
# for commands.
#
# The make up of this should be:
# "<Original command>":"<Alias>"
##################################################
{
"Hello Bot":"Bot hello",
}

View File

@ -7,6 +7,7 @@
"character": {
"name": "Bot",
"sensitivity_name": "Bot",
"avatar": "images/default_avatar.png",
"avatar_mime": "image/png"
},

View File

@ -15,9 +15,11 @@ from matrix_bot_api.mregex_handler import MRegexHandler
from .custom_matrix_client.api import CustomMatrixHttpApi
from .custom_matrix_client.client import CustomMatrixClient
MESSAGES_DIR = os.path.join(os.path.dirname(__file__), 'messages')
DATA_LOCATION = os.path.join(os.path.dirname(__file__), '../data/bot.db')
ALIASES_LOCATION = 'aliases.hjson'
CONFIG_LOCATION = 'config.hjson'
DATA_LOCATION = 'data/bot.db'
MESSAGES_DIR = os.path.join(os.path.dirname(__file__), 'messages')
HELP_LOCATION = MESSAGES_DIR + '/help'
def eprint(*args, **kwargs):
@ -26,8 +28,16 @@ def eprint(*args, **kwargs):
class MatrixBotAPI:
# rooms - List of rooms ids to operate in, or None to accept all rooms
def __init__(self, config, rooms=None):
self.config = config
def __init__(self, rooms=None):
# Load the configuration
with open(CONFIG_LOCATION) as hjson_data:
self.config = hjson.load(hjson_data)
# Load the aliases
with open(ALIASES_LOCATION) as hjson_data:
self.aliases = hjson.load(hjson_data)
print(self.aliases)
self.username = self.config['bot_credentials']['username']
# Authenticate with given credentials
@ -80,8 +90,7 @@ class MatrixBotAPI:
self.add_plugins()
# Add callback for help function
self.help_handler = MRegexHandler(
self.config['triggers']['help'], self.help)
self.help_handler = MRegexHandler("help", self.help, self)
self.add_handler(self.help_handler)

View File

@ -11,14 +11,23 @@ class MRegexHandler(MHandler):
# regex_str - Regular expression to test message against
#
# handle_callback - Function to call if messages matches regex
def __init__(self, regex_str, handle_callback):
def __init__(self, regex_str, handle_callback, bot):
MHandler.__init__(self, self.test_regex, handle_callback)
self.regex_str = regex_str
self.name = bot.config['character']['sensitivity_name']
regex_str_temp = "{} {}".format(self.name, regex_str)
self.regex_str = regex_str_temp
for key,value in bot.aliases.items():
if (value == regex_str_temp):
self.regex_str += "|{}".format(key)
def test_regex(self, room, event):
r = r'^{}'.format(self.regex_str)
# Test the message and see if it matches the regex
if event['type'] == "m.room.message":
if re.search(self.regex_str, event['content']['body']):
if re.search(r, event['content']['body'], re.I):
# The message matches the regex, return true
return True

View File

@ -53,14 +53,25 @@ class Plugin:
# Define sensitivity
self.handler = []
self.handler.append(MRegexHandler("Peter evenementen lijst",
self.list_callback))
self.handler.append(MRegexHandler("Peter evenementen info",
self.info_callback))
self.handler.append(MRegexHandler("Peter evenementen chat",
self.chat_callback))
self.handler.append(MRegexHandler("Peter evenementen deelnemers",
self.participants_callback))
self.handler.append(MRegexHandler(
"Peter evenementen lijst",
self.list_callback,
bot))
self.handler.append(MRegexHandler(
"Peter evenementen info",
self.info_callback,
bot))
self.handler.append(MRegexHandler(
"Peter evenementen chat",
self.chat_callback,
bot))
self.handler.append(MRegexHandler(
"Peter evenementen deelnemers",
self.participants_callback,
bot))
# Save parent bot
self.bot = bot

@ -1 +1 @@
Subproject commit 292b046b629ae9ad0be0e5f460aa02901cd69083
Subproject commit bd325511f171871efc6a2e77b875c6fb26f23283

View File

@ -13,7 +13,7 @@ class Plugin:
# Define sensitivity
self.handler = []
self.handler.append(MRegexHandler("Hello bot", self.hello_callback))
self.handler.append(MRegexHandler("Hello", self.hello_callback, bot))
# Save parent bot
self.bot = bot

View File

@ -35,9 +35,15 @@ class Plugin:
# Define sensitivity
self.handler = []
self.handler.append(MRegexHandler("Peter wiki search", self.search))
self.handler.append(MRegexHandler(
"Peter wiki search",
self.search,
bot))
self.handler.append(MRegexHandler("Peter wiki login", self.login))
self.handler.append(MRegexHandler(
"Peter wiki login",
self.login,
bot))
# Save parent bot

View File

@ -43,7 +43,10 @@ class Plugin:
# Define sensitivity
self.handler = []
self.handler.append(MRegexHandler("Peter coupon", self.grab_coupon_callback))
self.handler.append(MRegexHandler(
"Peter coupon",
self.grab_coupon_callback,
bot))
# initialize WooCommerce connection
self.wcapi = API(

13
run.py
View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""
This is Peter. Peter is using the Python Matrix Bot API
This is a Bot. He/she is using the Python Matrix Bot API
"""
__author__ = "Dennis Potter"
@ -11,23 +11,14 @@ __version__ = "0.5.0"
__maintainer__ = "Dennis Potter"
__email__ = "dennis@dennispotter.eu"
import hjson
import os
import time
import threading
# Bot API import
from matrix_bot_api.matrix_bot_api import MatrixBotAPI
CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.hjson')
def main():
# Load the configuration
with open(CONFIG_LOCATION) as hjson_data:
config = hjson.load(hjson_data)
# Create an instance of the MatrixBotAPI
bot = MatrixBotAPI(config)
bot = MatrixBotAPI()
# Start polling
bot.start_polling()