Improve telegram message formatting

This commit is contained in:
Matthias 2020-08-15 08:31:36 +02:00
parent f5863a1c6f
commit 1ffa3d1ae0
2 changed files with 13 additions and 18 deletions

View File

@ -639,19 +639,12 @@ class RPC:
buffer = bufferHandler.buffer[-limit:]
else:
buffer = bufferHandler.buffer
records = [[datetime.fromtimestamp(r.created), r.created, r.name, r.levelname, r.message]
records = [[datetime.fromtimestamp(r.created).strftime("%Y-%m-%d %H:%M:%S"),
r.created, r.name, r.levelname, r.message]
for r in buffer]
return {'log_count': len(records), 'logs': records}
def _rpc_get_logs_as_string(self, limit: Optional[int]) -> List[str]:
"""Returns the last X logs as formatted string (Using the default log format)"""
if limit:
buffer = bufferHandler.buffer[-limit:]
else:
buffer = bufferHandler.buffer
return [bufferHandler.format(r) for r in buffer]
def _rpc_edge(self) -> List[Dict[str, Any]]:
""" Returns information related to Edge """
if not self._freqtrade.edge:

View File

@ -649,20 +649,22 @@ class Telegram(RPC):
limit = int(context.args[0])
except (TypeError, ValueError, IndexError):
limit = 10
logs = self._rpc_get_logs_as_string(limit)
msg = ''
message_container = "<pre>{}</pre>"
logs = self._rpc_get_logs(limit)['logs']
msgs = ''
msg_template = "*{}* {}: {} - `{}`"
for logrec in logs:
if len(msg + logrec) + 10 >= MAX_TELEGRAM_MESSAGE_LENGTH:
msg = msg_template.format(logrec[0], logrec[2], logrec[3], logrec[4])
if len(msgs + msg) + 10 >= MAX_TELEGRAM_MESSAGE_LENGTH:
# Send message immediately if it would become too long
self._send_msg(message_container.format(msg), parse_mode=ParseMode.HTML)
msg = logrec + '\n'
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN)
msgs = msg + '\n'
else:
# Append message to messages to send
msg += logrec + '\n'
msgs += msg + '\n'
if msg:
self._send_msg(message_container.format(msg), parse_mode=ParseMode.HTML)
if msgs:
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN)
except RPCException as e:
self._send_msg(str(e))