Skip to content

Instantly share code, notes, and snippets.

@adambutler
Last active November 15, 2017 10:15
Show Gist options
  • Save adambutler/03b42f54dd755dfd70034325a68cd9d3 to your computer and use it in GitHub Desktop.
Save adambutler/03b42f54dd755dfd70034325a68cd9d3 to your computer and use it in GitHub Desktop.

Connect an inbound call

Specification

A building block that shows how to handle an inbound call that connects to another person by making an outbound call.

  • You must specify one endpoint must be on the path /webhooks/answer
  • Your endpoint must be accessible on a GET request
  • Your endpoint must listen on port 3000
  • Your response should return a 200 status code
  • You must provide a constant of YOUR_SECOND_NUMBER for the user to replace

Note: The example below assumes the constant YOUR_SECOND_NUMBER has been set or replaced with a string value of 447700900000.

  • Your response should be JSON NCCO with the the following or equivalent body:
[
  {
    "action": "connect",
    "endpoint": [
      {
        "type": "phone",
        "number": "447700900000"
      }
    ]
  }
]
  • Code examples should include a GitHub link to the file
  • Code examples should include the line numbers used from the linked file
  • All code examples must be present in the master branch of the relevant quickstart repo
  • All code examples must be tested to work (manual testing is fine)

Instructions

  1. Fork this gist
  2. Edit the gist and the building block files to match the specification
  3. Return Gist as a comment in the original issue

Connect an inbound call

Connecting two calls with Nexmo is easy. In this example we'll accept an inbound call and make an outbound call that will be connected.

Replace the following variables in the example below:

Key Description
YOUR_SECOND_NUMBER The number you wish the inbound caller to be connected to.

Implement a webhook

// GITHUB: https://github.com/nexmo-community/nexmo-node-quickstart/blob/master/voice/connect-an-inbound-call.js
// Lines: ALL LINES

const app = require('express')()

const onInboundCall = (request, response) => {
  const ncco = [{
    action: 'connect',
    endpoint: [{
      type: 'phone',
      number: YOUR_SECOND_NUMBER
    }]
  }]

  response.json(ncco)
}

app.get('/webhooks/answer', onInboundCall)

app.listen(3000)

Run your server

Save this file to your machine and run it using the node command:

$ node app.js

You'll need to expose your server to the open internet. During development you can use a tool like Ngrok to do that.

Associate an application to your webhook

To link your number to the endpoint you've just created we'll need an Application.

$ nexmo app:create demo <YOUR_HOSTNAME>/webhooks/answer <YOUR_HOSTNAME>/webhooks/event
$ nexmo link:app <NEXMO_NUMBER> <NEXMO_APPLICATION_ID>

Call your number

When you call your Nexmo number you should be connected to the the number you specified in place of YOUR_SECOND_NUMBER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment