Skip to content

Commit

Permalink
[EVOLUTION] start, status and better close commands, including from r…
Browse files Browse the repository at this point in the history
…c status
  • Loading branch information
dudanogueira committed Mar 8, 2024
1 parent 5a92791 commit 5818336
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compose/local/evolution/env
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LOG_BAILEYS=error
# Determine how long the instance should be deleted from memory in case of no connection.
# Default time: 5 minutes
# If you don't even want an expiration, enter the value false
DEL_INSTANCE=false
DEL_INSTANCE=1

# Temporary data storage
STORE_MESSAGES=true
Expand Down
60 changes: 57 additions & 3 deletions rocket_connect/plugins/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def initialize(self):

def status_session(self):
if self.config.get("endpoint", None):
output = {}
# GET {{baseUrl}}/instance/connectionState/{{instance}}
secret_key = self.config.get("secret_key")
instance_name = self.config.get("instance_name")
Expand All @@ -101,10 +102,29 @@ def status_session(self):
endpoint_webhook_response = requests.request(
"GET", endpoint_webhook_get_url, headers=headers
)
# fetch instances and filter for only this one
endpoint_fetchinstances_get_url = "{}/instance/fetchInstances".format(
self.config.get("endpoint")
)
endpoint_fetchinstances_response = requests.request(
"GET", endpoint_fetchinstances_get_url, headers=headers
)
if endpoint_fetchinstances_response.json():
instance = [
i for i in endpoint_fetchinstances_response.json()
if i.get("instance").get("instanceName") == "rocketchat_evolution_test"
]
if instance:
endpoint_fetchinstances_response = instance[0].get("instance")
else:
endpoint_fetchinstances_response = endpoint_fetchinstances_response.json()


if status_instance_response:
output = status_instance_response.json()
output["webhook"] = endpoint_webhook_response.json()
if endpoint_fetchinstances_response:
output["instance"] = endpoint_fetchinstances_response
return output
return {"error": "no endpoint"}
else:
Expand All @@ -121,7 +141,13 @@ def close_session(self):
status_instance_response = requests.request(
"DELETE", endpoint_status, headers=headers
)
return status_instance_response.ok
endpoint_status = "{}/instance/delete/{}".format(
self.config.get("endpoint"), instance_name
)
status_instance_response = requests.request(
"DELETE", endpoint_status, headers=headers
)
return {"success": status_instance_response.ok}


#
Expand Down Expand Up @@ -157,6 +183,32 @@ def get_message(self, message_id):
def incoming(self):
message = json.dumps(self.message)
self.logger_info(f"INCOMING MESSAGE: {message}")

if self.message.get("action"):
# check if session managemnt is active
if self.config.get("session_management_token"):
if self.message.get("session_management_token") != self.config.get(
"session_management_token"
):
output = {"success": False, "message": "INVALID TOKEN"}
return JsonResponse(output)
else:
action = self.message.get("action")
output = {"action": action}
if action == "start":
response = self.connector.initialize()
if action == "status":
response = self.connector.status_session()
if action == "close":
response = self.connector.close_session()
if action == "livechat":
response = self.livechat_manager(self.message)

# return status
output = {**output, **response}
self.logger_info(f"RETURN OF ACTION MESSAGE: {output}")
return JsonResponse(output)

#
# qrcode reading
#
Expand Down Expand Up @@ -616,13 +668,15 @@ class ConnectorConfigForm(BaseConnectorConfigForm):
enable_ack_receipt = forms.BooleanField(
required=False,
help_text="This will update the ingoing message to show it was delivered and received",
)
)
session_management_token = forms.CharField(required=False)

field_order = [
"webhook",
"endpoint",
"secret_key",
"instance_name",
"send_message_delay",
"enable_ack_receipt"
"enable_ack_receipt",
"session_management_token"
]
4 changes: 2 additions & 2 deletions rocket_connect/templates/instance/connector_analyze.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <h4 class="card-title">Connector Response</h4>
href="?connector=codechat&connector_action=status_session"
role="button">get
status</a>
{% if connector_action_response.status_session.status != "CONNECTED" %}
{% if connector_action_response.status_session.status != "CONNECTED" and connector_action_response.status_session.instance.status != "open" %}
<a name=""
id=""
class="btn btn-primary"
Expand All @@ -127,7 +127,7 @@ <h4 class="card-title">Connector Response</h4>
initialize
</a>
{% endif %}
{% if connector_action_response.status_session.state == "open" %}
{% if connector_action_response.status_session.state == "open" or connector_action_response.status_session.instance.status == "open" %}
<a name=""
id=""
class="btn btn-danger"
Expand Down

0 comments on commit 5818336

Please sign in to comment.