Example: Answering an incoming call

View as Markdown

For the complete documentation index, see llms.txt.

This example answers an inbound Voximplant call and bridges audio to OpenAI Realtime for speech‑to‑speech conversations.

Jump to the Full VoxEngine scenario.

Prerequisites

Session setup

The example configures the OpenAI Realtime session with a short system prompt, a voice, and server VAD:

Session setup
1voiceAIClient.sessionUpdate({
2 session: {
3 type: "realtime",
4 instructions: SYSTEM_PROMPT,
5 voice: "alloy",
6 output_modalities: ["audio"],
7 turn_detection: { type: "server_vad", interrupt_response: true },
8 },
9});

Connect call audio

Once the session is ready, bridge audio both ways between the call and OpenAI:

Connect call audio
1VoxEngine.sendMediaBetween(call, voiceAIClient);

Barge-in

Barge-in
1voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.InputAudioBufferSpeechStarted, () => {
2 voiceAIClient.clearMediaBuffer();
3});

Notes

See the VoxEngine API Reference for more details.

Full VoxEngine scenario

voxeengine-openai-answer-incoming-call.js
1/**
2 * Voximplant + OpenAI Realtime API connector demo
3 * Scenario: answer an incoming call and bridge it to OpenAI Realtime.
4 */
5
6require(Modules.OpenAI);
7const SYSTEM_PROMPT = `
8You are Voxi, a helpful voice assistant for phone callers.
9Keep responses short and telephony-friendly (usually 1-2 sentences).
10`;
11
12const SESSION_CONFIG = {
13 session: {
14 type: "realtime",
15 instructions: SYSTEM_PROMPT,
16 voice: "alloy",
17 output_modalities: ["audio"],
18 turn_detection: {type: "server_vad", interrupt_response: true},
19 },
20};
21
22VoxEngine.addEventListener(AppEvents.CallAlerting, async ({call}) => {
23 let voiceAIClient;
24
25 call.addEventListener(CallEvents.Disconnected, () => VoxEngine.terminate());
26 call.addEventListener(CallEvents.Failed, () => VoxEngine.terminate());
27
28 try {
29 call.answer();
30 // call.record({hd_audio: true, stereo: true}); // Optional: record the call
31
32 voiceAIClient = await OpenAI.createRealtimeAPIClient({
33 apiKey: VoxEngine.getSecretValue('OPENAI_API_KEY'),
34 model: "gpt-realtime-1.5",
35 onWebSocketClose: (event) => {
36 Logger.write("===OpenAI.WebSocket.Close===");
37 if (event) Logger.write(JSON.stringify(event));
38 VoxEngine.terminate();
39 },
40 });
41
42 //---------------------- Event handlers -----------------------
43 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.SessionCreated, () => {
44 voiceAIClient.sessionUpdate(SESSION_CONFIG);
45 });
46
47 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.SessionUpdated, () => {
48 VoxEngine.sendMediaBetween(call, voiceAIClient);
49 voiceAIClient.responseCreate({instructions: "Hello! How can I help today?"});
50 });
51
52 // Barge-in: clear buffered audio when the caller starts speaking
53 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.InputAudioBufferSpeechStarted, () => {
54 Logger.write("===BARGE-IN: OpenAI.InputAudioBufferSpeechStarted===");
55 voiceAIClient.clearMediaBuffer();
56 });
57
58 voiceAIClient.addEventListener(
59 OpenAI.RealtimeAPIEvents.ConversationItemInputAudioTranscriptionCompleted,
60 (event) => {
61 const payload = event?.data?.payload || event?.data || {};
62 const transcript = payload.transcript || payload.text || payload.delta;
63 if (transcript) Logger.write(`===USER=== ${transcript}`);
64 }
65 );
66
67 voiceAIClient.addEventListener(
68 OpenAI.RealtimeAPIEvents.ResponseOutputAudioTranscriptDelta,
69 (event) => {
70 const payload = event?.data?.payload || event?.data || {};
71 if (payload.delta) Logger.write(`===AGENT_DELTA=== ${payload.delta}`);
72 }
73 );
74
75 voiceAIClient.addEventListener(
76 OpenAI.RealtimeAPIEvents.ResponseOutputAudioTranscriptDone,
77 (event) => {
78 const payload = event?.data?.payload || event?.data || {};
79 const transcript = payload.transcript || payload.text;
80 if (transcript) Logger.write(`===AGENT=== ${transcript}`);
81 }
82 );
83
84 // Consolidated "log-only" handlers - key OpenAI/VoxEngine debugging events
85 [
86 OpenAI.RealtimeAPIEvents.ResponseCreated,
87 OpenAI.RealtimeAPIEvents.ResponseDone,
88 OpenAI.RealtimeAPIEvents.ResponseOutputAudioDone,
89 OpenAI.RealtimeAPIEvents.ConnectorInformation,
90 OpenAI.RealtimeAPIEvents.HTTPResponse,
91 OpenAI.RealtimeAPIEvents.WebSocketError,
92 OpenAI.RealtimeAPIEvents.Unknown,
93 OpenAI.Events.WebSocketMediaStarted,
94 OpenAI.Events.WebSocketMediaEnded,
95 ].forEach((eventName) => {
96 voiceAIClient.addEventListener(eventName, (event) => {
97 Logger.write(`===${event.name}===`);
98 if (event?.data) Logger.write(JSON.stringify(event.data));
99 });
100 });
101 } catch (error) {
102 Logger.write("===UNHANDLED_ERROR===");
103 Logger.write(error);
104 voiceAIClient?.close();
105 VoxEngine.terminate();
106 }
107});