2023-01-12 01:04:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-21 11:47:10 +00:00
|
|
|
# BSD 2-Clause License
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# Apprise - Push Notification Library.
|
|
|
|
# Copyright (c) 2023, Chris Caron <lead2gold@gmail.com>
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Create an incoming webhook; the website will provide you with something like:
|
|
|
|
# http://localhost:8065/hooks/yobjmukpaw3r3urc5h6i369yima
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
# |-- this is the webhook --|
|
|
|
|
#
|
|
|
|
# You can effectively turn the url above to read this:
|
|
|
|
# mmost://localhost:8065/yobjmukpaw3r3urc5h6i369yima
|
|
|
|
# - swap http with mmost
|
|
|
|
# - drop /hooks/ reference
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
import requests
|
|
|
|
from json import dumps
|
|
|
|
|
|
|
|
from .NotifyBase import NotifyBase
|
|
|
|
from ..common import NotifyImageSize
|
|
|
|
from ..common import NotifyType
|
|
|
|
from ..utils import parse_bool
|
|
|
|
from ..utils import parse_list
|
|
|
|
from ..utils import validate_regex
|
|
|
|
from ..AppriseLocale import gettext_lazy as _
|
|
|
|
|
|
|
|
# Some Reference Locations:
|
|
|
|
# - https://docs.mattermost.com/developer/webhooks-incoming.html
|
|
|
|
# - https://docs.mattermost.com/administration/config-settings.html
|
|
|
|
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
class NotifyMattermost(NotifyBase):
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
A wrapper for Mattermost Notifications
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# The default descriptive name associated with the Notification
|
2023-01-14 20:40:05 +00:00
|
|
|
service_name = 'Mattermost'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# The services URL
|
|
|
|
service_url = 'https://mattermost.com/'
|
|
|
|
|
|
|
|
# The default protocol
|
|
|
|
protocol = 'mmost'
|
|
|
|
|
|
|
|
# The default secure protocol
|
|
|
|
secure_protocol = 'mmosts'
|
|
|
|
|
|
|
|
# A URL that takes you to the setup/help of the specific protocol
|
|
|
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_mattermost'
|
|
|
|
|
|
|
|
# The default Mattermost port
|
|
|
|
default_port = 8065
|
|
|
|
|
|
|
|
# Allows the user to specify the NotifyImageSize object
|
|
|
|
image_size = NotifyImageSize.XY_72
|
|
|
|
|
|
|
|
# The maximum allowable characters allowed in the body per message
|
|
|
|
body_maxlen = 4000
|
|
|
|
|
|
|
|
# Mattermost does not have a title
|
|
|
|
title_maxlen = 0
|
|
|
|
|
|
|
|
# Define object templates
|
|
|
|
templates = (
|
2023-01-14 20:40:05 +00:00
|
|
|
'{schema}://{host}/{token}',
|
2023-10-21 11:47:10 +00:00
|
|
|
'{schema}://{host}:{port}/{token}',
|
|
|
|
'{schema}://{host}/{fullpath}/{token}',
|
|
|
|
'{schema}://{host}:{port}/{fullpath}/{token}',
|
2023-01-14 20:40:05 +00:00
|
|
|
'{schema}://{botname}@{host}/{token}',
|
|
|
|
'{schema}://{botname}@{host}:{port}/{token}',
|
|
|
|
'{schema}://{botname}@{host}/{fullpath}/{token}',
|
|
|
|
'{schema}://{botname}@{host}:{port}/{fullpath}/{token}',
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Define our template tokens
|
|
|
|
template_tokens = dict(NotifyBase.template_tokens, **{
|
|
|
|
'host': {
|
|
|
|
'name': _('Hostname'),
|
|
|
|
'type': 'string',
|
|
|
|
'required': True,
|
|
|
|
},
|
2023-01-14 20:40:05 +00:00
|
|
|
'token': {
|
|
|
|
'name': _('Webhook Token'),
|
2023-01-12 01:04:47 +00:00
|
|
|
'type': 'string',
|
|
|
|
'private': True,
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
'fullpath': {
|
|
|
|
'name': _('Path'),
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
'botname': {
|
|
|
|
'name': _('Bot Name'),
|
|
|
|
'type': 'string',
|
|
|
|
'map_to': 'user',
|
|
|
|
},
|
|
|
|
'port': {
|
|
|
|
'name': _('Port'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 1,
|
|
|
|
'max': 65535,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
# Define our template arguments
|
|
|
|
template_args = dict(NotifyBase.template_args, **{
|
|
|
|
'channels': {
|
|
|
|
'name': _('Channels'),
|
|
|
|
'type': 'list:string',
|
|
|
|
},
|
|
|
|
'image': {
|
|
|
|
'name': _('Include Image'),
|
|
|
|
'type': 'bool',
|
|
|
|
'default': True,
|
|
|
|
'map_to': 'include_image',
|
|
|
|
},
|
|
|
|
'to': {
|
|
|
|
'alias_of': 'channels',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
def __init__(self, token, fullpath=None, channels=None,
|
2023-01-12 01:04:47 +00:00
|
|
|
include_image=False, **kwargs):
|
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
Initialize Mattermost Object
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
super().__init__(**kwargs)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
if self.secure:
|
|
|
|
self.schema = 'https'
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.schema = 'http'
|
|
|
|
|
|
|
|
# our full path
|
|
|
|
self.fullpath = '' if not isinstance(
|
2023-01-14 20:40:05 +00:00
|
|
|
fullpath, str) else fullpath.strip()
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Authorization Token (associated with project)
|
2023-01-14 20:40:05 +00:00
|
|
|
self.token = validate_regex(token)
|
|
|
|
if not self.token:
|
|
|
|
msg = 'An invalid Mattermost Authorization Token ' \
|
|
|
|
'({}) was specified.'.format(token)
|
2023-01-12 01:04:47 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Optional Channels (strip off any channel prefix entries if present)
|
|
|
|
self.channels = [x.lstrip('#') for x in parse_list(channels)]
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
if not self.port:
|
|
|
|
self.port = self.default_port
|
|
|
|
|
|
|
|
# Place a thumbnail image inline with the message body
|
|
|
|
self.include_image = include_image
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
Perform Mattermost Notification
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Create a copy of our channels, otherwise place a dummy entry
|
|
|
|
channels = list(self.channels) if self.channels else [None, ]
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'User-Agent': self.app_id,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
|
|
|
|
# prepare JSON Object
|
|
|
|
payload = {
|
|
|
|
'text': body,
|
|
|
|
'icon_url': None,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Acquire our image url if configured to do so
|
|
|
|
image_url = None if not self.include_image \
|
|
|
|
else self.image_url(notify_type)
|
|
|
|
|
|
|
|
if image_url:
|
|
|
|
# Set our image configuration if told to do so
|
|
|
|
payload['icon_url'] = image_url
|
|
|
|
|
|
|
|
# Set our user
|
|
|
|
payload['username'] = self.user if self.user else self.app_id
|
|
|
|
|
|
|
|
# For error tracking
|
|
|
|
has_error = False
|
|
|
|
|
|
|
|
while len(channels):
|
|
|
|
# Pop a channel off of the list
|
|
|
|
channel = channels.pop(0)
|
|
|
|
|
|
|
|
if channel:
|
|
|
|
payload['channel'] = channel
|
|
|
|
|
|
|
|
url = '{}://{}:{}{}/hooks/{}'.format(
|
|
|
|
self.schema, self.host, self.port, self.fullpath,
|
2023-01-14 20:40:05 +00:00
|
|
|
self.token)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
self.logger.debug('Mattermost POST URL: %s (cert_verify=%r)' % (
|
2023-01-12 01:04:47 +00:00
|
|
|
url, self.verify_certificate,
|
|
|
|
))
|
2023-01-14 20:40:05 +00:00
|
|
|
self.logger.debug('Mattermost Payload: %s' % str(payload))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = requests.post(
|
|
|
|
url,
|
|
|
|
data=dumps(payload),
|
|
|
|
headers=headers,
|
|
|
|
verify=self.verify_certificate,
|
2023-01-14 20:40:05 +00:00
|
|
|
timeout=self.request_timeout,
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code != requests.codes.ok:
|
|
|
|
# We had a problem
|
|
|
|
status_str = \
|
2023-01-14 20:40:05 +00:00
|
|
|
NotifyMattermost.http_response_code_lookup(
|
2023-01-12 01:04:47 +00:00
|
|
|
r.status_code)
|
|
|
|
|
|
|
|
self.logger.warning(
|
2023-01-14 20:40:05 +00:00
|
|
|
'Failed to send Mattermost notification{}: '
|
2023-01-12 01:04:47 +00:00
|
|
|
'{}{}error={}.'.format(
|
|
|
|
'' if not channel
|
|
|
|
else ' to channel {}'.format(channel),
|
|
|
|
status_str,
|
|
|
|
', ' if status_str else '',
|
|
|
|
r.status_code))
|
|
|
|
|
|
|
|
self.logger.debug(
|
|
|
|
'Response Details:\r\n{}'.format(r.content))
|
|
|
|
|
|
|
|
# Flag our error
|
|
|
|
has_error = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.logger.info(
|
2023-01-14 20:40:05 +00:00
|
|
|
'Sent Mattermost notification{}.'.format(
|
2023-01-12 01:04:47 +00:00
|
|
|
'' if not channel
|
|
|
|
else ' to channel {}'.format(channel)))
|
|
|
|
|
|
|
|
except requests.RequestException as e:
|
|
|
|
self.logger.warning(
|
2023-01-14 20:40:05 +00:00
|
|
|
'A Connection error occurred sending Mattermost '
|
2023-01-12 01:04:47 +00:00
|
|
|
'notification{}.'.format(
|
|
|
|
'' if not channel
|
|
|
|
else ' to channel {}'.format(channel)))
|
|
|
|
self.logger.debug('Socket Exception: %s' % str(e))
|
|
|
|
|
|
|
|
# Flag our error
|
|
|
|
has_error = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Return our overall status
|
|
|
|
return not has_error
|
|
|
|
|
|
|
|
def url(self, privacy=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Define any URL parameters
|
|
|
|
params = {
|
2023-01-12 01:04:47 +00:00
|
|
|
'image': 'yes' if self.include_image else 'no',
|
|
|
|
}
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Extend our parameters
|
|
|
|
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
if self.channels:
|
|
|
|
# historically the value only accepted one channel and is
|
|
|
|
# therefore identified as 'channel'. Channels have always been
|
|
|
|
# optional, so that is why this setting is nested in an if block
|
2023-01-14 20:40:05 +00:00
|
|
|
params['channel'] = ','.join(
|
|
|
|
[NotifyMattermost.quote(x, safe='') for x in self.channels])
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
default_port = 443 if self.secure else self.default_port
|
|
|
|
default_schema = self.secure_protocol if self.secure else self.protocol
|
|
|
|
|
|
|
|
# Determine if there is a botname present
|
|
|
|
botname = ''
|
|
|
|
if self.user:
|
|
|
|
botname = '{botname}@'.format(
|
2023-01-14 20:40:05 +00:00
|
|
|
botname=NotifyMattermost.quote(self.user, safe=''),
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return \
|
2023-01-14 20:40:05 +00:00
|
|
|
'{schema}://{botname}{hostname}{port}{fullpath}{token}' \
|
|
|
|
'/?{params}'.format(
|
2023-01-12 01:04:47 +00:00
|
|
|
schema=default_schema,
|
|
|
|
botname=botname,
|
2023-01-14 20:40:05 +00:00
|
|
|
# never encode hostname since we're expecting it to be a valid
|
|
|
|
# one
|
|
|
|
hostname=self.host,
|
2023-01-12 01:04:47 +00:00
|
|
|
port='' if not self.port or self.port == default_port
|
|
|
|
else ':{}'.format(self.port),
|
|
|
|
fullpath='/' if not self.fullpath else '{}/'.format(
|
2023-01-14 20:40:05 +00:00
|
|
|
NotifyMattermost.quote(self.fullpath, safe='/')),
|
|
|
|
token=self.pprint(self.token, privacy, safe=''),
|
|
|
|
params=NotifyMattermost.urlencode(params),
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
Parses the URL and returns enough arguments that can allow
|
2023-01-14 20:40:05 +00:00
|
|
|
us to re-instantiate this object.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
results = NotifyBase.parse_url(url)
|
|
|
|
if not results:
|
|
|
|
# We're done early as we couldn't load the results
|
|
|
|
return results
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Acquire our tokens; the last one will always be our token
|
2023-01-12 01:04:47 +00:00
|
|
|
# all entries before it will be our path
|
2023-01-14 20:40:05 +00:00
|
|
|
tokens = NotifyMattermost.split_path(results['fullpath'])
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
results['token'] = None if not tokens else tokens.pop()
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Store our path
|
|
|
|
results['fullpath'] = '' if not tokens \
|
|
|
|
else '/{}'.format('/'.join(tokens))
|
|
|
|
|
|
|
|
# Define our optional list of channels to notify
|
|
|
|
results['channels'] = list()
|
|
|
|
|
|
|
|
# Support both 'to' (for yaml configuration) and channel=
|
|
|
|
if 'to' in results['qsd'] and len(results['qsd']['to']):
|
|
|
|
# Allow the user to specify the channel to post to
|
|
|
|
results['channels'].append(
|
2023-01-14 20:40:05 +00:00
|
|
|
NotifyMattermost.parse_list(results['qsd']['to']))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
if 'channel' in results['qsd'] and len(results['qsd']['channel']):
|
|
|
|
# Allow the user to specify the channel to post to
|
|
|
|
results['channels'].append(
|
2023-01-14 20:40:05 +00:00
|
|
|
NotifyMattermost.parse_list(results['qsd']['channel']))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Image manipulation
|
|
|
|
results['include_image'] = \
|
|
|
|
parse_bool(results['qsd'].get('image', False))
|
|
|
|
|
|
|
|
return results
|