Examples

View as Markdown

Follow the examples to see how’s easy to use the Relay SDK to interact with inbound or outbound calls.

Inbound Calls

Using RelayConsumer to manage inbound calls from both home and office contexts. Answer the call, ask the user to enter his PIN and playback the digits he sent if successful.

1const { RelayConsumer } = require('@signalwire/node')
2
3const consumer = new RelayConsumer({
4 project: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
5 token: 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
6 contexts: ['home', 'office'],
7 onIncomingCall: async (call) => {
8 const { successful } = await call.answer()
9 if (!successful) {
10 console.error('Answer Error')
11 return
12 }
13
14 const collect = {
15 type: 'digits',
16 digits_max: 3,
17 text: 'Welcome to SignalWire! Please, enter your 3 digits PIN'
18 }
19 const prompt = await call.promptTTS(collect)
20 if (prompt.successful) {
21 await call.playTTS({ text: `You entered: ${prompt.result}. Thanks and good bye!` })
22 }
23 await call.hangup()
24 }
25})
26
27consumer.run()

Outbound Calls

Using RelayConsumer to make a call and ask user to enter the PIN. When the prompt has completed, logs the result.

1const { RelayConsumer } = require('@signalwire/node')
2
3const consumer = new RelayConsumer({
4 project: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
5 token: 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
6 contexts: ['home', 'office'],
7 ready: async (consumer) => {
8 const dialResult = await consumer.client.calling.dial({
9 type: 'phone',
10 from: '+1XXXXXXXXXX', // Must be a number in your SignalWire Space
11 to: '+1YYYYYYYYYY'
12 })
13 const { successful, call } = dialResult
14 if (!successful) {
15 console.error('Dial error..')
16 return
17 }
18
19 const prompt = await call.promptTTS({
20 type: 'digits',
21 digits_max: 3,
22 text: 'Welcome to SignalWire! Enter your 3 digits PIN'
23 })
24
25 if (prompt.successful) {
26 console.log(`User digits: ${prompt.result}`)
27 }
28 }
29})
30
31consumer.run()