Refactor webhook method

This commit is contained in:
Matthias 2022-10-07 20:52:14 +02:00
parent 1aedf08ba5
commit df5ae66252
2 changed files with 38 additions and 40 deletions

View File

@ -3,7 +3,7 @@ This module manages webhook communication
"""
import logging
import time
from typing import Any, Dict
from typing import Any, Dict, Optional
from requests import RequestException, post
@ -41,9 +41,7 @@ class Webhook(RPCHandler):
"""
pass
def send_msg(self, msg: Dict[str, Any]) -> None:
""" Send a message to telegram channel """
try:
def _get_value_dict(self, msg: Dict[str, Any]) -> Optional[Dict[str, Any]]:
whconfig = self._config['webhook']
# Deprecated 2022.10 - only keep generic method.
if msg['type'] in [RPCMessageType.ENTRY]:
@ -72,7 +70,14 @@ class Webhook(RPCHandler):
RPCMessageType.ANALYZED_DF,
RPCMessageType.STRATEGY_MSG):
# Don't fail for non-implemented types
return
return None
return valuedict
def send_msg(self, msg: Dict[str, Any]) -> None:
""" Send a message to telegram channel """
try:
valuedict = self._get_value_dict(msg)
if not valuedict:
logger.info("Message type '%s' not configured for webhooks", msg['type'])

View File

@ -336,20 +336,13 @@ def test_exception_send_msg(default_conf, mocker, caplog):
caplog)
default_conf["webhook"] = get_webhook_dict()
default_conf["webhook"]["webhookentry"]["value1"] = "{DEADBEEF:8f}"
default_conf["webhook"]["strategy_msg"] = {"value1": "{DEADBEEF:8f}"}
msg_mock = MagicMock()
mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock)
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
msg = {
'type': RPCMessageType.ENTRY,
'exchange': 'Binance',
'pair': 'ETH/BTC',
'limit': 0.005,
'order_type': 'limit',
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
'type': RPCMessageType.STRATEGY_MSG,
'msg': 'hello world',
}
webhook.send_msg(msg)
assert log_has("Problem calling Webhook. Please check your webhook configuration. "