Skip to content

Instantly share code, notes, and snippets.

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

Handle user input with DTMF

Specification

A building block that shows how to handle an user input with DTMF

  • You must specify one endpoint must be on the path /webhooks/answer

  • Your endpoint must listen on port 3000

  • Your answer endpoint response should return a 200 status code

  • Your endpoint should handle both query string parameters and JSON on both GET and POST methods.

  • Your endpoints should handle query string parameters on GET and POST

  • Your endpoint should handle JSON on POST

  • When /webhooks/dtmf is called a 200 OK repsonse should be returned.

  • The eventURL should be like http://127.0.0.1:3000/webhooks/dtmf or https://demo.ngrok.io/webhooks/dtmf and should be dynamicly made up of:

    • The request protocol
    • The request hostname
    • The request port (only if present)
    • /webhooks/dtmf
  • Your answer response should be JSON NCCO with the the following or equivilent body:

    • GET

      curl "http://127.0.0.1:3000/webhooks/answer"

Should return:

[
  {
    "action": "talk",
    "text": "Please enter a digit"
  },
  {
    "action": "input",
    "eventUrl": [
      "http://127.0.0.1:3000/webhooks/dtmf"
    ]
  }
]
  • Your event response should be JSON NCCO with the the following or equivilent body:

    • POST + JSON

      curl -X "POST" "http://127.0.0.1:3000/webhooks/dtmf" \
      -H 'Content-Type: application/json; charset=utf-8' \
      -d $'{ "dtmf": "7" }'

Should return:

[
  {
    "action": "talk",
    "text": "You pressed 7"
  }
]
  • 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

Handle user input with DTMF

Handling user input with Nexmo is easy. You just need to provide an NCCO to start listenting for input and provide a webhook endpoint for handling the event when it happens.

Prerequisites

Implement a webhook

// GITHUB: https://github.com/nexmo-community/nexmo-node-quickstart/blob/master/voice/dtmf.js
// Lines: ALL LINES

const app = require('express')()
const bodyParser = require('body-parser')

app.use(bodyParser.json())

const onInboundCall = (request, response) => {
  const ncco = [
    {
      action: 'talk',
      text: 'Please enter a digit'
    },
    {
      action: 'input',
      eventUrl: [`${request.protocol}://${request.get('host')}/webhooks/dtmf`]
    }
  ]

  response.json(ncco)
}

const onInput = (request, response) => {
  const dtmf = request.body.dtmf

  const ncco = [{
    action: 'talk',
    text: `You pressed ${dtmf}`
  }]

  response.json(ncco)
}

app
  .get('/webhooks/answer', onInboundCall)
  .post('/webhooks/dtmf', onInput)

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 asked to enter a number that is then repeated back to you.

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