mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-16 02:02:58 -07:00
Add json support for MQTT notifications
This commit is contained in:
parent
39afabf1b7
commit
b6cb946ae2
2 changed files with 30 additions and 12 deletions
|
@ -402,7 +402,8 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data
|
|||
body=body_string,
|
||||
notify_action=notify_action,
|
||||
parameters=parameters,
|
||||
agent_id=notifier_config['agent_id'])
|
||||
agent_id=notifier_config['agent_id'],
|
||||
as_json=notifier_config['config'].get('as_json', False))
|
||||
|
||||
# Set the notification state in the db
|
||||
notification_id = set_notify_state(session=stream_data or timeline_data,
|
||||
|
@ -1259,7 +1260,7 @@ def build_server_notify_params(notify_action=None, **kwargs):
|
|||
return available_params
|
||||
|
||||
|
||||
def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None, test=False):
|
||||
def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None, test=False, as_json=False):
|
||||
# Default subject and body text
|
||||
if agent_id == 15:
|
||||
default_subject = default_body = ''
|
||||
|
@ -1320,21 +1321,22 @@ def build_notify_text(subject='', body='', notify_action=None, parameters=None,
|
|||
logger.exception("Tautulli NotificationHandler :: Unable to parse custom script arguments: %s. Using fallback." % e)
|
||||
script_args = []
|
||||
|
||||
elif agent_id == 25:
|
||||
elif agent_id == 25 or as_json:
|
||||
agent = 'MQTT' if agent_id == 23 else 'webhook'
|
||||
if subject:
|
||||
try:
|
||||
subject = json.loads(subject)
|
||||
except ValueError as e:
|
||||
logger.error("Tautulli NotificationHandler :: Unable to parse custom webhook json header data: %s. Using fallback." % e)
|
||||
logger.error("Tautulli NotificationHandler :: Unable to parse custom %s json header data: %s. Using fallback." % (agent, e))
|
||||
subject = ''
|
||||
if subject:
|
||||
try:
|
||||
subject = json.dumps(helpers.traverse_map(subject, str_formatter))
|
||||
except LookupError as e:
|
||||
logger.error("Tautulli NotificationHandler :: Unable to parse parameter %s in webhook header data. Using fallback." % e)
|
||||
logger.error("Tautulli NotificationHandler :: Unable to parse parameter %s in %s header data. Using fallback." % (e, agent))
|
||||
subject = ''
|
||||
except Exception as e:
|
||||
logger.exception("Tautulli NotificationHandler :: Unable to parse custom webhook header data: %s. Using fallback." % e)
|
||||
logger.exception("Tautulli NotificationHandler :: Unable to parse custom %s header data: %s. Using fallback." % (agent, e))
|
||||
subject = ''
|
||||
|
||||
if body:
|
||||
|
|
|
@ -2367,17 +2367,25 @@ class MQTT(Notifier):
|
|||
'topic': '',
|
||||
'qos': 1,
|
||||
'retain': 0,
|
||||
'keep_alive': 60
|
||||
'keep_alive': 60,
|
||||
'as_json': 0
|
||||
}
|
||||
|
||||
def agent_notify(self, subject='', body='', action='', **kwargs):
|
||||
if not self.config['topic']:
|
||||
topic = self.config['topic']
|
||||
if not topic:
|
||||
logger.error("Tautulli Notifiers :: MQTT topic not specified.")
|
||||
return
|
||||
|
||||
topic = topic.format(**kwargs.get('parameters', {}))
|
||||
|
||||
if self.config['as_json']:
|
||||
subject = json.loads(subject)
|
||||
body = json.loads(body)
|
||||
|
||||
data = {'subject': subject,
|
||||
'body': body,
|
||||
'topic': self.config['topic']}
|
||||
'topic': topic}
|
||||
|
||||
auth = {}
|
||||
if self.config['username']:
|
||||
|
@ -2390,7 +2398,7 @@ class MQTT(Notifier):
|
|||
logger.info("Tautulli Notifiers :: Sending {name} notification...".format(name=self.NAME))
|
||||
|
||||
paho.mqtt.publish.single(
|
||||
self.config['topic'], payload=json.dumps(data), qos=self.config['qos'], retain=bool(self.config['retain']),
|
||||
topic, payload=json.dumps(data), qos=self.config['qos'], retain=bool(self.config['retain']),
|
||||
hostname=self.config['broker'], port=self.config['port'], client_id=self.config['clientid'],
|
||||
keepalive=self.config['keep_alive'], auth=auth or None, protocol=protocol
|
||||
)
|
||||
|
@ -2443,7 +2451,9 @@ class MQTT(Notifier):
|
|||
{'label': 'Topic',
|
||||
'value': self.config['topic'],
|
||||
'name': 'mqtt_topic',
|
||||
'description': 'The topic to publish notifications to.',
|
||||
'description': 'The topic to publish notifications to. You can include'
|
||||
' <a href="#notify-text-sub-modal" data-toggle="modal">notification parameters</a>'
|
||||
' to be substituted.',
|
||||
'input_type': 'text'
|
||||
},
|
||||
{'label': 'Quality of Service',
|
||||
|
@ -2467,7 +2477,13 @@ class MQTT(Notifier):
|
|||
'name': 'mqtt_keep_alive',
|
||||
'description': 'Maximum period in seconds before timing out the connection with the broker.',
|
||||
'input_type': 'number'
|
||||
}
|
||||
},
|
||||
{'label': 'Send Message as JSON',
|
||||
'value': self.config['as_json'],
|
||||
'name': 'mqtt_as_json',
|
||||
'description': 'Parse and send the subject and body as JSON instead of as a raw string.',
|
||||
'input_type': 'checkbox'
|
||||
},
|
||||
]
|
||||
|
||||
return config_option
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue