diff --git a/rocket_connect/instance/management/commands/dev_settings.py b/rocket_connect/instance/management/commands/dev_settings.py index 5e26f37..de1bef6 100644 --- a/rocket_connect/instance/management/commands/dev_settings.py +++ b/rocket_connect/instance/management/commands/dev_settings.py @@ -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." @@ -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() @@ -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", @@ -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", diff --git a/rocket_connect/instance/models.py b/rocket_connect/instance/models.py index c2487d5..1b074e9 100644 --- a/rocket_connect/instance/models.py +++ b/rocket_connect/instance/models.py @@ -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} @@ -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( diff --git a/rocket_connect/plugins/base.py b/rocket_connect/plugins/base.py index c445c4f..21d7c33 100644 --- a/rocket_connect/plugins/base.py +++ b/rocket_connect/plugins/base.py @@ -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."