Merge pull request #2235 from ssplatt/twitter-bot-updates

Make the twitter social/announce bot functional
This commit is contained in:
Luis Fernando Molina authored and GitHub committed 2021-10-27 02:01:05 -04:00
commit 2efa45eb29
9 files changed
+8390 -435

No files matched your search

@@ -1142,12 +1142,12 @@ exports.newAlgorithmicTradingBotModulesTradingStages = function (processIndex) {
if (stage.status.value === 'Open') {
openStage(stage)
checkUserDefinedCode(stageName, 'Open')
checkAnnounce(stageName, 'Open')
checkAnnounce(stageName, stage.status.value)
}
if (stage.status.value === 'Closed') {
checkUserDefinedCode(stageName, 'Closed')
checkAnnounce(stageName, 'Closed')
closeStage(stage, stageName)
checkAnnounce(stageName, stage.status.value)
}
/*
@@ -18,7 +18,7 @@
"iconPathOn": "test-entity",
"iconPathOff": "test-entity",
"actionFunction": "payload.executeAction",
"actionProject": "SocialBots"
"actionProject": "Social-Bots"
},
{
"action": "Delete UI Object",
@@ -41,4 +41,4 @@
},
"childrenNodesProperties": [],
"propertyNameAtParent": "discordBots"
}
}
@@ -18,7 +18,7 @@
"iconPathOn": "test-entity",
"iconPathOff": "test-entity",
"actionFunction": "payload.executeAction",
"actionProject": "SocialBots"
"actionProject": "Social-Bots"
},
{
"action": "Delete UI Object",
@@ -41,4 +41,4 @@
},
"childrenNodesProperties": [],
"propertyNameAtParent": "slackBots"
}
}
@@ -26,7 +26,7 @@
"iconPathOn": "test-entity",
"iconPathOff": "test-entity",
"actionFunction": "payload.executeAction",
"actionProject": "SocialBots"
"actionProject": "Social-Bots"
},
{
"action": "Delete UI Object",
@@ -57,4 +57,4 @@
}
],
"propertyNameAtParent": "telegramBots"
}
}
@@ -26,7 +26,7 @@
"iconPathOn": "test-entity",
"iconPathOff": "test-entity",
"actionFunction": "payload.executeAction",
"actionProject": "SocialBots"
"actionProject": "Social-Bots"
},
{
"action": "Delete UI Object",
@@ -40,7 +40,7 @@
}
],
"initialValues": {
"config": "{ \n\"botToken\": \"Paste the bot token obtained from Twitter \",\n}"
"config": "{\n \"consumer_key\": \"TWITTER_CONSUMER_KEY\",\n \"consumer_secret\": \"TWITTER_CONSUMER_SECRET\",\n \"access_token_key\": \"TWITTER_ACCESS_TOKEN_KEY\",\n \"access_token_secret\": \"TWITTER_ACCESS_TOKEN_SECRET\"\n}"
},
"addLeftIcons": true,
"level": 3,
@@ -57,4 +57,4 @@
}
],
"propertyNameAtParent": "twitterBots"
}
}
@@ -28,7 +28,7 @@ exports.newSocialBotsBotModulesSocialBots = function (processIndex) {
socialBot.botInstance = TS.projects.socialBots.botModules.slackBot.newSocialBotsBotModulesSlackBot(processIndex)
socialBot.botInstance.initialize(config)
} else if (socialBot.type === "Twitter Bot") {
socialBot.botInstance = TS.projects.socialBots.botModules.slackBot.newSocialBotsBotModulesTwitterBot(processIndex)
socialBot.botInstance = TS.projects.socialBots.botModules.twitterBot.newSocialBotsBotModulesTwitterBot(processIndex)
socialBot.botInstance.initialize(config)
}
}
@@ -44,7 +44,7 @@ exports.newSocialBotsBotModulesSocialBots = function (processIndex) {
try {
if (socialBot.type === "Telegram Bot") {
socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, text).catch(err => TS.projects.foundations.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.type === "Slack Bot") {
} else if (socialBot.type === "Discord Bot" || socialBot.type === "Slack Bot" || socialBot.type === "Twitter Bot") {
socialBot.botInstance.sendMessage(text).catch(err => TS.projects.foundations.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) {
@@ -67,7 +67,7 @@ exports.newSocialBotsBotModulesSocialBots = function (processIndex) {
if (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots[prop] !== undefined) {
for (let i = 0; i < TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots[prop].length; i++) {
let socialBot = TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots[prop][i]
if (socialBot.type === "Telegram Bot" || socialBot.type === "Discord Bot" || socialBot.type === "Slack Bot") {
if (socialBot.type === "Telegram Bot" || socialBot.type === "Discord Bot" || socialBot.type === "Slack Bot" || socialBot.type === "Twitter Bot") {
socialBot.botInstance.finalize()
}
}
@@ -83,7 +83,7 @@ exports.newSocialBotsBotModulesSocialBots = function (processIndex) {
if (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots[prop] !== undefined) {
for (let i = 0; i < TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots[prop].length; i++) {
let socialBot = TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.socialBots[prop][i]
if (socialBot.type === "Telegram Bot" || socialBot.type === "Discord Bot" || socialBot.type === "Slack Bot") {
if (socialBot.type === "Telegram Bot" || socialBot.type === "Discord Bot" || socialBot.type === "Slack Bot" || socialBot.type === "Twitter Bot") {
socialBot.botInstance.sendMessage(message)
}
}
@@ -5,7 +5,7 @@ exports.newSocialBotsBotModulesTwitterBot = function (processIndex) {
let thisObject = {
taskParameters: undefined,
format: undefined,
twitterclient: undefined,
twitterClient: undefined,
sendMessage: sendMessage,
initialize: initialize,
finalize: finalize
@@ -35,20 +35,30 @@ exports.newSocialBotsBotModulesTwitterBot = function (processIndex) {
access_token_secret = process.env.TWITTER_ACCESS_TOKEN_SECRET
}
var twitterclient = new Twitter({
if (!consumer_key) {
logError("consumer_key must be set")
}
if (!consumer_secret) {
logError("consumer_secret must be set")
}
if (!access_token_key) {
logError("access_token_key must be set")
}
if (!access_token_secret) {
logError("access_token_secret must be set")
}
var twitterClient = new Twitter({
consumer_key: consumer_key,
consumer_secret: consumer_secret,
access_token_key: access_token_key,
access_token_secret: access_token_secret
});
thisObject.twitterclient = twitterclient
thisObject.twitterClient = twitterClient
try {
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
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}"
@@ -58,23 +68,23 @@ exports.newSocialBotsBotModulesTwitterBot = function (processIndex) {
} catch (err) {
logError("initialize -> err = " + err.stack);
}
}
function finalize() {}
function sendMessage(message) {
try {
message = formatMessage(message)
message = JSON.stringify({status: message})
message = {status: formatMessage(message)}
} catch (err) {
logError("announce -> Twitter JSON message error -> err = " + err)
}
console.log(thisObject.twitterClient)
thisObject.twitterclient.post('statuses/update', message, function(error, tweet, response) {
if(error) throw error;
console.log(tweet); // Tweet body.
console.log(response); // Raw response object.
thisObject.twitterClient.post('statuses/update', message, function(error) {
if(error) {
logError(error)
throw error
}
});
}
@@ -94,4 +104,4 @@ exports.newSocialBotsBotModulesTwitterBot = function (processIndex) {
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)
}
}
}
@@ -186,6 +186,49 @@ function newSocialBotsFunctionLibrarySocialBotsFunctions() {
}
function sendTwitterTestMessage(node, callBackFunction) {
let message = JSON.stringify({text: "Test message from Superalgos!"})
let url = "https://api.twitter.com/1.1/statuses/update.json"
httpRequestJSON(message, url, onResponse)
function onResponse(err) {
if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
node.payload.uiObject.setInfoMessage('Twitter status update sent.')
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
} else {
node.payload.uiObject.setErrorMessage('Could not send Twitter status update. 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: xmlHttpRequest.responseText
});
return;
}
};
if (pContentToSend === undefined) {
xmlHttpRequest.open("GET", pPath, true);
xmlHttpRequest.send();
} else {
try {
let blob = new Blob([pContentToSend]);
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 })
}
}
}
}
}
+8308 -406
View File
File diff suppressed because it is too large. Load diff