Add JSON-encoded webhooks

This commit is contained in:
Xanders 2021-02-26 17:46:23 +03:00 committed by crypto_rahino
parent 96a3fa6d26
commit 27b7e1cac3

View File

@ -28,6 +28,11 @@ class Webhook(RPCHandler):
self._url = self._config['webhook']['url']
self._format = self._config['webhook'].get('format', 'form')
if self._format != 'form' and self._format != 'json':
raise NotImplementedError('Unknown webhook format `{}`, possible values are `form` (default) and `json`'.format(self._format))
def cleanup(self) -> None:
"""
Cleanup pending module resources.
@ -66,7 +71,14 @@ class Webhook(RPCHandler):
def _send_msg(self, payload: dict) -> None:
"""do the actual call to the webhook"""
if self._format == 'form':
kwargs = {'data': payload}
elif self._format == 'json':
kwargs = {'json': payload}
else:
raise NotImplementedError('Unknown format: {}'.format(self._format))
try:
post(self._url, data=payload)
post(self._url, **kwargs)
except RequestException as exc:
logger.warning("Could not call webhook url. Exception: %s", exc)