From da902c228cae86c2b1ecf249907744837e13158b Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sun, 27 Jan 2019 10:48:28 -0500 Subject: [PATCH] Added contributions file and docs for Matrix support; refs #28 --- CONTRIBUTIONS.md | 21 ++++++++++++++++ README | 6 +++++ README.md | 1 + apprise/plugins/NotifyMatrix.py | 44 ++++++++++++++------------------- test/test_rest_plugins.py | 2 +- 5 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 CONTRIBUTIONS.md diff --git a/CONTRIBUTIONS.md b/CONTRIBUTIONS.md new file mode 100644 index 00000000..33ee7bf4 --- /dev/null +++ b/CONTRIBUTIONS.md @@ -0,0 +1,21 @@ +# Contributions to the apprise project + +## Creator & Maintainer + +* Chris Caron + +## Contributors + +The following users have contributed to this project and their deserved +recogition has been identified here. If you have contributed and wish +to be acknowledged for it, the syntax is as follows: + +``` +* [Your name or handle] <[email or website]> + * [Month Year] - [Brief summary of your contribution] +``` + +The contributors have been listed in alphabetical order: + +* Wim de With + * Dec 2018 - Added Matrix Support diff --git a/README b/README index 2c98b53a..cc1b5cb2 100644 --- a/README +++ b/README @@ -62,6 +62,12 @@ The table below identifies the services this tool supports and some example serv kodi://user@hostname kodi://user:password@hostname:port +* Matrix + matrix://token + matrix://user@token + matrixs://token?mode=slack + matrixs://user@token?mode=matrix + * Mattermost mmost://hostname/authkey mmost://hostname:80/authkey diff --git a/README.md b/README.md index 9dfda2ae..9c2444ac 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ The table below identifies the services this tool supports and some example serv | [IFTTT](https://github.com/caronc/apprise/wiki/Notify_ifttt) | ifttt:// | (TCP) 443 | ifttt://webhooksID/EventToTrigger
ifttt://webhooksID/EventToTrigger/Value1/Value2/Value3
ifttt://webhooksID/EventToTrigger/?Value3=NewEntry&Value2=AnotherValue | [Join](https://github.com/caronc/apprise/wiki/Notify_join) | join:// | (TCP) 443 | join://apikey/device
join://apikey/device1/device2/deviceN/
join://apikey/group
join://apikey/groupA/groupB/groupN
join://apikey/DeviceA/groupA/groupN/DeviceN/ | [KODI](https://github.com/caronc/apprise/wiki/Notify_kodi) | kodi:// or kodis:// | (TCP) 8080 or 443 | kodi://hostname
kodi://user@hostname
kodi://user:password@hostname:port +| [Matrix](https://github.com/caronc/apprise/wiki/Notify_matrix) | matrix:// | (TCP) 443 | matrix://token
matrix://user@token
matrixs://token?mode=slack
matrixs://user@token | [Mattermost](https://github.com/caronc/apprise/wiki/Notify_mattermost) | mmost:// | (TCP) 8065 | mmost://hostname/authkey
mmost://hostname:80/authkey
mmost://user@hostname:80/authkey
mmost://hostname/authkey?channel=channel
mmosts://hostname/authkey
mmosts://user@hostname/authkey
| [Prowl](https://github.com/caronc/apprise/wiki/Notify_prowl) | prowl:// | (TCP) 443 | prowl://apikey
prowl://apikey/providerkey | [Pushalot](https://github.com/caronc/apprise/wiki/Notify_pushalot) | palot:// | (TCP) 443 | palot://authorizationtoken diff --git a/apprise/plugins/NotifyMatrix.py b/apprise/plugins/NotifyMatrix.py index 476af47a..0f215ed3 100644 --- a/apprise/plugins/NotifyMatrix.py +++ b/apprise/plugins/NotifyMatrix.py @@ -2,7 +2,7 @@ # # Matrix WebHook Notify Wrapper # -# Copyright (C) 2018 Chris Caron , Wim de With +# Copyright (C) 2018-2019 Chris Caron # # This file is part of apprise. # @@ -30,8 +30,6 @@ from time import time from .NotifyBase import NotifyBase from .NotifyBase import HTTP_ERROR_MAP -from ..common import NotifyImageSize -from ..utils import compat_is_basestring # Token required as part of the API request VALIDATE_TOKEN = re.compile(r'[A-Za-z0-9]{64}') @@ -45,15 +43,18 @@ MATRIX_HTTP_ERROR_MAP.update({ 403: 'Unauthorized - Invalid Token.', }) + class MatrixNotificationMode(object): - SLACK = 0 - MATRIX = 1 + SLACK = "slack" + MATRIX = "matrix" + MATRIX_NOTIFICATION_MODES = ( MatrixNotificationMode.SLACK, MatrixNotificationMode.MATRIX, ) + class NotifyMatrix(NotifyBase): """ A wrapper for Matrix Notifications @@ -77,7 +78,7 @@ class NotifyMatrix(NotifyBase): # The maximum allowable characters allowed in the body per message body_maxlen = 1000 - def __init__(self, token, mode=None, **kwargs): + def __init__(self, token, mode=MatrixNotificationMode.MATRIX, **kwargs): """ Initialize Matrix Object """ @@ -90,10 +91,12 @@ class NotifyMatrix(NotifyBase): self.schema = 'http' if not isinstance(self.port, int): - self.notify_url = '%s://%s/api/v1/matrix/hook' % (self.schema, self.host) + self.notify_url = '%s://%s/api/v1/matrix/hook' % ( + self.schema, self.host) else: - self.notify_url = '%s://%s:%d/api/v1/matrix/hook' % (self.schema, self.host, self.port) + self.notify_url = '%s://%s:%d/api/v1/matrix/hook' % ( + self.schema, self.host, self.port) if not VALIDATE_TOKEN.match(token.strip()): self.logger.warning( @@ -111,13 +114,11 @@ class NotifyMatrix(NotifyBase): 'No user was specified; using %s.' % MATRIX_DEFAULT_USER) self.user = MATRIX_DEFAULT_USER - if not mode: - self.logger.warning( - 'No mode was specified, using Slack mode') - self.mode = MatrixNotificationMode.SLACK + if mode not in MATRIX_NOTIFICATION_MODES: + self.logger.warning('The mode specified (%s) is invalid.' % mode) + raise TypeError('The mode specified (%s) is invalid.' % mode) - else: - self.mode = mode + self.mode = mode self._re_formatting_map = { # New lines must become the string version @@ -159,7 +160,7 @@ class NotifyMatrix(NotifyBase): self.token, ) - if self.mode is MatrixNotificationMode.MATRIX: + if self.mode == MatrixNotificationMode.MATRIX: payload = self.__matrix_mode_payload(title, body, notify_type) else: @@ -188,8 +189,7 @@ class NotifyMatrix(NotifyBase): except KeyError: self.logger.warning( 'Failed to send Matrix ' - 'notification (error=%s).' % - r.status_code) + 'notification (error=%s).' % r.status_code) # Return; we're done notify_okay = False @@ -254,13 +254,7 @@ class NotifyMatrix(NotifyBase): results['token'] = NotifyBase.unquote(results['query']) if 'mode' in results['qsd'] and len(results['qsd']['mode']): - _map = { - 'slack': MatrixNotificationMode.SLACK, - 'matrix': MatrixNotificationMode.MATRIX, - } - try: - results['mode'] = _map[results['qsd']['mode'].lower()] - except KeyError: - pass + results['mode'] = results['qsd']\ + .get('mode', MatrixNotificationMode.MATRIX).lower() return results diff --git a/test/test_rest_plugins.py b/test/test_rest_plugins.py index ba6c0a04..5b07369b 100644 --- a/test/test_rest_plugins.py +++ b/test/test_rest_plugins.py @@ -579,7 +579,7 @@ TEST_URLS = ( }), # Name, port, token and invalid mode ('matrix://user@localhost:9000/%s?mode=foo' % ('a' * 64), { - 'instance': plugins.NotifyMatrix, + 'instance': TypeError, }), ('matrix://user@localhost:9000/%s?mode=slack' % ('a' * 64), { 'instance': plugins.NotifyMatrix,