From 2719b92288d828ef5385c1bd9afd3a0a375de85a Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 13 May 2019 17:10:45 +0800 Subject: [PATCH 01/12] Create notifyTelegram logic --- src/botPage/bot/Interface/MiscInterface.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/botPage/bot/Interface/MiscInterface.js b/src/botPage/bot/Interface/MiscInterface.js index 7182a3dac4..047200344a 100644 --- a/src/botPage/bot/Interface/MiscInterface.js +++ b/src/botPage/bot/Interface/MiscInterface.js @@ -1,10 +1,26 @@ +import { notify } from '../broadcast'; +import { translate } from '../../../common/i18n'; import { observer as globalObserver } from '../../../common/utils/observer'; export default Interface => class extends Interface { + notifyTelegram(botId, chatId, message) { + const url = encodeURI(`https://api.telegram.org/bot${botId}/sendMessage?chat_id=${chatId}&text=${message}`); + const onError = () => notify('warn', translate('The Telegram notification could not be sent')); + + fetch(url) + .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(), From df291e73d95f873c9f3bd3f864c5050804ef9c3e Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 13 May 2019 17:11:04 +0800 Subject: [PATCH 02/12] Add import for notify_telegram.js --- src/botPage/view/blockly/blocks/tools/index.js | 1 + 1 file changed, 1 insertion(+) 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'; From 6fc7a4ac1e6e68c59ca3e3dd42a17e8869c96944 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 13 May 2019 17:11:35 +0800 Subject: [PATCH 03/12] Add Telegram Notify block to toolbox --- static/xml/toolbox.xml | 898 +++++++++++++++++++++-------------------- 1 file changed, 457 insertions(+), 441 deletions(-) diff --git a/static/xml/toolbox.xml b/static/xml/toolbox.xml index a8c4658f1c..a4f325e33d 100644 --- a/static/xml/toolbox.xml +++ b/static/xml/toolbox.xml @@ -1,443 +1,459 @@ From 3c4c865617952386c0161bef3f60daf351ec4be7 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 13 May 2019 17:12:03 +0800 Subject: [PATCH 04/12] Block definition for notify_telegram type block --- .../blockly/blocks/tools/notify_telegram.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/botPage/view/blockly/blocks/tools/notify_telegram.js 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..73143f4b09 --- /dev/null +++ b/src/botPage/view/blockly/blocks/tools/notify_telegram.js @@ -0,0 +1,44 @@ +import { translate } from '../../../../../common/i18n'; + +Blockly.Blocks.notify_telegram = { + init() { + this.jsonInit({ + message0: translate('Notify Telegram %1 Bot ID: %2 Chat ID: %3 Message: %4'), + args0 : [ + { + type: 'input_dummy', + }, + { + type: 'input_value', + name: 'TELEGRAM_BOT_ID', + }, + { + 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 botId = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_BOT_ID') || ''; + const chatId = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_CHAT_ID') || ''; + const message = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_MESSAGE') || ''; + + if (!botId || !chatId || !message) { + return ''; + } + + const code = `Bot.notifyTelegram(${botId}, ${chatId}, ${message});\n`; + return code; +}; From 0a76765bbd025535af512cdc3ab405ba2ec8a79e Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Thu, 16 May 2019 13:13:43 +0800 Subject: [PATCH 05/12] Update 'Bot ID' to 'Access Token' --- src/botPage/view/blockly/blocks/tools/notify_telegram.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/botPage/view/blockly/blocks/tools/notify_telegram.js b/src/botPage/view/blockly/blocks/tools/notify_telegram.js index 73143f4b09..94989508b4 100644 --- a/src/botPage/view/blockly/blocks/tools/notify_telegram.js +++ b/src/botPage/view/blockly/blocks/tools/notify_telegram.js @@ -3,7 +3,7 @@ import { translate } from '../../../../../common/i18n'; Blockly.Blocks.notify_telegram = { init() { this.jsonInit({ - message0: translate('Notify Telegram %1 Bot ID: %2 Chat ID: %3 Message: %4'), + message0: translate('Notify Telegram %1 Access Token: %2 Chat ID: %3 Message: %4'), args0 : [ { type: 'input_dummy', From 1b5c762b30b78485cc64d6d8a40588c6d01382b3 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Thu, 16 May 2019 13:18:15 +0800 Subject: [PATCH 06/12] Update wording in code --- src/botPage/bot/Interface/MiscInterface.js | 6 ++++-- src/botPage/view/blockly/blocks/tools/notify_telegram.js | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/botPage/bot/Interface/MiscInterface.js b/src/botPage/bot/Interface/MiscInterface.js index 047200344a..c2b9516fe2 100644 --- a/src/botPage/bot/Interface/MiscInterface.js +++ b/src/botPage/bot/Interface/MiscInterface.js @@ -4,8 +4,10 @@ import { observer as globalObserver } from '../../../common/utils/observer'; export default Interface => class extends Interface { - notifyTelegram(botId, chatId, message) { - const url = encodeURI(`https://api.telegram.org/bot${botId}/sendMessage?chat_id=${chatId}&text=${message}`); + notifyTelegram(accessToken, chatId, message) { + const url = encodeURI( + `https://api.telegram.org/bot${accessToken}/sendMessage?chat_id=${chatId}&text=${message}` + ); const onError = () => notify('warn', translate('The Telegram notification could not be sent')); fetch(url) diff --git a/src/botPage/view/blockly/blocks/tools/notify_telegram.js b/src/botPage/view/blockly/blocks/tools/notify_telegram.js index 94989508b4..bcb1c33390 100644 --- a/src/botPage/view/blockly/blocks/tools/notify_telegram.js +++ b/src/botPage/view/blockly/blocks/tools/notify_telegram.js @@ -10,7 +10,7 @@ Blockly.Blocks.notify_telegram = { }, { type: 'input_value', - name: 'TELEGRAM_BOT_ID', + name: 'TELEGRAM_ACCESS_TOKEN', }, { type: 'input_value', @@ -31,14 +31,14 @@ Blockly.Blocks.notify_telegram = { }; Blockly.JavaScript.notify_telegram = block => { - const botId = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_BOT_ID') || ''; + const accessToken = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_ACCESS_TOKEN') || ''; const chatId = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_CHAT_ID') || ''; const message = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_MESSAGE') || ''; - if (!botId || !chatId || !message) { + if (!accessToken || !chatId || !message) { return ''; } - const code = `Bot.notifyTelegram(${botId}, ${chatId}, ${message});\n`; + const code = `Bot.notifyTelegram(${accessToken}, ${chatId}, ${message});\n`; return code; }; From 41bc8cd019fe7ca0aff0d9e3fad1f0ebcbe3af02 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Thu, 16 May 2019 13:25:47 +0800 Subject: [PATCH 07/12] Add exception for eslint --- src/botPage/bot/Interface/MiscInterface.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/botPage/bot/Interface/MiscInterface.js b/src/botPage/bot/Interface/MiscInterface.js index c2b9516fe2..de30daa701 100644 --- a/src/botPage/bot/Interface/MiscInterface.js +++ b/src/botPage/bot/Interface/MiscInterface.js @@ -4,6 +4,7 @@ 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, message) { const url = encodeURI( `https://api.telegram.org/bot${accessToken}/sendMessage?chat_id=${chatId}&text=${message}` From 5cc7b71069b26908ae45ee8b647954bcc9af2351 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 20 May 2019 11:30:43 +0800 Subject: [PATCH 08/12] Return ORDER_ATOMIC for valueToCode --- src/botPage/view/blockly/blocks/tools/notify_telegram.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/botPage/view/blockly/blocks/tools/notify_telegram.js b/src/botPage/view/blockly/blocks/tools/notify_telegram.js index bcb1c33390..c9adc439eb 100644 --- a/src/botPage/view/blockly/blocks/tools/notify_telegram.js +++ b/src/botPage/view/blockly/blocks/tools/notify_telegram.js @@ -1,4 +1,5 @@ import { translate } from '../../../../../common/i18n'; +import { notify } from '../../../../bot/broadcast'; Blockly.Blocks.notify_telegram = { init() { @@ -31,9 +32,10 @@ Blockly.Blocks.notify_telegram = { }; Blockly.JavaScript.notify_telegram = block => { - const accessToken = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_ACCESS_TOKEN') || ''; - const chatId = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_CHAT_ID') || ''; - const message = Blockly.JavaScript.valueToCode(block, 'TELEGRAM_MESSAGE') || ''; + 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 ''; From f44906e135ce4240e22081bfdc076d957ead6f8a Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 20 May 2019 11:31:18 +0800 Subject: [PATCH 09/12] Change method to POST for notifyTelegram --- src/botPage/bot/Interface/MiscInterface.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/botPage/bot/Interface/MiscInterface.js b/src/botPage/bot/Interface/MiscInterface.js index de30daa701..329c503bf5 100644 --- a/src/botPage/bot/Interface/MiscInterface.js +++ b/src/botPage/bot/Interface/MiscInterface.js @@ -5,13 +5,16 @@ 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, message) { - const url = encodeURI( - `https://api.telegram.org/bot${accessToken}/sendMessage?chat_id=${chatId}&text=${message}` - ); + 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) + fetch(url, { + method : 'POST', + mode : 'cors', + headers: { 'Content-Type': 'application/json' }, + body : JSON.stringify({ chat_id: chatId, text }), + }) .then(response => { if (!response.ok) { onError(); From 4c56f8fe353f858317cf066542b05c1c51a4ed07 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 20 May 2019 11:35:06 +0800 Subject: [PATCH 10/12] Remove unused import --- src/botPage/view/blockly/blocks/tools/notify_telegram.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/botPage/view/blockly/blocks/tools/notify_telegram.js b/src/botPage/view/blockly/blocks/tools/notify_telegram.js index c9adc439eb..f93a7cc9f8 100644 --- a/src/botPage/view/blockly/blocks/tools/notify_telegram.js +++ b/src/botPage/view/blockly/blocks/tools/notify_telegram.js @@ -1,5 +1,4 @@ import { translate } from '../../../../../common/i18n'; -import { notify } from '../../../../bot/broadcast'; Blockly.Blocks.notify_telegram = { init() { From 9a9a21ff2792f4e40d1a57264d31951580d70121 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Mon, 20 May 2019 11:39:17 +0800 Subject: [PATCH 11/12] Update shadow block value in toolbox --- static/xml/toolbox.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/xml/toolbox.xml b/static/xml/toolbox.xml index a4f325e33d..d19db21264 100644 --- a/static/xml/toolbox.xml +++ b/static/xml/toolbox.xml @@ -435,7 +435,7 @@ - + From 6d9419fd7ac0056dd627ee465496863864135163 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Wed, 22 May 2019 17:27:14 +0800 Subject: [PATCH 12/12] Fix typo in CSS selector --- src/common/lang.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'))); }); };