diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js new file mode 100644 index 000000000..73840e132 --- /dev/null +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/DiscordBot.js @@ -0,0 +1,66 @@ +exports.newSuperalgosBotModulesDiscordBot = function (processIndex) { + + const MODULE_NAME = 'Discord Bot' + + let thisObject = { + DiscordBot: undefined, + webhookURL: undefined, + sendMessage: sendMessage, + initialize: initialize, + finalize: finalize + } + + return thisObject + + function initialize(webhookURL) { + /* 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)) + + } catch (err) { + parentLogger.write(MODULE_NAME, "[WARN] 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 sendMessage(message) { + const https = require('https'); + + try { + message = JSON.stringify(message); + } catch (err) { + parentLogger.write(MODULE_NAME, "[WARN] announce -> Discord JSON message error -> err = " + err) + } + + return new Promise((resolve, reject) => { + const requestOptions = { + method: 'POST', + header: { 'Content-Type': 'application/json' } + } + + const req = https.request(thisObject.webhookURL, requestOptions, (res) => { + let response = '' + res.on('data', (data) => { response += data }) + res.on('end', () => { resolve(response) }) + }) + + req.on('error', (err) => { + reject(err) + parentLogger.write(MODULE_NAME, "[WARN] announce -> Discord request error -> err = " + err) + }) + + req.write(message) + req.end() + }) + } + +} 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 6e72dbc49..614c136cb 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/SocialBots.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/SocialBots.js @@ -19,6 +19,10 @@ exports.newSuperalgosBotModulesSocialBots = function (processIndex) { let config = socialBot.config socialBot.botInstance = TS.projects.superalgos.botModules.telegramBot.newSuperalgosBotModulesTelegramBot(processIndex) socialBot.botInstance.initialize(config.botToken, config.chatId) + } else if (socialBot.type === "Discord Bot"){ + let config = socialBot.config + socialBot.botInstance = TS.projects.superalgos.botModules.discordBot.newSuperalgosBotModulesTelegramBot(processIndex) + socialBot.botInstance.initialize(config.webhookURL) } } } @@ -30,6 +34,8 @@ exports.newSuperalgosBotModulesSocialBots = function (processIndex) { try { if (socialBot.type === "Telegram Bot") { socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, text).catch(err => TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) + } else if (socialBot.type === "Discord Bot") { + socialBot.botInstance.sendMessage(text).catch(err => TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Discord Bot error -> err = " + err)) } } catch (err) { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); @@ -47,7 +53,7 @@ exports.newSuperalgosBotModulesSocialBots = function (processIndex) { if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.bots !== undefined) { for (let i = 0; i < TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.bots.length; i++) { let socialBot = TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.bots[i] - if (socialBot.type === "Telegram Bot") { + if (socialBot.type === "Telegram Bot" || socialBot.type === "Discord Bot") { socialBot.botInstance.finalize() } } @@ -59,11 +65,11 @@ exports.newSuperalgosBotModulesSocialBots = function (processIndex) { if (TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.bots !== undefined) { for (let i = 0; i < TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.bots.length; i++) { let socialBot = TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.bots[i] - if (socialBot.type === "Telegram Bot") { + if (socialBot.type === "Telegram Bot" || socialBot.type === "Discord Bot") { socialBot.botInstance.sendMessage(message) } } } } } -} \ No newline at end of file +}