Merge pull request #2097 from Luis-Fernando-Molina/develop
Contribution: Social-Bots project extracted from Fundations
This commit is contained in:
58 files changed
+807
-122
No files matched your search
@@ -1,110 +0,0 @@
|
||||
exports.newFoundationsBotModulesAnnouncements = function (processIndex) {
|
||||
|
||||
const MODULE_NAME = 'Announcements'
|
||||
|
||||
let thisObject = {
|
||||
makeAnnoucements: makeAnnoucements,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
let tradingSystem
|
||||
let tradingEngine
|
||||
let sessionParameters
|
||||
let taskParameters
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize() {
|
||||
tradingSystem = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingSystem
|
||||
tradingEngine = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingEngine
|
||||
sessionParameters = TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.tradingParameters
|
||||
|
||||
let taskParameters = {
|
||||
exchange: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name,
|
||||
market: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName +
|
||||
'/' +
|
||||
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
|
||||
}
|
||||
}
|
||||
|
||||
function finalize() {
|
||||
tradingSystem = undefined
|
||||
tradingEngine = undefined
|
||||
sessionParameters = undefined
|
||||
taskParameters = undefined
|
||||
TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.messagesSent = undefined
|
||||
}
|
||||
|
||||
function makeAnnoucements(node) {
|
||||
if (node === undefined) { return }
|
||||
if (node.announcements === undefined) { return }
|
||||
|
||||
for (let i = 0; i < node.announcements.length; i++) {
|
||||
let announcement = node.announcements[i]
|
||||
let canAnnounce = true
|
||||
if (announcement.announcementCondition !== undefined) {
|
||||
let conditionValue
|
||||
try {
|
||||
conditionValue = eval(announcement.announcementCondition.code)
|
||||
} catch (err) {
|
||||
|
||||
const message = 'Announcement Condition Evaluation Error'
|
||||
let docs = {
|
||||
project: 'Foundations',
|
||||
category: 'Topic',
|
||||
type: 'TS LF Trading Bot Error - ' + message,
|
||||
placeholder: {}
|
||||
}
|
||||
|
||||
TS.projects.education.utilities.docsFunctions.buildPlaceholder(docs, err, announcement.announcementCondition.name, announcement.announcementCondition.code, undefined, undefined, undefined)
|
||||
|
||||
tradingSystem.addError([announcement.announcementCondition.id, err.message, docs])
|
||||
}
|
||||
if (conditionValue !== true) { canAnnounce = false }
|
||||
}
|
||||
if (canAnnounce === true) {
|
||||
let text = announcement.config.text
|
||||
if (announcement.announcementFormula !== undefined) {
|
||||
let formulaValue
|
||||
let errorMessage
|
||||
|
||||
try {
|
||||
formulaValue = eval(announcement.announcementFormula.code)
|
||||
} catch (err) {
|
||||
|
||||
errorMessage = err.message
|
||||
const message = 'Announcement Formula Evaluation Error'
|
||||
let docs = {
|
||||
project: 'Foundations',
|
||||
category: 'Topic',
|
||||
type: 'TS LF Trading Bot Error - ' + message,
|
||||
placeholder: {}
|
||||
}
|
||||
|
||||
TS.projects.education.utilities.docsFunctions.buildPlaceholder(docs, err, announcement.announcementFormula.name, announcement.announcementFormula.code, undefined, undefined, undefined)
|
||||
|
||||
tradingSystem.addError([announcement.announcementFormula.id, err.message, docs])
|
||||
}
|
||||
if (errorMessage == undefined) { text = formulaValue }
|
||||
}
|
||||
|
||||
if (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots !== undefined) {
|
||||
TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots.announce(text)
|
||||
tradingSystem.announcements.push([announcement.id, text])
|
||||
} else {
|
||||
|
||||
const message = 'Social Bots Node Missing'
|
||||
let docs = {
|
||||
project: 'Foundations',
|
||||
category: 'Topic',
|
||||
type: 'TS LF Trading Bot Error - ' + message,
|
||||
placeholder: {}
|
||||
}
|
||||
|
||||
tradingSystem.addError([TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.id, message, docs])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
exports.newFoundationsBotModulesDiscordBot = function (processIndex) {
|
||||
|
||||
const MODULE_NAME = 'Discord Bot'
|
||||
|
||||
let thisObject = {
|
||||
webhookURL: undefined,
|
||||
taskParameters: undefined,
|
||||
format: undefined,
|
||||
sendMessage: sendMessage,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize(config) {
|
||||
/* Discord Bot Initialization */
|
||||
|
||||
try {
|
||||
thisObject.webhookURL = config.webhookURL
|
||||
thisObject.taskParameters = {
|
||||
exchange: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name,
|
||||
market: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName +
|
||||
'/' +
|
||||
TS.projects.foundations.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) {
|
||||
logError("initialize -> err = " + err.stack);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function finalize() {}
|
||||
|
||||
function sendMessage(message) {
|
||||
const https = SA.nodeModules.https
|
||||
|
||||
try {
|
||||
message = formatMessage(message)
|
||||
message = JSON.stringify({content: message})
|
||||
} catch (err) {
|
||||
logError("announce -> Discord JSON message error -> err = " + err)
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: { '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)
|
||||
logWarn("announce -> Discord request error -> err = " + err)
|
||||
})
|
||||
|
||||
req.write(message)
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
|
||||
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.foundations.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.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[ERROR] ' + message)
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
exports.newFoundationsBotModulesSlackBot = function (processIndex) {
|
||||
|
||||
const MODULE_NAME = 'Slack Bot'
|
||||
|
||||
let thisObject = {
|
||||
webhookURL: undefined,
|
||||
taskParameters: undefined,
|
||||
format: undefined,
|
||||
sendMessage: sendMessage,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize(config) {
|
||||
/* Slack Bot Initialization */
|
||||
|
||||
try {
|
||||
thisObject.webhookURL = config.webhookURL
|
||||
thisObject.taskParameters = {
|
||||
exchange: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name,
|
||||
market: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName +
|
||||
'/' +
|
||||
TS.projects.foundations.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) {
|
||||
logError("initialize -> err = " + err.stack);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function finalize() {}
|
||||
|
||||
function sendMessage(message) {
|
||||
const https = SA.nodeModules.https
|
||||
|
||||
try {
|
||||
message = formatMessage(message)
|
||||
message = JSON.stringify({text: message})
|
||||
} catch (err) {
|
||||
logError("announce -> Slack JSON message error -> err = " + err)
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: { '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)
|
||||
logWarn("announce -> Slack request error -> err = " + err)
|
||||
})
|
||||
|
||||
req.write(message)
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
|
||||
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.foundations.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.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[ERROR] ' + message)
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
exports.newFoundationsBotModulesTelegramBot = function (processIndex) {
|
||||
|
||||
const MODULE_NAME = 'Telegram Bot'
|
||||
|
||||
let thisObject = {
|
||||
telegramBot: undefined,
|
||||
telegramAPI: undefined,
|
||||
chatId: undefined,
|
||||
sendMessage: sendMessage,
|
||||
initialize: initialize,
|
||||
finalize: finalize,
|
||||
interpretCommand: interpretCommand
|
||||
}
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize(config, commands) {
|
||||
/* Telegram Bot Initialization */
|
||||
try {
|
||||
let botToken = config.botToken
|
||||
let chatId = config.chatId
|
||||
|
||||
thisObject.chatId = chatId
|
||||
|
||||
const { Telegraf } = SA.nodeModules.telegraf
|
||||
|
||||
thisObject.telegramBot = new Telegraf(botToken)
|
||||
thisObject.telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.'))
|
||||
thisObject.telegramBot.help((ctx) => ctx.replyWithHTML('<b><u>SUPERALGOS BOT HELP</u></b>\n\n' +
|
||||
'• Use /stop to stop the session.\n' +
|
||||
'• Use /help to receive this help message.\n\n' +
|
||||
'Additional commands can be configured using the Social Bot Command node.'))
|
||||
thisObject.telegramBot.command('stop', (ctx) => TS.projects.foundations.functionLibraries.sessionFunctions.stopSession(processIndex, 'by command from Telegram.'))
|
||||
|
||||
/* Set up additional commands for each Social Bot Command node */
|
||||
if(commands !== undefined) {
|
||||
for (let i = 0; i < commands.length; i++) {
|
||||
thisObject.telegramBot.command(commands[i].config.name, (ctx) => interpretCommand(commands[i].formula.code, processIndex))
|
||||
}
|
||||
}
|
||||
|
||||
thisObject.telegramBot.launch()
|
||||
|
||||
thisObject.telegramAPI = thisObject.telegramBot.telegram
|
||||
|
||||
const message = "Telegram bot is starting. For assistance type /help."
|
||||
thisObject.telegramAPI.sendMessage(thisObject.chatId, message)
|
||||
|
||||
} catch (err) {
|
||||
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME,
|
||||
"[WARN] initialize -> Telegram API error -> err = " + err.stack)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function finalize() {
|
||||
const message = "Telegram bot is signing off."
|
||||
thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME,
|
||||
"[WARN] finalize -> Telegram API error -> err = " + err.stack))
|
||||
}
|
||||
|
||||
function sendMessage(message) {
|
||||
thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME,
|
||||
"[WARN] sendMessage -> Telegram API error -> err = " + err.stack))
|
||||
}
|
||||
|
||||
|
||||
function interpretCommand(command, processIndex) {
|
||||
|
||||
let message
|
||||
let tradingEngine
|
||||
let tradingSystem
|
||||
let sessionParameters
|
||||
|
||||
if(TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE !== undefined) {
|
||||
tradingEngine = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingEngine
|
||||
tradingSystem = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingSystem
|
||||
}
|
||||
sessionParameters = TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.tradingParameters
|
||||
|
||||
let taskParameters = {
|
||||
exchange: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name,
|
||||
market: TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName +
|
||||
'/' +
|
||||
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
|
||||
}
|
||||
|
||||
try {
|
||||
message = eval(command)
|
||||
} catch (err) {
|
||||
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME,
|
||||
"[WARN] interpretCommand -> Telegram API error evaluating user formula -> err = " + err.stack)
|
||||
return
|
||||
}
|
||||
|
||||
thisObject.telegramAPI.sendMessage(thisObject.chatId, message, {parse_mode: 'HTML'}).catch(err => TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME,
|
||||
"[WARN] sendMessage -> Telegram API error -> err = " + err.stack))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user