Skip to content

Commit

Permalink
update to twitter-v2, us TS for test message
Browse files Browse the repository at this point in the history
  • Loading branch information
ssplatt committed Nov 22, 2021
1 parent eb5556a commit 9a854b2
Show file tree
Hide file tree
Showing 5 changed files with 9,351 additions and 134 deletions.
18 changes: 9 additions & 9 deletions Projects/Social-Bots/TS/Bot-Modules/TwitterBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,14 @@ exports.newSocialBotsBotModulesTwitterBot = function (processIndex) {

function sendMessage(message) {
try {
message = {status: formatMessage(message)}
message = {text: formatMessage(message)}
} catch (err) {
logError("announce -> Twitter JSON message error -> err = " + err)
logError(`announce -> Twitter message formatting error -> err = ${err}`)
}

thisObject.twitterClient.post('statuses/update', message, function(error) {
if(error) {
logError(error)
throw error
}
});

const response = await thisObject.twitterClient.post('tweets', message)
.then(logInfo(`announce -> Twitter bot post tweet -> response -> ${response}`))
.catch((err) => { logError(`announce -> Twitter bot post tweet -> ${err}`) })
}

function formatMessage(message) {
Expand All @@ -96,6 +92,10 @@ exports.newSocialBotsBotModulesTwitterBot = function (processIndex) {
return formattedMessage
}

function logInfo(message) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, '[INFO] ' + message)
}

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)
}
Expand Down
93 changes: 58 additions & 35 deletions Projects/Social-Bots/UI/Function-Libraries/SocialBotsFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,49 +186,72 @@ 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)
let message = {text: "Test message from Superalgos!"}

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)
if (UI.environment.DEMO_MODE === true) {
if (window.location.hostname !== 'localhost') {
node.payload.uiObject.setWarningMessage('Superalgos is running is DEMO MODE. This means that you can not send test messages.', 5)
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
return
}
}

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;
}
};
let validationsResult = validations(node)
if (validationsResult === undefined) {
// If something fails at validations we just quit.
console.log('[DEBUG] valiations empty')
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
return
}
let lanNetworkNode = validationsResult.lanNetworkNode
if (lanNetworkNode === undefined) {
// This means that the validations failed.
console.log('[DEBUG] lan network empty')
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
return
}

if (pContentToSend === undefined) {
xmlHttpRequest.open("GET", pPath, true);
xmlHttpRequest.send();
// create event listener
console.log('[DEBUG] creating listener')
let eventSubscriptionIdOnStatus
let eventsServerClient = UI.projects.foundations.spaces.designSpace.workspace.eventsServerClients.get(lanNetworkNode.id)
let eventHandlerKey = `Social Bot - ${node.payload.parentNode.payload.parentNode.payload.parentNode.id}`
console.log('[DEBUG] event handler key: ', eventHandlerKey)
eventsServerClient.listenToEvent(eventHandlerKey, 'Test announcement', undefined, node.id, onResponse, onStatus)

// create event
console.log('[DEBUG] creating event')
let event = {
message: message
}
let key = node.name + '-' + node.type + '-' + node.id
eventsServerClient.raiseEvent(key, 'Test announcement', event)

function onResponse(message) {
console.log('[DEBUG] on response')
eventSubscriptionIdOnStatus = message.eventSubscriptionId
}

function onStatus(message) {
console.log('[DEBUG] on status')
eventsServerClient.stopListening(eventHandlerKey, eventSubscriptionIdOnStatus, node.id)

if (message.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
node.payload.uiObject.setInfoMessage('Twitter message sent.')
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
} 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 })
}
node.payload.uiObject.setErrorMessage('Could not send Twitter message. Error Response: ' + err.message)
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
}
}
}

function validations(node) {
let result = {}
console.log(node)
result.taskManager = node.payload.parentNode.payload.parentNode.payload.parentNode.payload.parentNode
result.lanNetworkNode = UI.projects.visualScripting.utilities.meshes.findNodeInNodeMesh(result.taskManager, 'LAN Network Node', undefined, true, false, true, false)

return result
}
}
2 changes: 1 addition & 1 deletion TaskServerRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ SA.nodeModules = {
ccxt: require('ccxt'),
ccxtMisc: require('./node_modules/ccxt/js/base/functions/misc'),
lookpath: require('lookpath'),
twitter: require('twitter')
twitter: require('twitter-v2')
}

run()
Expand Down
Loading

0 comments on commit 9a854b2

Please sign in to comment.