Added support for matrix notices (#460)

pull/470/head
Chris Caron 3 years ago committed by GitHub
parent ac7eb86185
commit 08b0577ed8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -67,6 +67,21 @@ IS_ROOM_ID = re.compile(
r'(?P<home_server>[a-z0-9.-]+))?\s*$', re.I) r'(?P<home_server>[a-z0-9.-]+))?\s*$', re.I)
class MatrixMessageType(object):
"""
The Matrix Message types
"""
TEXT = "text"
NOTICE = "notice"
# matrix message types are placed into this list for validation purposes
MATRIX_MESSAGE_TYPES = (
MatrixMessageType.TEXT,
MatrixMessageType.NOTICE,
)
class MatrixWebhookMode(object): class MatrixWebhookMode(object):
# Webhook Mode is disabled # Webhook Mode is disabled
DISABLED = "off" DISABLED = "off"
@ -81,7 +96,7 @@ class MatrixWebhookMode(object):
T2BOT = "t2bot" T2BOT = "t2bot"
# webhook modes are placed ito this list for validation purposes # webhook modes are placed into this list for validation purposes
MATRIX_WEBHOOK_MODES = ( MATRIX_WEBHOOK_MODES = (
MatrixWebhookMode.DISABLED, MatrixWebhookMode.DISABLED,
MatrixWebhookMode.MATRIX, MatrixWebhookMode.MATRIX,
@ -206,6 +221,12 @@ class NotifyMatrix(NotifyBase):
'values': MATRIX_WEBHOOK_MODES, 'values': MATRIX_WEBHOOK_MODES,
'default': MatrixWebhookMode.DISABLED, 'default': MatrixWebhookMode.DISABLED,
}, },
'msgtype': {
'name': _('Message Type'),
'type': 'choice:string',
'values': MATRIX_MESSAGE_TYPES,
'default': MatrixMessageType.TEXT,
},
'to': { 'to': {
'alias_of': 'targets', 'alias_of': 'targets',
}, },
@ -214,7 +235,8 @@ class NotifyMatrix(NotifyBase):
}, },
}) })
def __init__(self, targets=None, mode=None, include_image=False, **kwargs): def __init__(self, targets=None, mode=None, msgtype=None,
include_image=False, **kwargs):
""" """
Initialize Matrix Object Initialize Matrix Object
""" """
@ -240,13 +262,21 @@ class NotifyMatrix(NotifyBase):
self._room_cache = {} self._room_cache = {}
# Setup our mode # Setup our mode
self.mode = MatrixWebhookMode.DISABLED \ self.mode = self.template_args['mode']['default'] \
if not isinstance(mode, six.string_types) else mode.lower() if not isinstance(mode, six.string_types) else mode.lower()
if self.mode and self.mode not in MATRIX_WEBHOOK_MODES: if self.mode and self.mode not in MATRIX_WEBHOOK_MODES:
msg = 'The mode specified ({}) is invalid.'.format(mode) msg = 'The mode specified ({}) is invalid.'.format(mode)
self.logger.warning(msg) self.logger.warning(msg)
raise TypeError(msg) raise TypeError(msg)
# Setup our message type
self.msgtype = self.template_args['msgtype']['default'] \
if not isinstance(msgtype, six.string_types) else msgtype.lower()
if self.msgtype and self.msgtype not in MATRIX_MESSAGE_TYPES:
msg = 'The msgtype specified ({}) is invalid.'.format(msgtype)
self.logger.warning(msg)
raise TypeError(msg)
if self.mode == MatrixWebhookMode.T2BOT: if self.mode == MatrixWebhookMode.T2BOT:
# t2bot configuration requires that a webhook id is specified # t2bot configuration requires that a webhook id is specified
self.access_token = validate_regex( self.access_token = validate_regex(
@ -534,7 +564,7 @@ class NotifyMatrix(NotifyBase):
# Define our payload # Define our payload
payload = { payload = {
'msgtype': 'm.text', 'msgtype': 'm.{}'.format(self.msgtype),
'body': '{title}{body}'.format( 'body': '{title}{body}'.format(
title='' if not title else '{}\r\n'.format(title), title='' if not title else '{}\r\n'.format(title),
body=body), body=body),
@ -1109,6 +1139,7 @@ class NotifyMatrix(NotifyBase):
params = { params = {
'image': 'yes' if self.include_image else 'no', 'image': 'yes' if self.include_image else 'no',
'mode': self.mode, 'mode': self.mode,
'msgtype': self.msgtype,
} }
# Extend our parameters # Extend our parameters
@ -1189,6 +1220,11 @@ class NotifyMatrix(NotifyBase):
# unquote our hostname and pass it in as the password/token # unquote our hostname and pass it in as the password/token
results['password'] = NotifyMatrix.unquote(results['host']) results['password'] = NotifyMatrix.unquote(results['host'])
# Support the message type keyword
if 'msgtype' in results['qsd'] and len(results['qsd']['msgtype']):
results['msgtype'] = \
NotifyMatrix.unquote(results['qsd']['msgtype'])
# Support the use of the token= keyword # Support the use of the token= keyword
if 'token' in results['qsd'] and len(results['qsd']['token']): if 'token' in results['qsd'] and len(results['qsd']['token']):
results['password'] = NotifyMatrix.unquote(results['qsd']['token']) results['password'] = NotifyMatrix.unquote(results['qsd']['token'])

@ -26,6 +26,7 @@
import six import six
import mock import mock
import requests import requests
import pytest
from apprise import plugins from apprise import plugins
from apprise import AppriseAsset from apprise import AppriseAsset
from json import dumps from json import dumps
@ -110,6 +111,21 @@ def test_notify_matrix_plugin_general(mock_post, mock_get):
obj.send(body="test") is True obj.send(body="test") is True
obj.send(title="title", body="test") is True obj.send(title="title", body="test") is True
# Test notice type notifications
kwargs = plugins.NotifyMatrix.parse_url(
'matrix://user:passwd@hostname/#abcd?msgtype=notice')
obj = plugins.NotifyMatrix(**kwargs)
assert isinstance(obj.url(), six.string_types) is True
assert isinstance(obj, plugins.NotifyMatrix) is True
obj.send(body="test") is True
obj.send(title="title", body="test") is True
with pytest.raises(TypeError):
# invalid message type specified
kwargs = plugins.NotifyMatrix.parse_url(
'matrix://user:passwd@hostname/#abcd?msgtype=invalid')
obj = plugins.NotifyMatrix(**kwargs)
# Force a failed login # Force a failed login
ro = response_obj.copy() ro = response_obj.copy()
del ro['access_token'] del ro['access_token']

Loading…
Cancel
Save