Add Refresh / Reload Button on rpc/Telegram
This commit is contained in:
parent
caa3e1a7fa
commit
69d62ef383
@ -11,9 +11,9 @@ from typing import Any, Callable, Dict, List, Union
|
|||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
from telegram import KeyboardButton, ParseMode, ReplyKeyboardMarkup, Update
|
from telegram import KeyboardButton, ParseMode, ReplyKeyboardMarkup, Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||||
from telegram.error import NetworkError, TelegramError
|
from telegram.error import NetworkError, TelegramError
|
||||||
from telegram.ext import CallbackContext, CommandHandler, Updater
|
from telegram.ext import CallbackContext, CommandHandler, Updater, CallbackQueryHandler
|
||||||
from telegram.utils.helpers import escape_markdown
|
from telegram.utils.helpers import escape_markdown
|
||||||
|
|
||||||
from freqtrade.__init__ import __version__
|
from freqtrade.__init__ import __version__
|
||||||
@ -40,9 +40,13 @@ def authorized_only(command_handler: Callable[..., None]) -> Callable[..., Any]:
|
|||||||
update = kwargs.get('update') or args[0]
|
update = kwargs.get('update') or args[0]
|
||||||
|
|
||||||
# Reject unauthorized messages
|
# Reject unauthorized messages
|
||||||
chat_id = int(self._config['telegram']['chat_id'])
|
if update.callback_query:
|
||||||
|
cchat_id = int(update.callback_query.message.chat.id)
|
||||||
|
else:
|
||||||
|
cchat_id = int(update.message.chat_id)
|
||||||
|
|
||||||
if int(update.message.chat_id) != chat_id:
|
chat_id = int(self._config['telegram']['chat_id'])
|
||||||
|
if cchat_id != chat_id:
|
||||||
logger.info(
|
logger.info(
|
||||||
'Rejected unauthorized message from: %s',
|
'Rejected unauthorized message from: %s',
|
||||||
update.message.chat_id
|
update.message.chat_id
|
||||||
@ -150,10 +154,22 @@ class Telegram(RPCHandler):
|
|||||||
CommandHandler('logs', self._logs),
|
CommandHandler('logs', self._logs),
|
||||||
CommandHandler('edge', self._edge),
|
CommandHandler('edge', self._edge),
|
||||||
CommandHandler('help', self._help),
|
CommandHandler('help', self._help),
|
||||||
CommandHandler('version', self._version),
|
CommandHandler('version', self._version)
|
||||||
|
]
|
||||||
|
callbacks = [
|
||||||
|
CallbackQueryHandler(self._status_table, pattern='update_status_table'),
|
||||||
|
CallbackQueryHandler(self._daily, pattern='update_daily'),
|
||||||
|
CallbackQueryHandler(self._profit, pattern='update_profit'),
|
||||||
|
CallbackQueryHandler(self._profit, pattern='update_balance'),
|
||||||
|
CallbackQueryHandler(self._profit, pattern='update_performance'),
|
||||||
|
CallbackQueryHandler(self._profit, pattern='update_count')
|
||||||
]
|
]
|
||||||
for handle in handles:
|
for handle in handles:
|
||||||
self._updater.dispatcher.add_handler(handle)
|
self._updater.dispatcher.add_handler(handle)
|
||||||
|
|
||||||
|
for handle in callbacks:
|
||||||
|
self._updater.dispatcher.add_handler(handle)
|
||||||
|
|
||||||
self._updater.start_polling(
|
self._updater.start_polling(
|
||||||
clean=True,
|
clean=True,
|
||||||
bootstrap_retries=-1,
|
bootstrap_retries=-1,
|
||||||
@ -336,9 +352,12 @@ class Telegram(RPCHandler):
|
|||||||
try:
|
try:
|
||||||
statlist, head = self._rpc._rpc_status_table(
|
statlist, head = self._rpc._rpc_status_table(
|
||||||
self._config['stake_currency'], self._config.get('fiat_display_currency', ''))
|
self._config['stake_currency'], self._config.get('fiat_display_currency', ''))
|
||||||
|
|
||||||
message = tabulate(statlist, headers=head, tablefmt='simple')
|
message = tabulate(statlist, headers=head, tablefmt='simple')
|
||||||
self._send_msg(f"<pre>{message}</pre>", parse_mode=ParseMode.HTML)
|
if(update.callback_query):
|
||||||
|
query = update.callback_query
|
||||||
|
self._update_msg(chat_id=query.message.chat_id, message_id=query.message.message_id, msg=f"<pre>{message}</pre>", parse_mode=ParseMode.HTML, callback_path="update_status_table", reload_able=True)
|
||||||
|
else:
|
||||||
|
self._send_msg(f"<pre>{message}</pre>", reload_able=True, callback_path="update_status_table", parse_mode=ParseMode.HTML)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
self._send_msg(str(e))
|
||||||
|
|
||||||
@ -376,7 +395,11 @@ class Telegram(RPCHandler):
|
|||||||
],
|
],
|
||||||
tablefmt='simple')
|
tablefmt='simple')
|
||||||
message = f'<b>Daily Profit over the last {timescale} days</b>:\n<pre>{stats_tab}</pre>'
|
message = f'<b>Daily Profit over the last {timescale} days</b>:\n<pre>{stats_tab}</pre>'
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML)
|
if(update.callback_query):
|
||||||
|
query = update.callback_query
|
||||||
|
self._update_msg(chat_id=query.message.chat_id, message_id=query.message.message_id, msg=message, parse_mode=ParseMode.HTML, callback_path="update_daily", reload_able=True)
|
||||||
|
else:
|
||||||
|
self._send_msg(msg=message, parse_mode=ParseMode.HTML, callback_path="update_daily", reload_able=True)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
self._send_msg(str(e))
|
||||||
|
|
||||||
@ -435,7 +458,11 @@ class Telegram(RPCHandler):
|
|||||||
if stats['closed_trade_count'] > 0:
|
if stats['closed_trade_count'] > 0:
|
||||||
markdown_msg += (f"\n*Avg. Duration:* `{avg_duration}`\n"
|
markdown_msg += (f"\n*Avg. Duration:* `{avg_duration}`\n"
|
||||||
f"*Best Performing:* `{best_pair}: {best_rate:.2f}%`")
|
f"*Best Performing:* `{best_pair}: {best_rate:.2f}%`")
|
||||||
self._send_msg(markdown_msg)
|
if(update.callback_query):
|
||||||
|
query = update.callback_query
|
||||||
|
self._update_msg(chat_id=query.message.chat_id, message_id=query.message.message_id, msg=markdown_msg, callback_path="update_profit", reload_able=True)
|
||||||
|
else:
|
||||||
|
self._send_msg(msg=markdown_msg, callback_path="update_profit", reload_able=True)
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _stats(self, update: Update, context: CallbackContext) -> None:
|
def _stats(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -514,7 +541,11 @@ class Telegram(RPCHandler):
|
|||||||
output += ("\n*Estimated Value*:\n"
|
output += ("\n*Estimated Value*:\n"
|
||||||
"\t`{stake}: {total: .8f}`\n"
|
"\t`{stake}: {total: .8f}`\n"
|
||||||
"\t`{symbol}: {value: .2f}`\n").format(**result)
|
"\t`{symbol}: {value: .2f}`\n").format(**result)
|
||||||
self._send_msg(output)
|
if(update.callback_query):
|
||||||
|
query = update.callback_query
|
||||||
|
self._update_msg(chat_id=query.message.chat_id, message_id=query.message.message_id, msg=output, callback_path="update_balance", reload_able=True)
|
||||||
|
else:
|
||||||
|
self._send_msg(msg=output, callback_path="update_balance", reload_able=True)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
self._send_msg(str(e))
|
||||||
|
|
||||||
@ -679,7 +710,11 @@ class Telegram(RPCHandler):
|
|||||||
count=trade['count']
|
count=trade['count']
|
||||||
) for i, trade in enumerate(trades))
|
) for i, trade in enumerate(trades))
|
||||||
message = '<b>Performance:</b>\n{}'.format(stats)
|
message = '<b>Performance:</b>\n{}'.format(stats)
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML)
|
if(update.callback_query):
|
||||||
|
query = update.callback_query
|
||||||
|
self._update_msg(chat_id=query.message.chat_id, message_id=query.message.message_id, msg=message, parse_mode=ParseMode.HTML, callback_path="update_performance", reload_able=True)
|
||||||
|
else:
|
||||||
|
self._send_msg(msg=message, parse_mode=ParseMode.HTML, callback_path="update_performance", reload_able=True)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
self._send_msg(str(e))
|
||||||
|
|
||||||
@ -699,7 +734,11 @@ class Telegram(RPCHandler):
|
|||||||
tablefmt='simple')
|
tablefmt='simple')
|
||||||
message = "<pre>{}</pre>".format(message)
|
message = "<pre>{}</pre>".format(message)
|
||||||
logger.debug(message)
|
logger.debug(message)
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML)
|
if(update.callback_query):
|
||||||
|
query = update.callback_query
|
||||||
|
self._update_msg(chat_id=query.message.chat_id, message_id=query.message.message_id, msg=message, parse_mode=ParseMode.HTML, callback_path="update_count", reload_able=True)
|
||||||
|
else:
|
||||||
|
self._send_msg(msg=message, parse_mode=ParseMode.HTML, callback_path="update_count", reload_able=True)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
self._send_msg(str(e))
|
||||||
|
|
||||||
@ -901,8 +940,35 @@ class Telegram(RPCHandler):
|
|||||||
f"*Current state:* `{val['state']}`"
|
f"*Current state:* `{val['state']}`"
|
||||||
)
|
)
|
||||||
|
|
||||||
def _send_msg(self, msg: str, parse_mode: str = ParseMode.MARKDOWN,
|
def _update_msg(self, chat_id: str, message_id: str, msg: str, callback_path: str = "", reload_able: bool = False, parse_mode: str = ParseMode.MARKDOWN) -> None:
|
||||||
disable_notification: bool = False) -> None:
|
if reload_able:
|
||||||
|
reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton("Refresh", callback_data=callback_path)]])
|
||||||
|
else:
|
||||||
|
reply_markup = InlineKeyboardMarkup([[]])
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
self._updater.bot.edit_message_text(
|
||||||
|
chat_id=chat_id,
|
||||||
|
message_id=message_id,
|
||||||
|
text=msg,
|
||||||
|
parse_mode=parse_mode,
|
||||||
|
reply_markup=reply_markup
|
||||||
|
)
|
||||||
|
except BadRequest as e:
|
||||||
|
if 'not modified' in e.message.lower():
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
'TelegramError: %s',
|
||||||
|
e.message
|
||||||
|
)
|
||||||
|
except TelegramError as telegram_err:
|
||||||
|
logger.warning(
|
||||||
|
'TelegramError: %s! Giving up on that message.',
|
||||||
|
telegram_err.message
|
||||||
|
)
|
||||||
|
|
||||||
|
def _send_msg(self, msg: str, parse_mode: str = ParseMode.MARKDOWN, disable_notification: bool = False, callback_path: str = "", reload_able: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
Send given markdown message
|
Send given markdown message
|
||||||
:param msg: message
|
:param msg: message
|
||||||
@ -910,6 +976,9 @@ class Telegram(RPCHandler):
|
|||||||
:param parse_mode: telegram parse mode
|
:param parse_mode: telegram parse mode
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
if reload_able:
|
||||||
|
reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton("Refresh", callback_data=callback_path)]])
|
||||||
|
else:
|
||||||
reply_markup = ReplyKeyboardMarkup(self._keyboard)
|
reply_markup = ReplyKeyboardMarkup(self._keyboard)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user