How to do this simple thing
This section introduces what the building block does. Bear in mind a user could land here from a search result so we may need to add a little context, redefine abbreviations, or otherwise help them orient themselves.
Example
Introduce the example, what are we about to do or see?
Before diving into the code samples, define the placeholders used by the code samples that will follow. These should follow our guidelines for placeholders used throughout Nexmo Developer.
Key | Description |
---|---|
NEXMO_NUMBER |
Nexmo Number to call. |
Prerequisites
Create an application
A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Nexmo needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Example Application" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies
npm install express body-parser
Initialize your dependencies
Create a file named interesting.js
and add the following code:
const app = require('express')()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
Write the code
Add the following to interesting.js
:
app
.get('/webhooks/answer', onInboundCall)
.post('/webhooks/dtmf', onInput)
app.listen(3000)
Run your code
Save this file to your machine and run it:
node interesing.js
Prerequisites
Create an application
A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Nexmo needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Example Application" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies
Add the following to build.gradle
:
compile 'com.nexmo:client:4.0.0'
Write the code
Add the following to the main
method of the DtmfInput
class:
*/
Route answerRoute = (req, res) -> {
TalkAction intro = new TalkAction.Builder("Hello. Please press any key to continue.").build();
InputAction input = new InputAction.Builder()
.eventUrl(String.format("%s://%s/webhooks/dtmf", req.scheme(), req.host()))
.maxDigits(1)
.build();
res.type("application/json");
return new Ncco(intro, input).toJson();
};
/*
* Route which returns NCCO saying which DTMF code was received.
*/
Route inputRoute = (req, res) -> {
InputEvent event = InputEvent.fromJson(req.body());
TalkAction response = new TalkAction.Builder(String.format("You pressed %s, Goodbye.",
event.getDtmf()
)).build();
res.type("application/json");
return new Ncco(response).toJson();
};
Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Run your code
We can use the application
plugin for Gradle to simplify the running of our application.
Update your build.gradle
with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''
Run the following gradle
command to execute your application, replacing com.nexmo.quickstart.voice
with the package containing DtmfInput
:
gradle run -Pmain=com.nexmo.quickstart.voice.DtmfInput
Prerequisites
Create an application
A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Nexmo needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Example Application" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies
composer require slim/slim:^3.8
Write the code
Add the following to index.php
:
$app = new \Slim\App;
$app->get('/webhooks/answer', function (Request $request, Response $response) {
$uri = $request->getUri();
$ncco = [
[
'action' => 'talk',
'text' => 'Please enter a digit'
],
[
'action' => 'input',
'eventUrl' => [
$uri->getScheme().'://'.$uri->getHost().':'.$uri->getPort().'/webhooks/dtmf'
]
]
];
return $response->withJson($ncco);
});
$app->post('/webhooks/dtmf', function (Request $request, Response $response) {
$params = $request->getParsedBody();
$ncco = [
[
'action' => 'talk',
'text' => 'You pressed '.$params['dtmf']
]
];
return $response->withJson($ncco);
});
$app->run();
Run your code
Save this file to your machine and run it:
php -t . -S localhost:3000
Prerequisites
Create an application
A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Nexmo needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Example Application" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies
pip install 'flask>=1.0'
Initialize your dependencies
Create a file named handle-user-input.py
and add the following code:
from flask import Flask, request, jsonify
from pprint import pprint
app = Flask(__name__)
Write the code
Add the following to handle-user-input.py
:
@app.route("/webhooks/answer")
def answer_call():
for param_key, param_value in request.args.items():
print("{}: {}".format(param_key, param_value))
input_webhook_url = request.url_root + "webhooks/dtmf"
ncco =[
{
"action": "talk",
"text": "Hello, please press any key to continue."
},
{
"action": "input",
"maxDigits": 1,
"eventUrl": [input_webhook_url]
}
]
return jsonify(ncco)
@app.route("/webhooks/dtmf", methods=['POST'])
def dtmf():
data = request.get_json()
pprint(data)
ncco =[
{
"action": "talk",
"text": "You pressed {}, goodbye".format(data['dtmf'])
}
]
return jsonify(ncco)
Run your code
Save this file to your machine and run it:
python3 handle-user-input.py
Prerequisites
Create an application
A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Nexmo needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Example Application" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies
gem install nexmo
Write the code
Add the following to dtmf.rb
:
helpers do
def parsed_body
JSON.parse(request.body.read)
end
end
before do
content_type :json
end
route :get, :post, '/webhooks/answer' do
[
{
action: 'talk',
text: 'Please enter a digit'
},
{
action: 'input',
eventUrl: ["#{request.base_url}/webhooks/dtmf"]
}
].to_json
end
route :get, :post, '/webhooks/dtmf' do
dtmf = params['dtmf'] || parsed_body['dtmf']
[{
action: 'talk',
text: "You pressed #{dtmf}"
}].to_json
end
Run your code
Save this file to your machine and run it:
ruby dtmf.rb
Note the application
section of the building_blocks
construct is optional. If not used there will be no prerequisite section in the rendered block.
Try it out
What did the user just build? How should they use it? Add the instructions for how to demo the masterpiece the user has just created.
Further reading
Add resources for a user such as:
- A tutorial where this technique is used.
- Reference to either our own or external tools that might be handy.
- A guide for a similar topic (especially if that guide links to this building block).