From ca86ade0fa101160c1c424a32be19e158dca5b26 Mon Sep 17 00:00:00 2001 From: Dennis Potter Date: Wed, 27 Feb 2019 19:08:09 +0100 Subject: [PATCH] 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. --- aliases.hjson.template | 11 ++++++++ config.hjson.template | 1 + matrix_bot_api/matrix_bot_api.py | 21 ++++++++++----- matrix_bot_api/mregex_handler.py | 15 ++++++++--- plugins/admidio_events/admidio_events.py | 27 +++++++++++++------ plugins/admidio_events/admidio_python_api | 2 +- plugins/hello/hello.py | 2 +- plugins/mediawiki_search/mediawiki_search.py | 10 +++++-- .../woocommerce_coupons.py | 5 +++- run.py | 13 ++------- 10 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 aliases.hjson.template diff --git a/aliases.hjson.template b/aliases.hjson.template new file mode 100644 index 0000000..48f44d8 --- /dev/null +++ b/aliases.hjson.template @@ -0,0 +1,11 @@ +################################################## +# This file contains an easy way to create aliases +# for commands. +# +# The make up of this should be: +# "":"" +################################################## + +{ + "Hello Bot":"Bot hello", +} diff --git a/config.hjson.template b/config.hjson.template index e7872df..feb849a 100644 --- a/config.hjson.template +++ b/config.hjson.template @@ -7,6 +7,7 @@ "character": { "name": "Bot", + "sensitivity_name": "Bot", "avatar": "images/default_avatar.png", "avatar_mime": "image/png" }, diff --git a/matrix_bot_api/matrix_bot_api.py b/matrix_bot_api/matrix_bot_api.py index 66af0fc..9fa120a 100644 --- a/matrix_bot_api/matrix_bot_api.py +++ b/matrix_bot_api/matrix_bot_api.py @@ -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) diff --git a/matrix_bot_api/mregex_handler.py b/matrix_bot_api/mregex_handler.py index 32aa684..ce2dd28 100644 --- a/matrix_bot_api/mregex_handler.py +++ b/matrix_bot_api/mregex_handler.py @@ -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 diff --git a/plugins/admidio_events/admidio_events.py b/plugins/admidio_events/admidio_events.py index 7e71987..851b581 100644 --- a/plugins/admidio_events/admidio_events.py +++ b/plugins/admidio_events/admidio_events.py @@ -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 diff --git a/plugins/admidio_events/admidio_python_api b/plugins/admidio_events/admidio_python_api index 292b046..bd32551 160000 --- a/plugins/admidio_events/admidio_python_api +++ b/plugins/admidio_events/admidio_python_api @@ -1 +1 @@ -Subproject commit 292b046b629ae9ad0be0e5f460aa02901cd69083 +Subproject commit bd325511f171871efc6a2e77b875c6fb26f23283 diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index 8f23e4a..909be4f 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -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 diff --git a/plugins/mediawiki_search/mediawiki_search.py b/plugins/mediawiki_search/mediawiki_search.py index 5835043..936f326 100644 --- a/plugins/mediawiki_search/mediawiki_search.py +++ b/plugins/mediawiki_search/mediawiki_search.py @@ -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 diff --git a/plugins/woocommerce_coupons/woocommerce_coupons.py b/plugins/woocommerce_coupons/woocommerce_coupons.py index 25794be..10a4419 100644 --- a/plugins/woocommerce_coupons/woocommerce_coupons.py +++ b/plugins/woocommerce_coupons/woocommerce_coupons.py @@ -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( diff --git a/run.py b/run.py index 11aebec..87f746c 100755 --- a/run.py +++ b/run.py @@ -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()