Build a Call Forwarder using SignalWire RELAY

A new way to build voice automation using SignalWire RELAY.

Developer Advocate Moheeb Zara

We’ve published a SignalWire RELAY example that can answer a call with text to speech before connecting it to a designated number. All you need is a SignalWire account and phone number. All the instructions to run it are in the SignalWire-Relay-Examples GitHub Repository.

In this blog post we’ll go through all the inner workings of this example and explain what SignalWire RELAY is and how it can supercharge your automation!

What is SignalWire Relay?

RELAY is the next generation of interactive communication APIs available at SignalWire. It is a new, real-time web service protocol that provides for persistent, asynchronous connections to the SignalWire network.

Most providers use REST APIs that rely on one-way communication. This adds latency and limits interactivity of real-time events. SignalWire’s RELAY APIs use WebSocket technology, which allow for simultaneous, bi-directional data transmission. Using RELAY, you can deploy reliable, low latency, real-time communications.

RELAY allows for interactive and advanced command and control. Complete control will enable easy transfers and injections across all endpoints, making it easier and quicker to build applications. RELAY integrates easily with your products and infrastructure, enabling simple but powerful applications using Artificial Intelligence (AI) tools, data exchange, serverless technologies and more.

Finally, RELAY enables communications tools to perform in the most popular and widely used environments, like web browsers, mobile devices, in the cloud, or within your own infrastructure.

Call Forwarder Example

The call forwarder example uses the SignalWire RELAY NodeJS SDK to connect as a RELAY Consumer. A RELAY Consumer is essentially a client connected to the SignalWire RELAY API over web-sockets. This creates a persistent secure connection that allows the client SDK to subscribe to events and send commands.

RELAY uses Contexts, a named string to separate events to specific consumers for specific types of calls or messages or to scale them independently.

When creating outbound requests, or configuring phone numbers for inbound requests, you can specify the context; RELAY will then deliver that call or event to Consumers that are configured to listen for that context

This diagram shows how this example works. The RELAY Consumer first connects to the SignalWire cloud using a Project ID and API Token to authenticate. It then registers the context ‘voice-forwarder’.

const { RelayConsumer } = require('@signalwire/node')

const projectId = process.env.SIGNALWIRE_PROJECT_KEY
const token = process.env.SIGNALWIRE_TOKEN
const verifiedNumber = process.env.VERIFIED_NUMBER

const consumer = new RelayConsumer({
  project: projectId,
  token: token,
  contexts: ['voice-forwarder'],
  ready: async ({ client }) => {
    console.log('Consumer Ready!')....

When an inbound call is made to a SignalWire Phone Number configured to the context ‘voice-forwarder’, the Consumer is notified.

The Consumer has a handler function, onIncomingCall, it uses the RELAY SDK method call.playTTS to play a text-to-speech message to let the caller know they are being connected to another number.

onIncomingCall: async (call) => {
    const { successful } = await call.answer()
    if (!successful) { return }

    await call.playTTS({ text: 'Please wait on the line while we connect you.' })...

Another RELAY SDK method, call.connect, is used to command the SignalWire cloud to connect the incoming call to a Verified Number.

const connectResult = await call.connect({ type: 'phone', to: verifiedNumber, timeout: 30 })

If any member of the call disconnects an event, ‘connect.disconnected’, is sent to the Consumer and the method call.hangup is used to end the call.

call.on('connect.disconnected', async (call) => {
console.log('Call disconnected, hanging up')
await call.hangup()

What next?!

As shown by this example, SignalWire RELAY gives you a powerful set of tools to build telecommunications automation. Forwarding a call is just the tip of the iceberg. RELAY allows you to automate the sending and receiving of SMS, user-input from voice calls, and much more.

You can modify this particular example to pull from a list of verified numbers and direct calls to the appropriate number based on user input. With some of the other features of RELAY it is also possible to create automation flows that wait until a user performs an action on a website.

RELAY provides the building blocks to bring your ideas to life.

Join our community!

Here at SignalWire, we love our community of developers! Find us on SignalWire’s Twitter and be sure to join our SignalWire Community Slack. We’re always happy to help answer questions and we look forward to engaging with you!