detect

View as MarkdownOpen in Claude

Start audio detection on the call. Detects answering machines, fax tones, or DTMF digits. Returns a DetectAction that resolves on the first detection result or when the operation finishes.

The DetectAction resolves on the first detection result, not when the detect operation finishes. This means await action.wait() returns as soon as a result is available.

This method emits calling.call.detect events. See Call Events for payload details.

Parameters

detect
Record<string, unknown>Required

Detection configuration object.

detect.type
stringRequired

Detection type. Valid values:

  • "machine" — answering machine detection (AMD)
  • "fax" — fax tone detection (CNG/CED)
  • "digit" — DTMF digit detection
detect.params
Record<string, unknown>

Type-specific detection parameters.

timeout
number | undefined

Maximum seconds to run the detector before stopping.

controlId
string | undefined

Custom control ID. Auto-generated if not provided.

onCompleted
(event: RelayEvent) => void | Promise<void>

Callback invoked when detection completes.

Returns

Promise<DetectAction> — An action handle with stop() and wait() methods.

Example

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9client.onCall(async (call) => {
10 await call.answer();
11
12 // Answering machine detection
13 const action = await call.detect(
14 { type: 'machine', params: {} },
15 { timeout: 30 }
16 );
17 const event = await action.wait();
18
19 const detectResult = event.params.detect as Record<string, unknown> ?? {};
20 if (detectResult.type === 'machine') {
21 console.log('Answering machine detected');
22 await call.hangup();
23 } else {
24 console.log('Human answered');
25 const playAction = await call.play([{ type: 'tts', text: 'Hello! This is a call from...' }]);
26 await playAction.wait();
27 }
28});
29
30await client.run();