Skip to content

Commit

Permalink
feat: phone call worker
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Jan 5, 2024
1 parent 50309a9 commit 546901b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/server/models/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ChatModel(BaseModel):
agent_id = Column(UUID, ForeignKey("agent.id", ondelete="CASCADE"), index=True)
team_id = Column(UUID, ForeignKey("team.id", ondelete="CASCADE"), index=True)

campaign_id = Column(UUID, nullable=True, index=True)

voice_url = Column(String, nullable=True)

parent_id = Column(UUID, ForeignKey("chat.id", ondelete="CASCADE"), index=True)
Expand Down
2 changes: 2 additions & 0 deletions apps/worker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ REDIS_URL=redis://redis:6379/0
DEV_SERVER_URL=https://api-dev.l3vels.xyz
PROD_SERVER_URL=https://api.l3vels.xyz

PR_DEV_SERVER_URL=https://api-pr-dev.l3agi.com

SERVER_AUTH_TOKEN=secret
2 changes: 2 additions & 0 deletions apps/worker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ class Config:

SERVER_URL = os.environ.get("SERVER_URL")

PR_DEV_SERVER_URL = os.environ.get("PR_DEV_SERVER_URL")

SERVER_AUTH_TOKEN = os.environ.get("SERVER_AUTH_TOKEN")
69 changes: 69 additions & 0 deletions apps/worker/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from datetime import timedelta

import requests
Expand All @@ -21,6 +22,10 @@
"task": "register-fine-tuning-tasks",
"schedule": timedelta(minutes=2),
},
"register-campaign-phone-call-tasks": {
"task": "register-campaign-phone-call-tasks",
"schedule": timedelta(minutes=2),
},
}


Expand Down Expand Up @@ -98,5 +103,69 @@ def check_single_fine_tuning_task(fine_tuning_id: str):
return res.json()


@app.task(
name="register-campaign-phone-call-tasks",
autoretry_for=(Exception,),
retry_backoff=2,
max_retries=5,
)
def register_campaign_phone_call_tasks():
res = requests.get(
f"{Config.PR_DEV_SERVER_URL}/campaign/due",
headers={"Authorization": f"Bearer {Config.SERVER_AUTH_TOKEN}"},
)

campaigns = res.json()
campaign_ids = [campaign["id"] for campaign in campaigns]

for campaign in campaigns:
res = requests.post(
f"{Config.PR_DEV_SERVER_URL}/campaign/{campaign['id']}/start",
headers={"Authorization": f"Bearer {Config.SERVER_AUTH_TOKEN}"},
)

contact_ids = res.json()

for contact_id in contact_ids:
make_phone_call.apply_async(args=[campaign["id"], contact_id])

return campaign_ids


@app.task(
name="make-phone-call",
autoretry_for=(Exception,),
retry_backoff=2,
max_retries=5,
)
def make_phone_call(campaign_id: str, contact_id: str):
res = requests.post(
f"{Config.PR_DEV_SERVER_URL}/call/campaign",
headers={"Authorization": f"Bearer {Config.SERVER_AUTH_TOKEN}"},
data={
"campaign_id": campaign_id,
"contact_id": contact_id,
},
)

call_id = res.json().get("call_id")

# Checks for the status of the phone call to make sure phone call is finished
while True:
status_res = requests.get(
f"{Config.PR_DEV_SERVER_URL}/call/{call_id}",
headers={"Authorization": f"Bearer {Config.SERVER_AUTH_TOKEN}"},
)

status = status_res.json().get("status")

if status is not None:
break

time.sleep(15)

return "Phone call finished"


if __name__ == "__main__":
app.start()

0 comments on commit 546901b

Please sign in to comment.