Merge pull request #847 from ssplatt/discord-webhook-announce

WIP: Discord webhook announce
This commit is contained in:
Luis Fernando Molina authored and GitHub committed 2021-04-27 20:50:58 -04:00
commit f0ad6c51eb
9 files changed
+212 -9

No files matched your search

+8 -1
View File
@@ -675,6 +675,13 @@
"fileName": "TelegramBot.js",
"folderName": "Trading-Bot/Announcements"
},
{
"name": "Discord Bot",
"propertyName": "discordBot",
"functionName": "newSuperalgosBotModulesDiscordBot",
"fileName": "DiscordBot.js",
"folderName": "Trading-Bot/Announcements"
},
{
"name": "Trading Process",
"propertyName": "tradingProcess",
@@ -943,4 +950,4 @@
]
}
}
]
]
@@ -0,0 +1,41 @@
{
"type": "Discord Bot",
"editors": {
"config": true
},
"menuItems": [
{
"action": "Configure",
"actionFunction": "uiObject.configEditor.activate",
"label": "Configure",
"iconPathOn": "configuration",
"iconPathOff": "configuration"
},
{
"action": "Send Discord Test Message",
"label": "Send Discord Test Message",
"iconPathOn": "test-entity",
"iconPathOff": "test-entity",
"actionFunction": "payload.executeAction"
},
{
"action": "Delete UI Object",
"askConfirmation": true,
"confirmationLabel": "Confirm to Delete",
"label": "Delete",
"iconPathOn": "delete-entity",
"iconPathOff": "delete-entity",
"actionFunction": "payload.executeAction"
}
],
"initialValues": {
"config": "{\n\"webhookURL\": \"Paste the webhook url obtained from Discord > Channel Settings > Integrations > Webhooks > New Webhook\"\n}"
},
"addLeftIcons": true,
"level": 4,
"attachingRules": {
"compatibleTypes": "->Social Bots->"
},
"childrenNodesProperties": [],
"propertyNameAtParent": "bots"
}
@@ -7,6 +7,12 @@
"relatedUiObject": "Telegram Bot",
"actionFunction": "payload.executeAction"
},
{
"action": "Add Discord Bot",
"label": "Add Discord Bot",
"relatedUiObject": "Discord Bot",
"actionFunction": "payload.executeAction"
},
{
"action": "Delete UI Object",
"askConfirmation": true,
@@ -26,9 +32,9 @@
{
"name": "bots",
"type": "array",
"childType": "Telegram Bot",
"childType": "Telegram Bot, Discord Bot",
"autoAdd": true
}
],
"propertyNameAtParent": "socialBots"
}
}
@@ -0,0 +1,14 @@
{
"type": "Discord Bot",
"definition": {
"text": "The Discord Bot node is used for defining the configuration for a Discord Bot. This bot allows Superalgos to send announcements to a Discord Channel.",
"updated": 1619546769461
},
"paragraphs": [
{
"style": "Link",
"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
}
]
}
@@ -8,8 +8,8 @@
},
{
"style": "Note",
"text": "At present Superalgos only supports the Telegram messaging application.",
"updated": 1618221056200
"text": "At present Superalgos only supports Telegram and Discord messaging applications.",
"updated": 1619547181067
}
],
"definition": {
@@ -0,0 +1,65 @@
exports.newSuperalgosBotModulesDiscordBot = function (processIndex) {
const MODULE_NAME = 'Discord Bot'
let thisObject = {
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()
})
}
}
@@ -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)
}
}
}
}
}
}
}
@@ -717,6 +717,11 @@ function newSuperalgosActionSwitch() {
UI.projects.superalgos.functionLibraries.socialBotsFunctions.sendTelegramTestMessage(action.node, action.callBackFunction)
}
break
case 'Send Discord Test Message':
{
UI.projects.superalgos.functionLibraries.socialBotsFunctions.sendDiscordTestMessage(action.node, action.callBackFunction)
}
break
}
}
}
@@ -1,6 +1,7 @@
function newSuperalgosFunctionLibrarySocialBotsFunctions() {
thisObject = {
sendTelegramTestMessage: sendTelegramTestMessage
sendTelegramTestMessage: sendTelegramTestMessage,
sendDiscordTestMessage: sendDiscordTestMessage
}
return thisObject
@@ -70,4 +71,62 @@ function newSuperalgosFunctionLibrarySocialBotsFunctions() {
}
}
}
function sendDiscordTestMessage(node, callBackFunction) {
let webhookURL = UI.projects.superalgos.utilities.nodeConfig.loadPropertyFromNodeConfig(node.payload, 'webhookURL')
if (webhookURL === undefined) {
node.payload.uiObject.setErrorMessage('webhookURL parameter not defined.')
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
return
}
let params = {
text: "Test message from Superalgos!"
}
httpRequestJSON(JSON.stringify(params), webhookURL, onResponse)
function onResponse(err) {
if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
node.payload.uiObject.setInfoMessage('Discord message sent.')
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
} else {
node.payload.uiObject.setErrorMessage('Could not send Discord message. Error Response: ' + err.message)
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
}
}
function httpRequestJSON(pContentToSend, pPath, callBackFunction) {
let xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, xmlHttpRequest.responseText);
return;
} else if (this.readyState === 4 && (this.status === 404 || this.status === 401 || this.status === 400)) {
callBackFunction({
result: "Fail",
message: JSON.parse(xmlHttpRequest.responseText).description,
code: JSON.parse(xmlHttpRequest.responseText).error_code
});
return;
}
};
if (pContentToSend === undefined) {
xmlHttpRequest.open("GET", pPath, true);
xmlHttpRequest.send();
} else {
try {
let blob = new Blob([pContentToSend], { type: 'application/json' });
xmlHttpRequest.open("POST", pPath, true);
xmlHttpRequest.send(blob);
} catch (err) {
if (ERROR_LOG === true) { console.log(spacePad(MODULE_NAME, 50) + " : " + "[ERROR] callServer -> err.message = " & err.message); }
callBackFunction({ result: "Fail", message: err.message })
}
}
}
}
}