diff --git a/vapi_python/daily_call.py b/vapi_python/daily_call.py index 5dcc1cd..f4a7911 100644 --- a/vapi_python/daily_call.py +++ b/vapi_python/daily_call.py @@ -1,6 +1,7 @@ import daily import threading import pyaudio +import json SAMPLE_RATE = 16000 NUM_CHANNELS = 1 @@ -160,3 +161,15 @@ def receive_bot_audio(self): if len(buffer) > 0: self.__output_audio_stream.write(buffer, CHUNK_SIZE) + + def send_app_message(self, message): + """ + Send an application message to the assistant. + + :param message: The message to send (expects a dictionary). + """ + try: + serialized_message = json.dumps(message) + self.__call_client.send_app_message(serialized_message) + except Exception as e: + print(f"Failed to send app message: {e}") diff --git a/vapi_python/vapi_python.py b/vapi_python/vapi_python.py index e6f5ac8..69e9797 100644 --- a/vapi_python/vapi_python.py +++ b/vapi_python/vapi_python.py @@ -6,13 +6,13 @@ CHANNELS = 1 -def create_web_call(api_url, api_key, assistant): +def create_web_call(api_url, api_key, payload): url = f"{api_url}/call/web" headers = { 'Authorization': 'Bearer ' + api_key, 'Content-Type': 'application/json' } - response = requests.post(url, headers=headers, json=assistant) + response = requests.post(url, headers=headers, json=payload) data = response.json() if response.status_code == 201: call_id = data.get('id') @@ -27,15 +27,29 @@ def __init__(self, *, api_key, api_url="https://api.vapi.ai"): self.api_key = api_key self.api_url = api_url - def start(self, *, assistant_id=None, assistant=None, assistant_overrides=None): + def start( + self, + *, + assistant_id=None, + assistant=None, + assistant_overrides=None, + squad_id=None, + squad=None, + ): # Start a new call if assistant_id: - assistant = {'assistantId': assistant_id, 'assistantOverrides': assistant_overrides} + payload = {'assistantId': assistant_id, 'assistantOverrides': assistant_overrides} elif assistant: - assistant = {'assistant': assistant, 'assistantOverrides': assistant_overrides} + payload = {'assistant': assistant, 'assistantOverrides': assistant_overrides} + elif squad_id: + payload = {'squadId': squad_id} + elif squad: + payload = {'squad': squad} + else: + raise Exception("Error: No assistant specified.") call_id, web_call_url = create_web_call( - self.api_url, self.api_key, assistant) + self.api_url, self.api_key, payload) if not web_call_url: raise Exception("Error: Unable to create call.") @@ -48,3 +62,34 @@ def start(self, *, assistant_id=None, assistant=None, assistant_overrides=None): def stop(self): self.__client.leave() self.__client = None + + def send(self, message): + """ + Send a generic message to the assistant. + + :param message: A dictionary containing the message type and content. + """ + if not self.__client: + raise Exception("Call not started. Please start the call first.") + + # Check message format here instead of serialization + if not isinstance(message, dict) or 'type' not in message: + raise ValueError("Invalid message format.") + + try: + self.__client.send_app_message(message) # Send dictionary directly + except Exception as e: + print(f"Failed to send message: {e}") + + def add_message(self, role, content): + """ + method to send text messages with specific parameters. + """ + message = { + 'type': 'add-message', + 'message': { + 'role': role, + 'content': content + } + } + self.send(message)