diff --git a/Projects/Superalgos/Schemas/Docs-Nodes/discord-bot.json b/Projects/Superalgos/Schemas/Docs-Nodes/discord-bot.json index 826b51e65..b745a1206 100644 --- a/Projects/Superalgos/Schemas/Docs-Nodes/discord-bot.json +++ b/Projects/Superalgos/Schemas/Docs-Nodes/discord-bot.json @@ -6,9 +6,53 @@ }, "paragraphs": [ { - "style": "Link", + "style": "Text", "text": "In order to use the Discord Bot, you will need to create a new Webhook Integration in Discord. The only configuration that is required within Superalgos is the webhookURL.", "updated": 1619546875758 + }, + { + "style": "Text", + "text": "Parameters:" + }, + { + "style": "Text", + "text": "- webhookURL - required - obtained from Integrations > Webhooks in the channel settings." + }, + { + "style": "Text", + "text": "- format - optional - allows custom formatting of the message being sent into Discord." + }, + { + "style": "Text", + "text": "Recognized Format Variables:" + }, + { + "style": "Text", + "text": "- `%{EXCHANGE}` the exchange which is operating, i.e. Binance" + }, + { + "style": "Text", + "text": "- `%{MARKET}` the market which is operating. i.e. BTC/USDT" + }, + { + "style": "Text", + "text": "- `%{MESSAGE}` the text being sent" + }, + { + "style": "Text", + "text": "An example format is:" + }, + { + "style": "Javascript", + "text": "\"%{EXCHANGE} %{MARKET}: %{MESSAGE}\"" + }, + { + "style": "Text", + "text": "Which results in a final message in Discord that looks like:" + }, + { + "style": "Javascript", + "text": "\"Binance BTC/USDT: Backtesting Session 'Lightning Strike' is starting.\"" } ] -} \ No newline at end of file +} diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js index af4c7a5aa..abc1f2fb7 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js @@ -4,6 +4,8 @@ exports.newSuperalgosBotModulesDiscordBot = function (processIndex) { let thisObject = { webhookURL: undefined, + taskParameters: undefined, + format: undefined, sendMessage: sendMessage, initialize: initialize, finalize: finalize @@ -11,32 +13,38 @@ exports.newSuperalgosBotModulesDiscordBot = function (processIndex) { return thisObject - function initialize(webhookURL) { + function initialize(config) { /* Discord Bot Initialization */ try { - thisObject.webhookURL = webhookURL - - const message = "Discord bot has started." - thisObject.sendMessage(message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeDiscordBot -> Discord error -> err = " + err)) + thisObject.webhookURL = config.webhookURL + thisObject.taskParameters = { + exchange: TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name, + market: TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + + '/' + + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName + } + if (config.format === undefined) { + thisObject.format = "%{MESSAGE}" + } else { + thisObject.format = config.format + } } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> err = " + err.stack); + logError("initialize -> err = " + err.stack); } } - function finalize() { - const message = "Discord bot has stopped." - thisObject.sendMessage(message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] finalize -> Discord error -> err = " + err)) - } + function finalize() {} function sendMessage(message) { const https = require('https') try { + message = formatMessage(message) message = JSON.stringify({content: message}) } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] announce -> Discord JSON message error -> err = " + err) + logError("announce -> Discord JSON message error -> err = " + err) } return new Promise((resolve, reject) => { @@ -53,7 +61,7 @@ exports.newSuperalgosBotModulesDiscordBot = function (processIndex) { req.on('error', (err) => { reject(err) - parentLogger.write(MODULE_NAME, "[WARN] announce -> Discord request error -> err = " + err) + logWarn("announce -> Discord request error -> err = " + err) }) req.write(message) @@ -61,4 +69,19 @@ exports.newSuperalgosBotModulesDiscordBot = function (processIndex) { }) } + function formatMessage(message) { + formattedMessage = thisObject.format + formattedMessage = formattedMessage.replace('%{EXCHANGE}', thisObject.taskParameters.exchange) + formattedMessage = formattedMessage.replace('%{MARKET}', thisObject.taskParameters.market) + formattedMessage = formattedMessage.replace('%{MESSAGE}', message) + return formattedMessage + } + + function logWarn(message) { + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[WARN] ' + message) + } + + function logError(message) { + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[ERROR] ' + message) + } } diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/SocialBots.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/SocialBots.js index 28581db0d..4fd29c7f2 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/SocialBots.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/SocialBots.js @@ -23,7 +23,7 @@ exports.newSuperalgosBotModulesSocialBots = function (processIndex) { } else if (socialBot.type === "Discord Bot") { let config = socialBot.config socialBot.botInstance = TS.projects.superalgos.botModules.discordBot.newSuperalgosBotModulesDiscordBot(processIndex) - socialBot.botInstance.initialize(config.webhookURL) + socialBot.botInstance.initialize(config) } } } diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/Announcements.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/Announcements.js index 1d18840c8..fa28ff6cc 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/Announcements.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/Announcements.js @@ -21,6 +21,7 @@ exports.newSuperalgosBotModulesAnnouncements = function (processIndex) { sessionParameters = TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.tradingParameters let taskParameters = { + exchange: TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name, market: TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName @@ -86,7 +87,6 @@ exports.newSuperalgosBotModulesAnnouncements = function (processIndex) { tradingSystem.addError([announcement.announcementFormula.id, err.message, docs]) } if (errorMessage == undefined) { text = formulaValue } - } if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots !== undefined) { diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js index c3b50a007..a99e5cc46 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js @@ -1353,4 +1353,3 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex) { throw 'Error Already Recorded' } } -