diff --git a/Projects/Superalgos/Icons-SVG-Sources/menu/test.svg b/Projects/Superalgos/Icons-SVG-Sources/menu/test.svg new file mode 100644 index 000000000..bff868d7a --- /dev/null +++ b/Projects/Superalgos/Icons-SVG-Sources/menu/test.svg @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Projects/Superalgos/Icons/test-entity.png b/Projects/Superalgos/Icons/test-entity.png new file mode 100644 index 000000000..c42733847 Binary files /dev/null and b/Projects/Superalgos/Icons/test-entity.png differ diff --git a/Projects/Superalgos/Schemas/AppSchema.json b/Projects/Superalgos/Schemas/AppSchema.json index a91037675..b28029cc8 100644 --- a/Projects/Superalgos/Schemas/AppSchema.json +++ b/Projects/Superalgos/Schemas/AppSchema.json @@ -14879,6 +14879,13 @@ "iconPathOn": "configuration", "iconPathOff": "configuration" }, + { + "action": "Send Telegram Test Message", + "label": "Send Telegram Test Message", + "iconPathOn": "test-entity", + "iconPathOff": "test-entity", + "actionFunction": "payload.executeAction" + }, { "action": "Delete UI Object", "askConfirmation": true, diff --git a/Projects/Superalgos/UI/Function-Libraries/ActionSwitch.js b/Projects/Superalgos/UI/Function-Libraries/ActionSwitch.js index da50856fe..5e2324136 100644 --- a/Projects/Superalgos/UI/Function-Libraries/ActionSwitch.js +++ b/Projects/Superalgos/UI/Function-Libraries/ActionSwitch.js @@ -712,6 +712,11 @@ function newSuperalgosActionSwitch() { UI.projects.superalgos.spaces.floatingSpace.uiObjectConstructor.createUiObject(true, action.node.payload) } break + case 'Send Telegram Test Message': + { + UI.projects.superalgos.functionLibraries.socialBotsFunctions.sendTelegramTestMessage(action.node, action.callBackFunction) + } + break } } } diff --git a/Projects/Superalgos/UI/Function-Libraries/SocialBotsFunctions.js b/Projects/Superalgos/UI/Function-Libraries/SocialBotsFunctions.js new file mode 100644 index 000000000..ad4be83fb --- /dev/null +++ b/Projects/Superalgos/UI/Function-Libraries/SocialBotsFunctions.js @@ -0,0 +1,73 @@ +function newSuperalgosFunctionLibrarySocialBotsFunctions() { + thisObject = { + sendTelegramTestMessage: sendTelegramTestMessage + } + + return thisObject + + function sendTelegramTestMessage(node, callBackFunction) { + + let botToken = UI.projects.superalgos.utilities.nodeConfig.loadPropertyFromNodeConfig(node.payload, 'botToken') + let chatId = UI.projects.superalgos.utilities.nodeConfig.loadPropertyFromNodeConfig(node.payload, 'chatId') + + if (botToken === undefined) { + node.payload.uiObject.setErrorMessage('botToken parameter not defined.') + callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE) + return + } + + if (chatId === undefined) { + node.payload.uiObject.setErrorMessage('chatId parameter not defined.') + callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE) + return + } + + let params = { + text: "Test Message from Superalgos", + chat_id: chatId + } + + httpRequestJSON(JSON.stringify(params),"https://api.telegram.org/bot" + botToken + "/sendMessage", onResponse) + + function onResponse(err) { + if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) { + node.payload.uiObject.setInfoMessage('Telegram message sent.') + callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE) + } else { + node.payload.uiObject.setErrorMessage('Could not send Telegram 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 }) + } + } + } + } +}