Skip to content

Commit

Permalink
improvements to TransferCall action
Browse files Browse the repository at this point in the history
* move to_phone to be a parameter (instead of in the config) so you can dynamically adjust who to send to
* use proper Twilio rest sdk rather than raw http calls
  • Loading branch information
rjheeta committed May 16, 2024
1 parent ab6a8ad commit 7f925f2
Showing 1 changed file with 9 additions and 33 deletions.
42 changes: 9 additions & 33 deletions vocode/streaming/action/transfer_call.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os
import aiohttp

from aiohttp import BasicAuth
from typing import Type
from pydantic.v1 import BaseModel, Field
from twilio.rest import Client

from vocode.streaming.action.phone_call_action import TwilioPhoneCallAction
from vocode.streaming.models.actions import (
Expand All @@ -13,61 +11,39 @@
ActionType,
)


class TransferCallActionConfig(ActionConfig, type=ActionType.TRANSFER_CALL):
to_phone: str


class TransferCallParameters(BaseModel):
pass

class TransferCallParameters(BaseModel):
to_phone: str = Field(..., description="phone number to transfer the call to")

class TransferCallResponse(BaseModel):
status: str = Field("success", description="status of the transfer")


class TransferCall(
TwilioPhoneCallAction[
TransferCallActionConfig, TransferCallParameters, TransferCallResponse
]
):
description: str = (
"transfers the call. use when you need to connect the active call to another phone line."
)
description: str = "transfers the call. use when you need to connect the active call to another phone line."
parameters_type: Type[TransferCallParameters] = TransferCallParameters
response_type: Type[TransferCallResponse] = TransferCallResponse

async def transfer_call(self, twilio_call_sid, to_phone):
twilio_account_sid = os.environ["TWILIO_ACCOUNT_SID"]
twilio_auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(twilio_account_sid, twilio_auth_token)

url = "https://api.twilio.com/2010-04-01/Accounts/{twilio_account_sid}/Calls/{twilio_auth_token}.json".format(
twilio_account_sid=twilio_account_sid, twilio_auth_token=twilio_call_sid
)

twiml_data = "<Response><Dial>{to_phone}</Dial></Response>".format(
to_phone=to_phone
)

payload = {"Twiml": twiml_data}
twiml = f"<Response><Dial>{to_phone}</Dial></Response>"
call = client.calls(twilio_call_sid).update(twiml=twiml)

auth = BasicAuth(twilio_account_sid, twilio_auth_token)

async with aiohttp.ClientSession(auth=auth) as session:
async with session.post(url, data=payload) as response:
if response.status != 200:
print(await response.text())
raise Exception("failed to update call")
else:
return await response.json()
return call

async def run(
self, action_input: ActionInput[TransferCallParameters]
) -> ActionOutput[TransferCallResponse]:
twilio_call_sid = self.get_twilio_sid(action_input)

await self.transfer_call(twilio_call_sid, self.action_config.to_phone)

await self.transfer_call(twilio_call_sid, action_input.params.to_phone)
return ActionOutput(
action_type=action_input.action_config.type,
response=TransferCallResponse(status="success"),
Expand Down

0 comments on commit 7f925f2

Please sign in to comment.