Last active
March 16, 2024 19:39
-
-
Save dudanogueira/965e9e3e9a2c27cea66ddfa75e97c8ed to your computer and use it in GitHub Desktop.
Rocket.Connect.Management.Webhook.Script.Evolution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Script { | |
/** | |
* @params {object} request | |
*/ | |
prepare_outgoing_request({ request }) { | |
let match; | |
// match a allowed commands | |
match = '^'+ request.data.trigger_word + '\\s(status|start|close|livechat)' | |
var re = new RegExp(match, 'g'); | |
match = request.data.text.match(re); | |
if (match) { | |
return { | |
url: request.url, | |
headers: request.headers, | |
method: 'POST', | |
data: { | |
session_management_token: request.data.token, | |
action: request.data.text.split(" ")[1], | |
text: request.data.text | |
} | |
}; | |
} else { | |
// Prevent the request and return a help message | |
return { | |
message: { | |
text: [ | |
'**commands**', | |
'```', | |
request.data.trigger_word + ' [status|start|close|livechat]', | |
'```', | |
'status - Bring Connector Status', | |
'start - Initiate Connector', | |
'close - Close Connector', | |
'', | |
//'Livechat - Manage livechat. Examples: ', | |
//request.data.trigger_word + ' livechat forward 30 alice Consultas - This will *forward* all messages created 30+ minutes ago with the user alice to department Consultas', | |
//request.data.trigger_word + ' livechat close 30 alice - This will *close* all messages created 30+ minutes ago served by the user alice', | |
//request.data.trigger_word + ' livechat close 30 - This will *close* all messages created 30+ minutes ago,' | |
].join('\n') | |
} | |
}; | |
} | |
} | |
process_outgoing_response({ request, response }) { | |
obj = response.content; | |
if (typeof(response.content) == "string"){ | |
obj = JSON.parse(response.content); | |
} | |
lines = [] | |
if (!response.error) { | |
lines.push(":arrow_right: " + obj.action + " executed succesfully") | |
if (obj.action == "status") { | |
if (obj.instance.status == "open"){ | |
lines.push(":white_check_mark: STATUS " + obj.instance.status || obj.status) | |
}else{ | |
lines.push(":red_circle: STATUS " + obj.instance.status || obj.status) | |
} | |
lines.push(":admission_tickets: OWNER " + obj.instance.owner || obj.host_device.phoneNumber) | |
lines.push(":popcorn: SESSION NAME " + obj.instance.instanceName) | |
} | |
if (obj.action == "livechat"){ | |
lines.push("Livechat return: " + obj.message) | |
} | |
} else { | |
lines.push(":red_circle: Error while executing " + obj.action + " : " + response.error) | |
} | |
return { | |
content: { | |
text: lines.join('\n'), | |
parseUrls: false | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment