Skip to content

Instantly share code, notes, and snippets.

@dudanogueira
Last active August 19, 2022 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudanogueira/ae8e92c5071b750de405546980eba7dc to your computer and use it in GitHub Desktop.
Save dudanogueira/ae8e92c5071b750de405546980eba7dc to your computer and use it in GitHub Desktop.
An Outgoing webhook script to integrate with Rocket.Connect from within Rocket.Chat
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.status == "CONNECTED") {
lines.push(":white_check_mark: STATUS " + obj.status)
lines.push(":iphone: CONNECTED NUMBER: " + obj.host_device.phoneNumber)
if(obj.host_device.battery){
lines.push(":battery: BATTERY " + obj.host_device.battery + "%")
lines.push(":electric_plug: Connected to Plug? " + obj.host_device.plugged)
}else{
lines.push(":dna: MD/Beta Mode")
}
lines.push(":computer: Platform: " + obj.host_device.platform)
} else {
lines.push(":red_circle: STATUS " + obj.status)
}
lines.push(":popcorn: SESSION NAME " + obj.session)
lines.push(":dart: WPPCONNECT VERSION " + obj.version)
}
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