Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transfer call #292

Merged
merged 6 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions vocode/streaming/action/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
NylasSendEmailActionConfig,
)
from vocode.streaming.models.actions import ActionConfig
from vocode.streaming.action.transfer_call import TransferCall, TransferCallActionConfig


class ActionFactory:
def create_action(self, action_config: ActionConfig) -> BaseAction:
if isinstance(action_config, NylasSendEmailActionConfig):
return NylasSendEmail(action_config, should_respond=True)
elif isinstance(action_config, TransferCallActionConfig):
return TransferCall(action_config)
else:
raise Exception("Invalid action type")
72 changes: 72 additions & 0 deletions vocode/streaming/action/transfer_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import aiohttp

from aiohttp import BasicAuth
from typing import Type
from pydantic import BaseModel, Field

from vocode.streaming.action.phone_call_action import TwilioPhoneCallAction
from vocode.streaming.models.actions import (
ActionConfig,
ActionInput,
ActionOutput,
ActionType,
)


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


class TransferCallParameters(BaseModel):
pass


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."
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"]

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}

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()

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)

return ActionOutput(
action_type=action_input.action_config.type,
response=TransferCallResponse(status="success"),
)
2 changes: 2 additions & 0 deletions vocode/streaming/models/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class ActionType(str, Enum):
BASE = "action_base"
NYLAS_SEND_EMAIL = "action_nylas_send_email"
TRANSFER_CALL = "action_transfer_call"


class ActionConfig(TypedModel, type=ActionType.BASE):
Expand All @@ -27,6 +28,7 @@ class Config:
arbitrary_types_allowed = True



class FunctionFragment(BaseModel):
name: str
arguments: str
Expand Down
18 changes: 12 additions & 6 deletions vocode/streaming/telephony/conversation/twilio_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ async def attach_ws_and_start(self, ws: WebSocket):
twilio_call = twilio_call_ref.fetch()

if self.twilio_config.record:
recordings_create_params = self.twilio_config.extra_params.get("recordings_create_params") if self.twilio_config.extra_params else None
recording = twilio_call_ref.recordings.create(
**recordings_create_params
) if recordings_create_params else twilio_call_ref.recordings.create()
recordings_create_params = (
self.twilio_config.extra_params.get("recordings_create_params")
if self.twilio_config.extra_params
else None
)
recording = (
twilio_call_ref.recordings.create(**recordings_create_params)
if recordings_create_params
else twilio_call_ref.recordings.create()
)
self.logger.info(f"Recording: {recording.sid}")

if twilio_call.answered_by in ("machine_start", "fax"):
Expand Down Expand Up @@ -156,5 +162,5 @@ async def handle_ws_message(self, message) -> Optional[PhoneCallWebsocketAction]

def mark_terminated(self):
super().mark_terminated()
asyncio.create_task(self.telephony_client.end_call(self.twilio_sid))

if self.active is False:
asyncio.create_task(self.telephony_client.end_call(self.twilio_sid))
Loading