Merge pull request #904 from ssplatt/discord-text

Add formatting option for Discord announcement messages
This commit is contained in:
Luis Fernando Molina authored and GitHub committed 2021-05-11 09:38:47 -04:00
commit 31233588db
5 files changed
+83 -17

No files matched your search

@@ -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.\""
}
]
}
}
@@ -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)
}
}
@@ -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)
}
}
}
@@ -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) {
@@ -1353,4 +1353,3 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex) {
throw 'Error Already Recorded'
}
}