diff --git a/src/botPage/bot/Interface/MiscInterface.js b/src/botPage/bot/Interface/MiscInterface.js index 7182a3dac4..329c503bf5 100644 --- a/src/botPage/bot/Interface/MiscInterface.js +++ b/src/botPage/bot/Interface/MiscInterface.js @@ -1,10 +1,32 @@ +import { notify } from '../broadcast'; +import { translate } from '../../../common/i18n'; import { observer as globalObserver } from '../../../common/utils/observer'; export default Interface => class extends Interface { + // eslint-disable-next-line class-methods-use-this + notifyTelegram(accessToken, chatId, text) { + const url = `https://api.telegram.org/bot${accessToken}/sendMessage`; + const onError = () => notify('warn', translate('The Telegram notification could not be sent')); + + fetch(url, { + method : 'POST', + mode : 'cors', + headers: { 'Content-Type': 'application/json' }, + body : JSON.stringify({ chat_id: chatId, text }), + }) + .then(response => { + if (!response.ok) { + onError(); + } + }) + .catch(onError); + } + getMiscInterface() { return { notify : args => globalObserver.emit('Notify', args), + notifyTelegram: this.notifyTelegram, getTotalRuns : () => this.tradeEngine.getTotalRuns(), getBalance : type => this.tradeEngine.getBalance(type), getTotalProfit: () => this.tradeEngine.getTotalProfit(), diff --git a/src/botPage/view/blockly/blocks/tools/index.js b/src/botPage/view/blockly/blocks/tools/index.js index a4c2bede3e..699e51210a 100644 --- a/src/botPage/view/blockly/blocks/tools/index.js +++ b/src/botPage/view/blockly/blocks/tools/index.js @@ -1,5 +1,6 @@ import './balance'; import './notify'; +import './notify_telegram'; import './total_profit'; import './total_runs'; import './block_holder'; diff --git a/src/botPage/view/blockly/blocks/tools/notify_telegram.js b/src/botPage/view/blockly/blocks/tools/notify_telegram.js new file mode 100644 index 0000000000..f93a7cc9f8 --- /dev/null +++ b/src/botPage/view/blockly/blocks/tools/notify_telegram.js @@ -0,0 +1,45 @@ +import { translate } from '../../../../../common/i18n'; + +Blockly.Blocks.notify_telegram = { + init() { + this.jsonInit({ + message0: translate('Notify Telegram %1 Access Token: %2 Chat ID: %3 Message: %4'), + args0 : [ + { + type: 'input_dummy', + }, + { + type: 'input_value', + name: 'TELEGRAM_ACCESS_TOKEN', + }, + { + type: 'input_value', + name: 'TELEGRAM_CHAT_ID', + }, + { + type: 'input_value', + name: 'TELEGRAM_MESSAGE', + }, + ], + colour : '#dedede', + inputsInline : true, + previousStatement: null, + nextStatement : null, + tooltip : translate('Sends a message to Telegram'), + }); + }, +}; + +Blockly.JavaScript.notify_telegram = block => { + const accessToken = + Blockly.JavaScript.valueToCode(block, 'TELEGRAM_ACCESS_TOKEN', Blockly.JavaScript.ORDER_ATOMIC) || ''; + const chatId = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_CHAT_ID', Blockly.JavaScript.ORDER_ATOMIC) || ''; + const message = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_MESSAGE', Blockly.JavaScript.ORDER_ATOMIC) || ''; + + if (!accessToken || !chatId || !message) { + return ''; + } + + const code = `Bot.notifyTelegram(${accessToken}, ${chatId}, ${message});\n`; + return code; +}; diff --git a/src/common/lang.js b/src/common/lang.js index ad2fe3a4a2..196ef3fd5a 100644 --- a/src/common/lang.js +++ b/src/common/lang.js @@ -17,7 +17,7 @@ const addUiLang = () => { el.text(translate($(this).attr('data-i18n-text'))).append(contents); }); - document.querySelectorAll('[data-i18n-title').forEach(titleNode => { + document.querySelectorAll('[data-i18n-title]').forEach(titleNode => { titleNode.setAttribute('title', translate(titleNode.getAttribute('data-i18n-title'))); }); }; diff --git a/static/xml/toolbox.xml b/static/xml/toolbox.xml index a8c4658f1c..d19db21264 100644 --- a/static/xml/toolbox.xml +++ b/static/xml/toolbox.xml @@ -1,443 +1,459 @@