@jambonz/sdk 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +721 -0
- package/examples/agent/ws-app.ts +138 -0
- package/examples/assemblyai-autogenerate-prompt/app.js +92 -0
- package/examples/assemblyai-autogenerate-prompt/package.json +14 -0
- package/examples/bedrock-agent.ts +267 -0
- package/examples/call-recording/README.md +24 -0
- package/examples/call-recording/app.ts +70 -0
- package/examples/call-recording/ws-app.ts +47 -0
- package/examples/deepgram-voice-agent/ws-app.ts +118 -0
- package/examples/dial/app.ts +22 -0
- package/examples/echo/app.ts +65 -0
- package/examples/echo/ws-app.ts +67 -0
- package/examples/elevenlabs-voice-agent/ws-app.ts +81 -0
- package/examples/hello-world/README.md +15 -0
- package/examples/hello-world/app.js +20 -0
- package/examples/hello-world/app.ts +19 -0
- package/examples/hello-world/ws-app.ts +18 -0
- package/examples/ivr-menu/README.md +15 -0
- package/examples/ivr-menu/app.js +85 -0
- package/examples/ivr-menu/app.ts +70 -0
- package/examples/listen-record/app.ts +22 -0
- package/examples/llm-streaming/ws-app.ts +96 -0
- package/examples/openai-realtime/ws-app.ts +131 -0
- package/examples/queue-with-hold/README.md +19 -0
- package/examples/queue-with-hold/app.ts +68 -0
- package/examples/queue-with-hold/ws-app.ts +41 -0
- package/examples/realtime-translator/src/app.ts +21 -0
- package/examples/realtime-translator/src/routes/translator.ts +108 -0
- package/examples/realtime-translator/src/utils/config.ts +21 -0
- package/examples/realtime-translator/src/utils/translate.ts +23 -0
- package/examples/retell-hosted/src/app.ts +22 -0
- package/examples/retell-hosted/src/config.ts +53 -0
- package/examples/retell-hosted/src/retell.ts +166 -0
- package/examples/two-stage-dialer/app.ts +96 -0
- package/examples/ultravox-voice-agent/ws-app.ts +116 -0
- package/examples/voice-agent/README.md +19 -0
- package/examples/voice-agent/app.js +133 -0
- package/examples/voice-agent/app.ts +132 -0
- package/examples/voice-agent/ws-app.ts +137 -0
- package/package.json +9 -4
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import { createEndpoint } from '@jambonz/sdk/websocket';
|
|
3
|
+
|
|
4
|
+
const server = http.createServer();
|
|
5
|
+
const makeService = createEndpoint({
|
|
6
|
+
server,
|
|
7
|
+
port: 3000,
|
|
8
|
+
envVars: {
|
|
9
|
+
DEEPGRAM_API_KEY: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: 'Deepgram API key for Voice Agent',
|
|
12
|
+
required: true,
|
|
13
|
+
obscure: true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const svc = makeService({ path: '/deepgram-s2s' });
|
|
19
|
+
|
|
20
|
+
svc.on('session:new', (session) => {
|
|
21
|
+
console.log(`Incoming call: ${session.callSid}`);
|
|
22
|
+
console.log(' from: %s to: %s', session.data.from, session.data.to);
|
|
23
|
+
console.log(' env_vars:', JSON.stringify(session.data.env_vars || {}));
|
|
24
|
+
|
|
25
|
+
const apiKey = session.data.env_vars?.DEEPGRAM_API_KEY;
|
|
26
|
+
if (!apiKey) {
|
|
27
|
+
console.error('ERROR: DEEPGRAM_API_KEY not found in application environment variables');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
session
|
|
32
|
+
.on('/event', (_evt: Record<string, any>) => {
|
|
33
|
+
// Deepgram voice agent events
|
|
34
|
+
})
|
|
35
|
+
.on('/toolCall', async (evt: Record<string, any>) => {
|
|
36
|
+
const { name, args, tool_call_id } = evt;
|
|
37
|
+
|
|
38
|
+
if (name === 'get_weather') {
|
|
39
|
+
try {
|
|
40
|
+
const geoRes = await fetch(
|
|
41
|
+
`https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(args.location)}&count=1&language=en&format=json`
|
|
42
|
+
);
|
|
43
|
+
const geoData = await geoRes.json() as any;
|
|
44
|
+
if (!geoData.results?.length) throw new Error('location_not_found');
|
|
45
|
+
|
|
46
|
+
const { latitude: lat, longitude: lng } = geoData.results[0];
|
|
47
|
+
const wxRes = await fetch(
|
|
48
|
+
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}¤t=temperature_2m,wind_speed_10m&temperature_unit=${args.scale}`
|
|
49
|
+
);
|
|
50
|
+
const weather = await wxRes.json();
|
|
51
|
+
|
|
52
|
+
session.sendToolOutput(tool_call_id, {
|
|
53
|
+
type: 'FunctionCallResponse',
|
|
54
|
+
id: tool_call_id,
|
|
55
|
+
name,
|
|
56
|
+
content: JSON.stringify(weather),
|
|
57
|
+
});
|
|
58
|
+
} catch (err) {
|
|
59
|
+
session.sendToolOutput(tool_call_id, { error: String(err) });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
.on('/final', (evt: Record<string, any>) => {
|
|
64
|
+
if (['server failure', 'server error'].includes(evt.completion_reason)) {
|
|
65
|
+
session.say({ text: 'Sorry, there was an error processing your request.' }).hangup();
|
|
66
|
+
}
|
|
67
|
+
session.reply();
|
|
68
|
+
})
|
|
69
|
+
.on('close', (code: number) => {
|
|
70
|
+
console.log(`Session ${session.callSid} closed: ${code}`);
|
|
71
|
+
})
|
|
72
|
+
.on('error', (err: Error) => {
|
|
73
|
+
console.error(`Session ${session.callSid} error:`, err);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
session
|
|
77
|
+
.answer()
|
|
78
|
+
.pause({ length: 1 })
|
|
79
|
+
.say({ text: 'Hello, how can I help you today?' })
|
|
80
|
+
.deepgram_s2s({
|
|
81
|
+
model: 'voice-agent',
|
|
82
|
+
auth: { apiKey },
|
|
83
|
+
actionHook: '/final',
|
|
84
|
+
eventHook: '/event',
|
|
85
|
+
toolHook: '/toolCall',
|
|
86
|
+
events: ['all'],
|
|
87
|
+
llmOptions: {
|
|
88
|
+
Settings: {
|
|
89
|
+
type: 'Settings',
|
|
90
|
+
agent: {
|
|
91
|
+
listen: { provider: { type: 'deepgram', model: 'nova-3' } },
|
|
92
|
+
think: {
|
|
93
|
+
provider: { type: 'open_ai', model: 'gpt-4o-mini' },
|
|
94
|
+
prompt: 'Please help the user with their request.',
|
|
95
|
+
functions: [
|
|
96
|
+
{
|
|
97
|
+
name: 'get_weather',
|
|
98
|
+
description: 'Get the weather at a given location',
|
|
99
|
+
parameters: {
|
|
100
|
+
type: 'object',
|
|
101
|
+
properties: {
|
|
102
|
+
location: { type: 'string', description: 'Location to get the weather from' },
|
|
103
|
+
scale: { type: 'string', enum: ['fahrenheit', 'celsius'] },
|
|
104
|
+
},
|
|
105
|
+
required: ['location', 'scale'],
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
.hangup()
|
|
115
|
+
.send();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
console.log('Deepgram voice agent listening on port 3000');
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { WebhookResponse } from '@jambonz/sdk/webhook';
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
app.post('/dial', (_req, res) => {
|
|
8
|
+
const jambonz = new WebhookResponse();
|
|
9
|
+
jambonz.dial({
|
|
10
|
+
answerOnBridge: true,
|
|
11
|
+
target: [
|
|
12
|
+
{
|
|
13
|
+
type: 'phone',
|
|
14
|
+
number: process.env.OUTDIAL_NUMBER || '13034997111',
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
res.json(jambonz);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
app.listen(3000, () => console.log('Dial webhook app listening on port 3000'));
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { WebhookResponse } from '@jambonz/sdk/webhook';
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
app.post('/echo', (_req, res) => {
|
|
8
|
+
const jambonz = new WebhookResponse();
|
|
9
|
+
jambonz
|
|
10
|
+
.pause({ length: 1 })
|
|
11
|
+
.gather({
|
|
12
|
+
input: ['speech'],
|
|
13
|
+
actionHook: '/echo-result',
|
|
14
|
+
timeout: 25,
|
|
15
|
+
say: { text: 'Please say something and I will echo it back to you.' },
|
|
16
|
+
})
|
|
17
|
+
.say({ text: 'I didn\'t hear anything. Goodbye.' })
|
|
18
|
+
.hangup();
|
|
19
|
+
|
|
20
|
+
res.json(jambonz);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
app.post('/echo-result', (req, res) => {
|
|
24
|
+
const { reason, speech } = req.body;
|
|
25
|
+
const jambonz = new WebhookResponse();
|
|
26
|
+
|
|
27
|
+
switch (reason) {
|
|
28
|
+
case 'speechDetected': {
|
|
29
|
+
const { transcript, confidence } = speech.alternatives[0];
|
|
30
|
+
jambonz
|
|
31
|
+
.say({
|
|
32
|
+
text: confidence
|
|
33
|
+
? `You said: ${transcript}. The confidence score was ${confidence.toFixed(2)}.`
|
|
34
|
+
: `You said: ${transcript}.`,
|
|
35
|
+
})
|
|
36
|
+
.gather({
|
|
37
|
+
input: ['speech'],
|
|
38
|
+
actionHook: '/echo-result',
|
|
39
|
+
timeout: 25,
|
|
40
|
+
say: { text: 'Please say something else.' },
|
|
41
|
+
})
|
|
42
|
+
.say({ text: 'I didn\'t hear anything. Goodbye.' })
|
|
43
|
+
.hangup();
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'timeout':
|
|
47
|
+
jambonz
|
|
48
|
+
.gather({
|
|
49
|
+
input: ['speech'],
|
|
50
|
+
actionHook: '/echo-result',
|
|
51
|
+
timeout: 25,
|
|
52
|
+
say: { text: 'Are you still there? I didn\'t hear anything.' },
|
|
53
|
+
})
|
|
54
|
+
.say({ text: 'I didn\'t hear anything. Goodbye.' })
|
|
55
|
+
.hangup();
|
|
56
|
+
break;
|
|
57
|
+
default:
|
|
58
|
+
jambonz.hangup();
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
res.json(jambonz);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
app.listen(3000, () => console.log('Echo webhook app listening on port 3000'));
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import { createEndpoint } from '@jambonz/sdk/websocket';
|
|
3
|
+
|
|
4
|
+
const server = http.createServer();
|
|
5
|
+
const makeService = createEndpoint({ server, port: 3000 });
|
|
6
|
+
|
|
7
|
+
const svc = makeService({ path: '/echo' });
|
|
8
|
+
|
|
9
|
+
svc.on('session:new', (session) => {
|
|
10
|
+
console.log(`New incoming call: ${session.callSid}`);
|
|
11
|
+
|
|
12
|
+
session
|
|
13
|
+
.on('close', (code: number, _reason: Buffer) => {
|
|
14
|
+
console.log(`Session ${session.callSid} closed: ${code}`);
|
|
15
|
+
})
|
|
16
|
+
.on('error', (err: Error) => {
|
|
17
|
+
console.error(`Session ${session.callSid} error:`, err);
|
|
18
|
+
})
|
|
19
|
+
.on('/echo', (evt: Record<string, any>) => {
|
|
20
|
+
console.log('Got speech event:', evt.reason);
|
|
21
|
+
|
|
22
|
+
switch (evt.reason) {
|
|
23
|
+
case 'speechDetected': {
|
|
24
|
+
const { transcript, confidence } = evt.speech.alternatives[0];
|
|
25
|
+
session
|
|
26
|
+
.say({
|
|
27
|
+
text: confidence
|
|
28
|
+
? `You said: ${transcript}. The confidence score was ${confidence.toFixed(2)}.`
|
|
29
|
+
: `You said: ${transcript}.`,
|
|
30
|
+
})
|
|
31
|
+
.gather({
|
|
32
|
+
input: ['speech'],
|
|
33
|
+
actionHook: '/echo',
|
|
34
|
+
timeout: 25,
|
|
35
|
+
say: { text: 'Please say something else.' },
|
|
36
|
+
})
|
|
37
|
+
.reply();
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case 'timeout':
|
|
41
|
+
session
|
|
42
|
+
.gather({
|
|
43
|
+
input: ['speech'],
|
|
44
|
+
actionHook: '/echo',
|
|
45
|
+
timeout: 25,
|
|
46
|
+
say: { text: 'Are you still there? I didn\'t hear anything.' },
|
|
47
|
+
})
|
|
48
|
+
.reply();
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
session.reply();
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
session
|
|
57
|
+
.pause({ length: 1 })
|
|
58
|
+
.gather({
|
|
59
|
+
input: ['speech'],
|
|
60
|
+
actionHook: '/echo',
|
|
61
|
+
timeout: 25,
|
|
62
|
+
say: { text: 'Please say something and I will echo it back to you.' },
|
|
63
|
+
})
|
|
64
|
+
.send();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
console.log('Echo WebSocket app listening on port 3000');
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import { createEndpoint } from '@jambonz/sdk/websocket';
|
|
3
|
+
|
|
4
|
+
const server = http.createServer();
|
|
5
|
+
const makeService = createEndpoint({
|
|
6
|
+
server,
|
|
7
|
+
port: 3000,
|
|
8
|
+
envVars: {
|
|
9
|
+
ELEVENLABS_AGENT_ID: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: 'ElevenLabs Conversational AI agent ID',
|
|
12
|
+
required: true,
|
|
13
|
+
},
|
|
14
|
+
ELEVENLABS_API_KEY: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'ElevenLabs API key (optional, enables signed URLs)',
|
|
17
|
+
obscure: true,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const svc = makeService({ path: '/elevenlabs-s2s' });
|
|
23
|
+
|
|
24
|
+
svc.on('session:new', (session) => {
|
|
25
|
+
console.log(`Incoming call: ${session.callSid}`);
|
|
26
|
+
console.log(' from: %s to: %s', session.data.from, session.data.to);
|
|
27
|
+
|
|
28
|
+
const agentId = session.data.env_vars?.ELEVENLABS_AGENT_ID;
|
|
29
|
+
const apiKey = session.data.env_vars?.ELEVENLABS_API_KEY;
|
|
30
|
+
|
|
31
|
+
if (!agentId) {
|
|
32
|
+
console.error('ERROR: ELEVENLABS_AGENT_ID not found in application environment variables');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const auth: Record<string, string> = { agent_id: agentId };
|
|
37
|
+
if (apiKey) auth.api_key = apiKey;
|
|
38
|
+
|
|
39
|
+
session
|
|
40
|
+
.on('/event', (_evt: Record<string, any>) => {
|
|
41
|
+
// ElevenLabs events (user_transcript, agent_response, etc.)
|
|
42
|
+
})
|
|
43
|
+
.on('/toolCall', async (evt: Record<string, any>) => {
|
|
44
|
+
const { name, args, tool_call_id } = evt;
|
|
45
|
+
console.log(`Tool call: ${name}`, args);
|
|
46
|
+
|
|
47
|
+
// Return tool results in ElevenLabs client_tool_result format
|
|
48
|
+
session.sendToolOutput(tool_call_id, {
|
|
49
|
+
type: 'client_tool_result',
|
|
50
|
+
tool_call_id,
|
|
51
|
+
result: JSON.stringify({ error: `Unknown tool: ${name}` }),
|
|
52
|
+
is_error: true,
|
|
53
|
+
});
|
|
54
|
+
})
|
|
55
|
+
.on('/final', (evt: Record<string, any>) => {
|
|
56
|
+
console.log(`Session ${session.callSid} completed: ${evt.completion_reason}`);
|
|
57
|
+
session.reply();
|
|
58
|
+
})
|
|
59
|
+
.on('close', (code: number) => {
|
|
60
|
+
console.log(`Session ${session.callSid} closed: ${code}`);
|
|
61
|
+
})
|
|
62
|
+
.on('error', (err: Error) => {
|
|
63
|
+
console.error(`Session ${session.callSid} error:`, err);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
session
|
|
67
|
+
.answer()
|
|
68
|
+
.pause({ length: 1 })
|
|
69
|
+
.elevenlabs_s2s({
|
|
70
|
+
auth,
|
|
71
|
+
llmOptions: {},
|
|
72
|
+
actionHook: '/final',
|
|
73
|
+
eventHook: '/event',
|
|
74
|
+
toolHook: '/toolCall',
|
|
75
|
+
events: ['all'],
|
|
76
|
+
})
|
|
77
|
+
.hangup()
|
|
78
|
+
.send();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
console.log('ElevenLabs voice agent listening on port 3000');
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Hello World
|
|
2
|
+
|
|
3
|
+
The simplest jambonz application: answer a call, greet the caller, and hang up.
|
|
4
|
+
|
|
5
|
+
## What It Demonstrates
|
|
6
|
+
- Basic verb array structure
|
|
7
|
+
- The `say` verb
|
|
8
|
+
- The `hangup` verb
|
|
9
|
+
|
|
10
|
+
## Webhook Version
|
|
11
|
+
- `app.js` — Plain JavaScript with Express
|
|
12
|
+
- `app.ts` — TypeScript using `@jambonz/sdk` WebhookResponse builder
|
|
13
|
+
|
|
14
|
+
## WebSocket Version
|
|
15
|
+
- `ws-app.ts` — TypeScript using `@jambonz/sdk` createEndpoint + Session
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const app = express();
|
|
3
|
+
app.use(express.json());
|
|
4
|
+
|
|
5
|
+
// jambonz calls this URL when a call arrives
|
|
6
|
+
app.post('/incoming', (req, res) => {
|
|
7
|
+
console.log(`Incoming call from ${req.body.from} to ${req.body.to}`);
|
|
8
|
+
|
|
9
|
+
res.json([
|
|
10
|
+
{
|
|
11
|
+
verb: 'say',
|
|
12
|
+
text: 'Hello! Welcome to our service. Thank you for calling. Goodbye!'
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
verb: 'hangup'
|
|
16
|
+
}
|
|
17
|
+
]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
app.listen(3000, () => console.log('jambonz app listening on port 3000'));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { WebhookResponse } from '@jambonz/sdk/webhook';
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
// jambonz calls this URL when a call arrives
|
|
8
|
+
app.post('/incoming', (req, res) => {
|
|
9
|
+
console.log(`Incoming call from ${req.body.from} to ${req.body.to}`);
|
|
10
|
+
|
|
11
|
+
const jambonz = new WebhookResponse();
|
|
12
|
+
jambonz
|
|
13
|
+
.say({ text: 'Hello! Welcome to our service. Thank you for calling. Goodbye!' })
|
|
14
|
+
.hangup();
|
|
15
|
+
|
|
16
|
+
res.json(jambonz);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
app.listen(3000, () => console.log('jambonz app listening on port 3000'));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import { createEndpoint } from '@jambonz/sdk/websocket';
|
|
3
|
+
|
|
4
|
+
const server = http.createServer();
|
|
5
|
+
const makeService = createEndpoint({ server, port: 3000 });
|
|
6
|
+
|
|
7
|
+
const svc = makeService({ path: '/' });
|
|
8
|
+
|
|
9
|
+
svc.on('session:new', (session) => {
|
|
10
|
+
console.log(`Incoming call: ${session.callSid}`);
|
|
11
|
+
|
|
12
|
+
session
|
|
13
|
+
.say({ text: 'Hello! Welcome to our service. Thank you for calling. Goodbye!' })
|
|
14
|
+
.hangup()
|
|
15
|
+
.send();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
console.log('jambonz ws app listening on port 3000');
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# IVR Menu
|
|
2
|
+
|
|
3
|
+
A classic interactive voice response menu that routes callers based on DTMF or speech input.
|
|
4
|
+
|
|
5
|
+
## What It Demonstrates
|
|
6
|
+
- The `gather` verb with nested `say` prompt
|
|
7
|
+
- Handling DTMF digit input
|
|
8
|
+
- Handling speech input
|
|
9
|
+
- Routing to different handlers based on user input
|
|
10
|
+
- Using `actionHook` to receive gather results and return new verbs
|
|
11
|
+
- Fallback when no input is received (timeout)
|
|
12
|
+
|
|
13
|
+
## Files
|
|
14
|
+
- `app.js` — Plain JavaScript with Express
|
|
15
|
+
- `app.ts` — TypeScript using `@jambonz/sdk` WebhookResponse builder
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const app = express();
|
|
3
|
+
app.use(express.json());
|
|
4
|
+
|
|
5
|
+
// Initial call handler — present the menu
|
|
6
|
+
app.post('/incoming', (req, res) => {
|
|
7
|
+
res.json([
|
|
8
|
+
{
|
|
9
|
+
verb: 'gather',
|
|
10
|
+
input: ['speech', 'digits'],
|
|
11
|
+
actionHook: '/menu-selection',
|
|
12
|
+
numDigits: 1,
|
|
13
|
+
timeout: 10,
|
|
14
|
+
say: {
|
|
15
|
+
text: 'Welcome to Acme Corp. Press 1 or say sales for sales. Press 2 or say support for technical support. Press 3 or say billing for billing.'
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
// Fallback if no input received
|
|
19
|
+
{
|
|
20
|
+
verb: 'say',
|
|
21
|
+
text: 'We did not receive any input. Goodbye.'
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
verb: 'hangup'
|
|
25
|
+
}
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Handle the menu selection
|
|
30
|
+
app.post('/menu-selection', (req, res) => {
|
|
31
|
+
const { digits, speech } = req.body;
|
|
32
|
+
const transcript = speech?.alternatives?.[0]?.transcript?.toLowerCase() || '';
|
|
33
|
+
|
|
34
|
+
let department;
|
|
35
|
+
if (digits === '1' || transcript.includes('sales')) {
|
|
36
|
+
department = 'sales';
|
|
37
|
+
} else if (digits === '2' || transcript.includes('support')) {
|
|
38
|
+
department = 'support';
|
|
39
|
+
} else if (digits === '3' || transcript.includes('billing')) {
|
|
40
|
+
department = 'billing';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (department) {
|
|
44
|
+
res.json([
|
|
45
|
+
{
|
|
46
|
+
verb: 'say',
|
|
47
|
+
text: `Connecting you to ${department}. Please hold.`
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
verb: 'dial',
|
|
51
|
+
target: [{ type: 'user', name: `${department}-queue` }],
|
|
52
|
+
answerOnBridge: true,
|
|
53
|
+
timeout: 30,
|
|
54
|
+
actionHook: '/dial-result'
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
verb: 'say',
|
|
58
|
+
text: `Sorry, ${department} is not available right now. Please try again later.`
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
verb: 'hangup'
|
|
62
|
+
}
|
|
63
|
+
]);
|
|
64
|
+
} else {
|
|
65
|
+
// Unrecognized input — replay the menu
|
|
66
|
+
res.json([
|
|
67
|
+
{
|
|
68
|
+
verb: 'say',
|
|
69
|
+
text: 'Sorry, I didn\'t understand that.'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
verb: 'redirect',
|
|
73
|
+
actionHook: '/incoming'
|
|
74
|
+
}
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Handle dial result
|
|
80
|
+
app.post('/dial-result', (req, res) => {
|
|
81
|
+
console.log(`Call ended: ${JSON.stringify(req.body)}`);
|
|
82
|
+
res.json([{ verb: 'hangup' }]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
app.listen(3000, () => console.log('IVR app listening on port 3000'));
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { WebhookResponse } from '@jambonz/sdk/webhook';
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
// Initial call handler — present the menu
|
|
8
|
+
app.post('/incoming', (_req, res) => {
|
|
9
|
+
const jambonz = new WebhookResponse();
|
|
10
|
+
jambonz
|
|
11
|
+
.gather({
|
|
12
|
+
input: ['speech', 'digits'],
|
|
13
|
+
actionHook: '/menu-selection',
|
|
14
|
+
numDigits: 1,
|
|
15
|
+
timeout: 10,
|
|
16
|
+
say: {
|
|
17
|
+
text: 'Welcome to Acme Corp. Press 1 or say sales for sales. Press 2 or say support for technical support. Press 3 or say billing for billing.',
|
|
18
|
+
},
|
|
19
|
+
})
|
|
20
|
+
.say({ text: 'We did not receive any input. Goodbye.' })
|
|
21
|
+
.hangup();
|
|
22
|
+
|
|
23
|
+
res.json(jambonz);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Handle the menu selection
|
|
27
|
+
app.post('/menu-selection', (req, res) => {
|
|
28
|
+
const { digits, speech } = req.body;
|
|
29
|
+
const transcript: string = speech?.alternatives?.[0]?.transcript?.toLowerCase() || '';
|
|
30
|
+
|
|
31
|
+
let department: string | undefined;
|
|
32
|
+
if (digits === '1' || transcript.includes('sales')) {
|
|
33
|
+
department = 'sales';
|
|
34
|
+
} else if (digits === '2' || transcript.includes('support')) {
|
|
35
|
+
department = 'support';
|
|
36
|
+
} else if (digits === '3' || transcript.includes('billing')) {
|
|
37
|
+
department = 'billing';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const jambonz = new WebhookResponse();
|
|
41
|
+
|
|
42
|
+
if (department) {
|
|
43
|
+
jambonz
|
|
44
|
+
.say({ text: `Connecting you to ${department}. Please hold.` })
|
|
45
|
+
.dial({
|
|
46
|
+
target: [{ type: 'user', name: `${department}-queue` }],
|
|
47
|
+
answerOnBridge: true,
|
|
48
|
+
timeout: 30,
|
|
49
|
+
actionHook: '/dial-result',
|
|
50
|
+
})
|
|
51
|
+
.say({ text: `Sorry, ${department} is not available right now. Please try again later.` })
|
|
52
|
+
.hangup();
|
|
53
|
+
} else {
|
|
54
|
+
jambonz
|
|
55
|
+
.say({ text: "Sorry, I didn't understand that." })
|
|
56
|
+
.redirect({ actionHook: '/incoming' });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
res.json(jambonz);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Handle dial result
|
|
63
|
+
app.post('/dial-result', (req, res) => {
|
|
64
|
+
console.log(`Call ended: ${JSON.stringify(req.body)}`);
|
|
65
|
+
const jambonz = new WebhookResponse();
|
|
66
|
+
jambonz.hangup();
|
|
67
|
+
res.json(jambonz);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
app.listen(3000, () => console.log('IVR app listening on port 3000'));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { WebhookResponse } from '@jambonz/sdk/webhook';
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
app.post('/record', (_req, res) => {
|
|
8
|
+
const wsUrl = process.env.WS_RECORD_URL;
|
|
9
|
+
if (!wsUrl) throw new Error('WS_RECORD_URL env variable is required');
|
|
10
|
+
|
|
11
|
+
const jambonz = new WebhookResponse();
|
|
12
|
+
jambonz
|
|
13
|
+
.pause({ length: 1 })
|
|
14
|
+
.say({
|
|
15
|
+
text: 'Hi there. Please leave a message, and we will get back to you shortly.',
|
|
16
|
+
})
|
|
17
|
+
.listen({ url: wsUrl });
|
|
18
|
+
|
|
19
|
+
res.json(jambonz);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
app.listen(3000, () => console.log('Listen-record webhook app listening on port 3000'));
|