From 2893ba9780cebb7d6b746c607b90143f9cd42d10 Mon Sep 17 00:00:00 2001 From: Ajay Raj Date: Sat, 4 Mar 2023 13:59:33 -0800 Subject: [PATCH] inbound calls --- simple_conversation.py | 7 +---- simple_inbound_call_server.py | 8 +++++ simple_outbound_call.py | 8 ++--- user_implemented_agent.py | 2 +- vocode/models/telephony.py | 10 ++++-- vocode/telephony/inbound_call_server.py | 35 +++++++++++++++++++++ vocode/{ => telephony}/outbound_call.py | 6 ++-- vocode/user_implemented_agent/base_agent.py | 2 +- 8 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 simple_inbound_call_server.py create mode 100644 vocode/telephony/inbound_call_server.py rename vocode/{ => telephony}/outbound_call.py (82%) diff --git a/simple_conversation.py b/simple_conversation.py index 1a33a89..d4b0119 100644 --- a/simple_conversation.py +++ b/simple_conversation.py @@ -20,12 +20,7 @@ if __name__ == "__main__": input_device=microphone_input, output_device=speaker_output, transcriber_config=DeepgramTranscriberConfig.from_input_device(microphone_input), - agent_config=WebSocketUserImplementedAgentConfig( - initial_message="Hello!", - respond=WebSocketUserImplementedAgentConfig.RouteConfig( - url="ws://localhost:3001/respond" - ) - ), + agent_config=EchoAgentConfig(initial_message="Hello!"), synthesizer_config=AzureSynthesizerConfig.from_output_device(speaker_output) ) signal.signal(signal.SIGINT, lambda _0, _1: conversation.deactivate()) diff --git a/simple_inbound_call_server.py b/simple_inbound_call_server.py new file mode 100644 index 0000000..20fdfaf --- /dev/null +++ b/simple_inbound_call_server.py @@ -0,0 +1,8 @@ +from vocode.telephony.inbound_call_server import InboundCallServer +from vocode.models.agent import EchoAgentConfig + +if __name__ == '__main__': + server = InboundCallServer( + agent_config=EchoAgentConfig(initial_message="hello!") + ) + server.run(port=3001) \ No newline at end of file diff --git a/simple_outbound_call.py b/simple_outbound_call.py index 59038d5..d146701 100644 --- a/simple_outbound_call.py +++ b/simple_outbound_call.py @@ -1,4 +1,4 @@ -from vocode.outbound_call import OutboundCall +from vocode.telephony.outbound_call import OutboundCall from vocode.models.telephony import CallEntity from vocode.models.agent import EchoAgentConfig, WebSocketUserImplementedAgentConfig @@ -10,10 +10,6 @@ if __name__ == '__main__': caller=CallEntity( phone_number="+14086600744", ), - agent_config=WebSocketUserImplementedAgentConfig( - respond=WebSocketUserImplementedAgentConfig.RouteConfig( - url="ws://localhost:3001/respond" - ) - ) + agent_config=EchoAgentConfig(initial_message="Hello!") ) call.start() \ No newline at end of file diff --git a/user_implemented_agent.py b/user_implemented_agent.py index 710e0ea..71d2429 100644 --- a/user_implemented_agent.py +++ b/user_implemented_agent.py @@ -24,4 +24,4 @@ class TestWebSocketAgent(WebSocketAgent): if __name__ == "__main__": agent = TestWebSocketAgent() - agent.run(port=3001) + agent.run(port=3000) diff --git a/vocode/models/telephony.py b/vocode/models/telephony.py index cc5bfd9..a1a0a4d 100644 --- a/vocode/models/telephony.py +++ b/vocode/models/telephony.py @@ -1,11 +1,15 @@ from vocode.models.model import BaseModel -from vocode.models.agent import AgentConfig, InformationRetrievalAgentConfig +from vocode.models.agent import AgentConfig class CallEntity(BaseModel): phone_number: str -class CreateCallRequest(BaseModel): +class CreateInboundCall(BaseModel): + agent_config: AgentConfig + twilio_sid: str + +class CreateOutboundCall(BaseModel): recipient: CallEntity caller: CallEntity - agent_config: AgentConfig + agent_config: AgentConfig # TODO add IVR/etc. diff --git a/vocode/telephony/inbound_call_server.py b/vocode/telephony/inbound_call_server.py new file mode 100644 index 0000000..cee19ec --- /dev/null +++ b/vocode/telephony/inbound_call_server.py @@ -0,0 +1,35 @@ +from fastapi import FastAPI, Response, Form +import requests +import uvicorn +from .. import api_key, BASE_URL + +from ..models.agent import AgentConfig +from ..models.telephony import CreateInboundCall + +VOCODE_INBOUND_CALL_URL = f"https://{BASE_URL}/create_inbound_call" + +class InboundCallServer(): + + def __init__(self, agent_config: AgentConfig): + self.agent_config = agent_config + self.app = FastAPI() + self.app.post("/vocode")(self.handle_call) + + async def handle_call(self, twilio_sid: str = Form(alias='CallSid')): + response = requests.post( + VOCODE_INBOUND_CALL_URL, + headers={ + "Authorization": f"Bearer {api_key}" + }, + json=CreateInboundCall( + agent_config=self.agent_config, + twilio_sid=twilio_sid + ).dict() + ) + return Response( + response.text, + media_type="application/xml", + ) + + def run(self, host="localhost", port=3000): + uvicorn.run(self.app, host=host, port=port) \ No newline at end of file diff --git a/vocode/outbound_call.py b/vocode/telephony/outbound_call.py similarity index 82% rename from vocode/outbound_call.py rename to vocode/telephony/outbound_call.py index 21b5278..72e72e8 100644 --- a/vocode/outbound_call.py +++ b/vocode/telephony/outbound_call.py @@ -1,6 +1,6 @@ -from .models.telephony import CallEntity, CreateCallRequest +from ..models.telephony import CallEntity, CreateOutboundCall import requests -from . import api_key, BASE_URL +from .. import api_key, BASE_URL VOCODE_OUTBOUND_CALL_URL = f"https://{BASE_URL}/create_outbound_call" @@ -17,7 +17,7 @@ class OutboundCall: headers={ "Authorization": f"Bearer {api_key}" }, - json=CreateCallRequest( + json=CreateOutboundCall( recipient=self.recipient, caller=self.caller, agent_config=self.agent_config diff --git a/vocode/user_implemented_agent/base_agent.py b/vocode/user_implemented_agent/base_agent.py index 3a08a4f..4ddee94 100644 --- a/vocode/user_implemented_agent/base_agent.py +++ b/vocode/user_implemented_agent/base_agent.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, APIRouter +from fastapi import FastAPI import uvicorn class BaseAgent():