From 7675187c377865abe6d4fc3f58543e294ae7cb53 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 5 Aug 2022 07:29:49 +0200 Subject: [PATCH] Use telegram message length to avoid constants --- freqtrade/rpc/telegram.py | 18 ++++++++---------- tests/rpc/test_rpc_telegram.py | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 66192fb16..84b051255 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -16,8 +16,8 @@ from typing import Any, Callable, Dict, List, Optional, Union import arrow from tabulate import tabulate -from telegram import (CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, - ParseMode, ReplyKeyboardMarkup, Update) +from telegram import (MAX_MESSAGE_LENGTH, CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup, + KeyboardButton, ParseMode, ReplyKeyboardMarkup, Update) from telegram.error import BadRequest, NetworkError, TelegramError from telegram.ext import CallbackContext, CallbackQueryHandler, CommandHandler, Updater from telegram.utils.helpers import escape_markdown @@ -35,8 +35,6 @@ logger = logging.getLogger(__name__) logger.debug('Included module rpc.telegram ...') -MAX_TELEGRAM_MESSAGE_LENGTH = 4096 - @dataclass class TimeunitMappings: @@ -908,7 +906,7 @@ class Telegram(RPCHandler): total_dust_currencies += 1 # Handle overflowing message length - if len(output + curr_output) >= MAX_TELEGRAM_MESSAGE_LENGTH: + if len(output + curr_output) >= MAX_MESSAGE_LENGTH: self._send_msg(output) output = curr_output else: @@ -1171,7 +1169,7 @@ class Telegram(RPCHandler): f"({trade['profit_ratio']:.2%}) " f"({trade['count']})\n") - if len(output + stat_line) >= MAX_TELEGRAM_MESSAGE_LENGTH: + if len(output + stat_line) >= MAX_MESSAGE_LENGTH: self._send_msg(output, parse_mode=ParseMode.HTML) output = stat_line else: @@ -1206,7 +1204,7 @@ class Telegram(RPCHandler): f"({trade['profit_ratio']:.2%}) " f"({trade['count']})\n") - if len(output + stat_line) >= MAX_TELEGRAM_MESSAGE_LENGTH: + if len(output + stat_line) >= MAX_MESSAGE_LENGTH: self._send_msg(output, parse_mode=ParseMode.HTML) output = stat_line else: @@ -1241,7 +1239,7 @@ class Telegram(RPCHandler): f"({trade['profit_ratio']:.2%}) " f"({trade['count']})\n") - if len(output + stat_line) >= MAX_TELEGRAM_MESSAGE_LENGTH: + if len(output + stat_line) >= MAX_MESSAGE_LENGTH: self._send_msg(output, parse_mode=ParseMode.HTML) output = stat_line else: @@ -1276,7 +1274,7 @@ class Telegram(RPCHandler): f"({trade['profit']:.2%}) " f"({trade['count']})\n") - if len(output + stat_line) >= MAX_TELEGRAM_MESSAGE_LENGTH: + if len(output + stat_line) >= MAX_MESSAGE_LENGTH: self._send_msg(output, parse_mode=ParseMode.HTML) output = stat_line else: @@ -1415,7 +1413,7 @@ class Telegram(RPCHandler): escape_markdown(logrec[2], version=2), escape_markdown(logrec[3], version=2), escape_markdown(logrec[4], version=2)) - if len(msgs + msg) + 10 >= MAX_TELEGRAM_MESSAGE_LENGTH: + if len(msgs + msg) + 10 >= MAX_MESSAGE_LENGTH: # Send message immediately if it would become too long self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2) msgs = msg + '\n' diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index 9508a6b42..164ed0bc6 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -1516,7 +1516,7 @@ def test_telegram_logs(default_conf, update, mocker) -> None: msg_mock.reset_mock() # Test with changed MaxMessageLength - mocker.patch('freqtrade.rpc.telegram.MAX_TELEGRAM_MESSAGE_LENGTH', 200) + mocker.patch('freqtrade.rpc.telegram.MAX_MESSAGE_LENGTH', 200) context = MagicMock() context.args = [] telegram._logs(update=update, context=context)