Skip to content

Commit

Permalink
[IMPROVE] Optional Connector status in session status
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanogueira committed Feb 11, 2022
1 parent 33ceaa0 commit 0078fee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 70 deletions.
73 changes: 5 additions & 68 deletions rocket_connect/instance/management/commands/dev_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.core.management.base import BaseCommand
from instance.models import Server

wpp_admin_script = """// Check gist https://gist.github.com/dudanogueira/ae8e92c5071b750de405546980eba7dc"""


class Command(BaseCommand):
help = "My shiny new management command."
Expand Down Expand Up @@ -69,6 +71,7 @@ def handle_django(self):
"outcome_message_with_quoted_message": False,
"outcome_attachment_description_as_new_message": False,
"supress_visitor_name": False,
"include_connector_status": True,
"force_close_message": "This is a default closing message per connector.",
}
connector.save()
Expand Down Expand Up @@ -114,6 +117,7 @@ def handle_django(self):
"endpoint": "http://wppconnect:21465",
"secret_key": "My53cr3tKY",
"instance_name": "test",
"include_connector_status": True,
"outcome_attachment_description_as_new_message": True,
"active_chat_webhook_integration_token": "WPP_ZAPIT_TOKEN",
"session_management_token": "session_management_secret_token",
Expand Down Expand Up @@ -350,74 +354,7 @@ def handle_rocketchat(self):
"username": "rocket.cat",
"urls": ["http://django:8000/connector/WPP_EXTERNAL_TOKEN/"],
"scriptEnabled": True,
"script": """
class Script {
/**
* @params {object} request
*/
prepare_outgoing_request({ request }) {
let match;
// match a allowed commands
match = request.data.text.match(/^rc\s(status|start|close)/);
if (match) {
return {
url: request.url,
headers: request.headers,
method: 'POST',
data:{
session_management_token: request.data.token,
action: request.data.text.split(" ")[1]
}
};
}else{
// Prevent the request and return a help message
return {
message: {
text: [
'**commands**',
'```',
' rc [status|start|close]',
'```'
].join('\n')
}
};
}
}
process_outgoing_response({ request, response }) {
const 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.wid.user)
lines.push(":battery: BATTERY " + obj.host_device.battery + "%")
lines.push(":electric_plug: Connected to Plug? " + obj.host_device.plugged)
lines.push(":computer: Platform: " + obj.host_device.platform)
}else{
lines.push(":red_circle: STATUS " + obj.status)
}
}
}else{
lines.push(":red_circle: Error while executing " + obj.action + " : " + response.error)
}
return {
content: {
text: lines.join('\n'),
parseUrls: false
}
};
}
}
""", # noqa
"script": wpp_admin_script,
"channel": "#manager_channel",
"triggerWords": [
"rc",
Expand Down
25 changes: 24 additions & 1 deletion rocket_connect/instance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ def status_session(self, request=None):
connector = Connector(self, {}, "incoming")
# return initialize result
try:
return connector.status_session()
# return informations from the connector
status = connector.status_session()
if self.config.get("include_connector_status"):
status["connector"] = self.connector_status()
return status
except requests.ConnectionError:
return {"success": False}

Expand Down Expand Up @@ -323,6 +327,25 @@ def force_delivery(self):
for message in messages:
message.force_delivery()

def connector_status(self):
"""
this method will return the status of the connector
"""
return self.messages.aggregate(
undelivered_messages=models.Count(
"id",
models.Q(delivered=False) | models.Q(delivered=None),
distinct=True,
),
total_messages=models.Count("id", distinct=True),
open_rooms=models.Count(
"room__id", models.Q(room__open=True), distinct=True
),
total_rooms=models.Count("room__id", distinct=True),
last_message=models.Max("created"),
total_visitors=models.Count("room__token", distinct=True),
)

uuid = models.UUIDField(default=uuid.uuid4, editable=False)
external_token = models.CharField(max_length=50, default=random_string, unique=True)
name = models.CharField(
Expand Down
5 changes: 4 additions & 1 deletion rocket_connect/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,10 @@ def save(self):
required=False,
help_text="do not overwrite visitor name with connector visitor name",
)

include_connector_status = forms.BooleanField(
required=False,
help_text="Includes connector status in the status payload. Disable for better performance",
)
alert_agent_of_automated_message_sent = forms.BooleanField(
required=False,
help_text="Alert the agent whenever you send an automated text."
Expand Down

0 comments on commit 0078fee

Please sign in to comment.