From 36e5c18fa600a3dbf3c58f6e2e7ee149a5b71ede Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 5 Sep 2022 20:54:03 +0200 Subject: [PATCH] Don't raise exception when a message is not implemented in telegram --- freqtrade/rpc/telegram.py | 12 +++++++----- tests/rpc/test_rpc_telegram.py | 10 +++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 8c988d570..4a759f6ec 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -6,6 +6,7 @@ This module manage Telegram communication import json import logging import re +from copy import deepcopy from dataclasses import dataclass from datetime import date, datetime, timedelta from functools import partial @@ -374,7 +375,7 @@ class Telegram(RPCHandler): message += f"\n*Duration:* `{msg['duration']} ({msg['duration_min']:.1f} min)`" return message - def compose_message(self, msg: Dict[str, Any], msg_type: RPCMessageType) -> str: + def compose_message(self, msg: Dict[str, Any], msg_type: RPCMessageType) -> Optional[str]: if msg_type in [RPCMessageType.ENTRY, RPCMessageType.ENTRY_FILL]: message = self._format_entry_msg(msg) @@ -411,7 +412,8 @@ class Telegram(RPCHandler): elif msg_type == RPCMessageType.STRATEGY_MSG: message = f"{msg['msg']}" else: - raise NotImplementedError(f"Unknown message type: {msg_type}") + logger.debug("Unknown message type: %s", msg_type) + return None return message def send_msg(self, msg: Dict[str, Any]) -> None: @@ -438,9 +440,9 @@ class Telegram(RPCHandler): # Notification disabled return - message = self.compose_message(msg, msg_type) - - self._send_msg(message, disable_notification=(noti == 'silent')) + message = self.compose_message(deepcopy(msg), msg_type) + if message: + self._send_msg(message, disable_notification=(noti == 'silent')) def _get_sell_emoji(self, msg): """ diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index cde7025a7..f2e490dff 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -2138,11 +2138,11 @@ def test_send_msg_strategy_msg_notification(default_conf, mocker) -> None: def test_send_msg_unknown_type(default_conf, mocker) -> None: - telegram, _, _ = get_telegram_testobject(mocker, default_conf) - with pytest.raises(NotImplementedError, match=r'Unknown message type: None'): - telegram.send_msg({ - 'type': None, - }) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) + telegram.send_msg({ + 'type': None, + }) + msg_mock.call_count == 0 @pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [