Use telegram message length to avoid constants

This commit is contained in:
Matthias 2022-08-05 07:29:49 +02:00
parent cffc769549
commit 7675187c37
2 changed files with 9 additions and 11 deletions

View File

@ -16,8 +16,8 @@ from typing import Any, Callable, Dict, List, Optional, Union
import arrow import arrow
from tabulate import tabulate from tabulate import tabulate
from telegram import (CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, from telegram import (MAX_MESSAGE_LENGTH, CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup,
ParseMode, ReplyKeyboardMarkup, Update) KeyboardButton, ParseMode, ReplyKeyboardMarkup, Update)
from telegram.error import BadRequest, NetworkError, TelegramError from telegram.error import BadRequest, NetworkError, TelegramError
from telegram.ext import CallbackContext, CallbackQueryHandler, CommandHandler, Updater from telegram.ext import CallbackContext, CallbackQueryHandler, CommandHandler, Updater
from telegram.utils.helpers import escape_markdown from telegram.utils.helpers import escape_markdown
@ -35,8 +35,6 @@ logger = logging.getLogger(__name__)
logger.debug('Included module rpc.telegram ...') logger.debug('Included module rpc.telegram ...')
MAX_TELEGRAM_MESSAGE_LENGTH = 4096
@dataclass @dataclass
class TimeunitMappings: class TimeunitMappings:
@ -908,7 +906,7 @@ class Telegram(RPCHandler):
total_dust_currencies += 1 total_dust_currencies += 1
# Handle overflowing message length # 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) self._send_msg(output)
output = curr_output output = curr_output
else: else:
@ -1171,7 +1169,7 @@ class Telegram(RPCHandler):
f"({trade['profit_ratio']:.2%}) " f"({trade['profit_ratio']:.2%}) "
f"({trade['count']})</code>\n") f"({trade['count']})</code>\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) self._send_msg(output, parse_mode=ParseMode.HTML)
output = stat_line output = stat_line
else: else:
@ -1206,7 +1204,7 @@ class Telegram(RPCHandler):
f"({trade['profit_ratio']:.2%}) " f"({trade['profit_ratio']:.2%}) "
f"({trade['count']})</code>\n") f"({trade['count']})</code>\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) self._send_msg(output, parse_mode=ParseMode.HTML)
output = stat_line output = stat_line
else: else:
@ -1241,7 +1239,7 @@ class Telegram(RPCHandler):
f"({trade['profit_ratio']:.2%}) " f"({trade['profit_ratio']:.2%}) "
f"({trade['count']})</code>\n") f"({trade['count']})</code>\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) self._send_msg(output, parse_mode=ParseMode.HTML)
output = stat_line output = stat_line
else: else:
@ -1276,7 +1274,7 @@ class Telegram(RPCHandler):
f"({trade['profit']:.2%}) " f"({trade['profit']:.2%}) "
f"({trade['count']})</code>\n") f"({trade['count']})</code>\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) self._send_msg(output, parse_mode=ParseMode.HTML)
output = stat_line output = stat_line
else: else:
@ -1415,7 +1413,7 @@ class Telegram(RPCHandler):
escape_markdown(logrec[2], version=2), escape_markdown(logrec[2], version=2),
escape_markdown(logrec[3], version=2), escape_markdown(logrec[3], version=2),
escape_markdown(logrec[4], 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 # Send message immediately if it would become too long
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2) self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2)
msgs = msg + '\n' msgs = msg + '\n'

View File

@ -1516,7 +1516,7 @@ def test_telegram_logs(default_conf, update, mocker) -> None:
msg_mock.reset_mock() msg_mock.reset_mock()
# Test with changed MaxMessageLength # 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 = MagicMock()
context.args = [] context.args = []
telegram._logs(update=update, context=context) telegram._logs(update=update, context=context)