Skip to content

Instantly share code, notes, and snippets.

@mheap

mheap/_README.md Secret

Forked from adambutler/_README.md
Last active November 29, 2017 13:01
Show Gist options
  • Save mheap/ee1425590095207b3af42b7ca51ccefc to your computer and use it in GitHub Desktop.
Save mheap/ee1425590095207b3af42b7ca51ccefc 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-php-quickstart/blob/master/voice/connect-call-slim/index.php
// Lines: ALL LINES

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';

$app = new \Slim\App;

$app->get('/webhooks/answer', function (Request $request, Response $response) {
    $ncco = [
        [
            'action' => 'connect',
            'endpoint' => [
                [
                    'type' => 'phone',
                    'number' => YOUR_SECOND_NUMBER
                ]
            ]
        ]
    ];
    return $response->withJson($ncco);
});
$app->run();

Run your server

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

$ php -t . -S 127.0.0.1:3000

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