Skip to content

Commit

Permalink
Added Task 7: Manage Abandoned Calls
Browse files Browse the repository at this point in the history
Added button to easily add/update default tasks.
  • Loading branch information
dudanogueira committed Apr 21, 2023
1 parent 309e0af commit dad8a12
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 20 deletions.
37 changes: 18 additions & 19 deletions local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ services:


rocketchat:
image: registry.rocket.chat/rocketchat/rocket.chat:${RELEASE:-6.1.0}
image: registry.rocket.chat/rocketchat/rocket.chat:${RELEASE:-develop}
restart: on-failure
environment:
MONGO_URL: "${MONGO_URL:-\
Expand Down Expand Up @@ -197,24 +197,23 @@ services:
ports:
- "21465:21465"

quepasa:
image: sufficit/quepasa
mem_limit: 4096M
ports:
- 31000:31000
extra_hosts:
- "host.docker.internal:host-gateway"
restart: always
stdin_open: true
tty: true
environment:
- WEBSOCKETSSL=false
- WEBAPIPORT=31000
- APP_ENV=production
- MIGRATIONS=/opt/quepasa/migrations
- DEBUGJSONMESSAGES=false
- HTTPLOGS=false

# quepasa:
# image: sufficit/quepasa
# mem_limit: 4096M
# ports:
# - 31000:31000
# extra_hosts:
# - "host.docker.internal:host-gateway"
# restart: always
# stdin_open: true
# tty: true
# environment:
# - WEBSOCKETSSL=false
# - WEBAPIPORT=31000
# - APP_ENV=production
# - MIGRATIONS=/opt/quepasa/migrations
# - DEBUGJSONMESSAGES=false
# - HTTPLOGS=false

apache:
image: 'php:apache'
Expand Down
31 changes: 31 additions & 0 deletions rocket_connect/instance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,37 @@ def install_server_tasks(self):
)
self.tasks.add(task)
added_tasks.append(task)
#
# T7 manage_abandoned_chats
#
task = PeriodicTask.objects.filter(
task="instance.tasks.manage_abandoned_chats",
kwargs__contains=self.external_token,
)
if not task.exists():
crontab = CrontabSchedule.objects.first()
task = PeriodicTask.objects.create(
enabled=False,
name=f"Manage Abandoned Calls for {self.name} (ID {self.id})",
description="""This task can transfer to department or agent"""
+ """, close or just alert the user on abandoned chats""",
crontab=crontab,
task="instance.tasks.manage_abandoned_chats",
kwargs=json.dumps(
{
"server_token": self.external_token,
"excluded_departments": [],
"message_template": "This chat is abandoned",
"last_message_seconds": 10,
"last_message_users": "*",
"action": "transfer|close|alert",
"target_department_id": None,
"target_agent_user_id": None,
}
),
)
self.tasks.add(task)
added_tasks.append(task)
# return added tasks
return added_tasks

Expand Down
83 changes: 83 additions & 0 deletions rocket_connect/instance/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,86 @@ def alert_undelivered_messages(

responses = {"targets": targets, "sent_messages": sent_messages}
return responses


# T7
@celery_app.task(retry_kwargs={"max_retries": 7, "countdown": 5})
def manage_abandoned_chats(
server_token,
excluded_departments,
message_template,
last_message_seconds,
last_message_users,
action="close",
target_department_id=None,
target_agent_user_id=None,
):
# get server
server = Server.objects.get(external_token=server_token)
# get rocket
rocket = server.get_rocket_client()
# list all open messages
open_rooms = server.get_open_rooms()
# create returns
output = {"action": action, "rooms": []}

if open_rooms:
for room in open_rooms.get("rooms"):
# do not close for configured rooms
if room.get("departmentId") not in excluded_departments:
if room.get("lastMessage", False):
# get last message
last_message = room["lastMessage"]
# define last message users or all
if (
last_message["u"]["username"] in last_message_users
or last_message_users == "*"
):
ts = dateutil.parser.parse(last_message["ts"])
now = timezone.now()
delta = now - ts
if delta.total_seconds() >= last_message_seconds:
if message_template:
rocket.chat_post_message(
room_id=room["_id"], text=message_template
).json()
if action == "close":
# close messages on this situation
room_close_options = {
"rid": room["_id"],
"token": room["v"]["token"],
}
close = rocket.call_api_post(
"livechat/room.close", **room_close_options
)
output["rooms"].append(close.json())
elif action == "transfer":
if target_department_id and not target_agent_user_id:
# transfer messages on this situation to department
room_transfer_options = {
"rid": room["_id"],
"token": room["v"]["token"],
"department": target_department_id,
}
transfer = rocket.call_api_post(
"livechat/room.transfer",
**room_transfer_options
)
output["rooms"].append(transfer.json())
if target_agent_user_id and not target_department_id:
# forward messages on this situation to agent
room_transfer_options = {
"roomId": room["_id"],
"userId": target_agent_user_id,
}
transfer = rocket.call_api_post(
"livechat/room.forward", **room_transfer_options
)
output["rooms"].append(
{
"room_id": room["_id"],
"response": transfer.json(),
}
)

return output
6 changes: 5 additions & 1 deletion rocket_connect/templates/instance/server_detail_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ <h1>

{% endif %}


{% if delivered_messages_to_delete %}
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
Expand Down Expand Up @@ -138,6 +137,11 @@ <h2>
</div>
<!--- TASKS -->
<div class="tab-pane fade" id="tasks" role="tabpanel" aria-labelledby="tasks-tab">

<div class="text-right my-4 ">
<a name="" id="" class="btn btn-primary " href="?install-default-tasks=1" role="button">install tasks</a>
</div>

{% for task in tasks.all %}
<div class="card mb-3">
<div class="card-header">
Expand Down

0 comments on commit dad8a12

Please sign in to comment.