2018-07-05 19:32:53 +00:00
|
|
|
"""
|
|
|
|
This module manages webhook communication
|
|
|
|
"""
|
|
|
|
import logging
|
2021-11-28 00:42:57 +00:00
|
|
|
import time
|
2020-09-28 17:39:41 +00:00
|
|
|
from typing import Any, Dict
|
2018-07-05 19:32:53 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
from requests import RequestException, post
|
2018-07-05 19:32:53 +00:00
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
from freqtrade.constants import Config
|
2021-06-09 17:51:44 +00:00
|
|
|
from freqtrade.enums import RPCMessageType
|
|
|
|
from freqtrade.rpc import RPC, RPCHandler
|
2018-07-05 19:32:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
logger.debug('Included module rpc.webhook ...')
|
|
|
|
|
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
class Webhook(RPCHandler):
|
2018-07-05 19:32:53 +00:00
|
|
|
""" This class handles all webhook communication """
|
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
def __init__(self, rpc: RPC, config: Config) -> None:
|
2018-07-05 19:32:53 +00:00
|
|
|
"""
|
2020-12-24 08:01:53 +00:00
|
|
|
Init the Webhook class, and init the super class RPCHandler
|
|
|
|
:param rpc: instance of RPC Helper class
|
|
|
|
:param config: Configuration object
|
2018-07-05 19:32:53 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
2020-12-24 08:01:53 +00:00
|
|
|
super().__init__(rpc, config)
|
2018-07-05 19:32:53 +00:00
|
|
|
|
2018-07-14 11:29:34 +00:00
|
|
|
self._url = self._config['webhook']['url']
|
2021-02-26 14:46:23 +00:00
|
|
|
self._format = self._config['webhook'].get('format', 'form')
|
2021-11-28 00:42:57 +00:00
|
|
|
self._retries = self._config['webhook'].get('retries', 0)
|
|
|
|
self._retry_delay = self._config['webhook'].get('retry_delay', 0.1)
|
|
|
|
|
2018-07-05 19:32:53 +00:00
|
|
|
def cleanup(self) -> None:
|
|
|
|
"""
|
|
|
|
Cleanup pending module resources.
|
|
|
|
This will do nothing for webhooks, they will simply not be called anymore
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def send_msg(self, msg: Dict[str, Any]) -> None:
|
|
|
|
""" Send a message to telegram channel """
|
|
|
|
try:
|
2022-04-04 17:05:36 +00:00
|
|
|
whconfig = self._config['webhook']
|
2022-10-07 18:44:47 +00:00
|
|
|
# Deprecated 2022.10 - only keep generic method.
|
2022-04-04 17:29:15 +00:00
|
|
|
if msg['type'] in [RPCMessageType.ENTRY]:
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookentry')
|
2022-04-04 17:29:15 +00:00
|
|
|
elif msg['type'] in [RPCMessageType.ENTRY_CANCEL]:
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookentrycancel')
|
2022-04-04 17:29:15 +00:00
|
|
|
elif msg['type'] in [RPCMessageType.ENTRY_FILL]:
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookentryfill')
|
2022-04-04 17:10:44 +00:00
|
|
|
elif msg['type'] == RPCMessageType.EXIT:
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookexit')
|
2022-04-04 17:08:31 +00:00
|
|
|
elif msg['type'] == RPCMessageType.EXIT_FILL:
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookexitfill')
|
2022-04-04 17:07:20 +00:00
|
|
|
elif msg['type'] == RPCMessageType.EXIT_CANCEL:
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookexitcancel')
|
2021-04-20 04:41:58 +00:00
|
|
|
elif msg['type'] in (RPCMessageType.STATUS,
|
|
|
|
RPCMessageType.STARTUP,
|
|
|
|
RPCMessageType.WARNING):
|
2022-05-17 22:11:10 +00:00
|
|
|
valuedict = whconfig.get('webhookstatus')
|
2022-10-07 18:44:47 +00:00
|
|
|
elif msg['type'].value in whconfig:
|
|
|
|
# Allow all types ...
|
|
|
|
valuedict = whconfig.get(msg['type'].value)
|
2022-10-01 07:35:21 +00:00
|
|
|
elif msg['type'] in (
|
|
|
|
RPCMessageType.PROTECTION_TRIGGER,
|
|
|
|
RPCMessageType.PROTECTION_TRIGGER_GLOBAL,
|
|
|
|
RPCMessageType.WHITELIST,
|
|
|
|
RPCMessageType.ANALYZED_DF,
|
|
|
|
RPCMessageType.STRATEGY_MSG):
|
|
|
|
# Don't fail for non-implemented types
|
|
|
|
return
|
2022-10-07 18:44:47 +00:00
|
|
|
|
2018-07-05 19:32:53 +00:00
|
|
|
if not valuedict:
|
2020-09-19 18:04:12 +00:00
|
|
|
logger.info("Message type '%s' not configured for webhooks", msg['type'])
|
2018-07-05 19:32:53 +00:00
|
|
|
return
|
|
|
|
|
2018-07-12 19:54:31 +00:00
|
|
|
payload = {key: value.format(**msg) for (key, value) in valuedict.items()}
|
2018-07-05 19:32:53 +00:00
|
|
|
self._send_msg(payload)
|
|
|
|
except KeyError as exc:
|
|
|
|
logger.exception("Problem calling Webhook. Please check your webhook configuration. "
|
|
|
|
"Exception: %s", exc)
|
|
|
|
|
|
|
|
def _send_msg(self, payload: dict) -> None:
|
2018-07-14 11:29:34 +00:00
|
|
|
"""do the actual call to the webhook"""
|
2018-07-05 19:32:53 +00:00
|
|
|
|
2021-11-28 00:42:57 +00:00
|
|
|
success = False
|
|
|
|
attempts = 0
|
|
|
|
while not success and attempts <= self._retries:
|
|
|
|
if attempts:
|
2021-11-29 18:54:54 +00:00
|
|
|
if self._retry_delay:
|
|
|
|
time.sleep(self._retry_delay)
|
2021-11-28 00:42:57 +00:00
|
|
|
logger.info("Retrying webhook...")
|
|
|
|
|
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self._format == 'form':
|
|
|
|
response = post(self._url, data=payload)
|
|
|
|
elif self._format == 'json':
|
|
|
|
response = post(self._url, json=payload)
|
|
|
|
elif self._format == 'raw':
|
2021-11-29 18:54:54 +00:00
|
|
|
response = post(self._url, data=payload['data'],
|
|
|
|
headers={'Content-Type': 'text/plain'})
|
2021-11-28 00:42:57 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError('Unknown format: {}'.format(self._format))
|
2021-11-29 18:54:54 +00:00
|
|
|
|
2021-11-28 23:30:41 +00:00
|
|
|
# Throw a RequestException if the post was not successful
|
2021-11-28 00:42:57 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
success = True
|
|
|
|
|
|
|
|
except RequestException as exc:
|
|
|
|
logger.warning("Could not call webhook url. Exception: %s", exc)
|