Receive an inbound call
In this code snippet you see how to receive an inbound call.
Example
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Vonage CLI (see below) or via the dashboard. To learn more about applications see our Vonage 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 Vonage 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
Vonage 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 "Receive Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
npm install express
Create a file named receive-an-inbound-call.js
and add the following code:
const app = require('express')()
Write the code
Add the following to receive-an-inbound-call.js
:
const onInboundCall = (request, response) => {
const from = request.query.from
const fromSplitIntoCharacters = from.split('').join(' ')
const ncco = [{
action: 'talk',
text: `Thank you for calling from ${fromSplitIntoCharacters}`
}]
response.json(ncco)
}
app.get('/webhooks/answer', onInboundCall)
Run your code
Save this file to your machine and run it:
node receive-an-inbound-call.js
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Vonage CLI (see below) or via the dashboard. To learn more about applications see our Vonage 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 Vonage 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
Vonage 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 "Receive Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Add the following to `build.gradle`:
compile 'com.vonage:client:6.2.0'
compile 'com.sparkjava:spark-core:2.7.2'
Write the code
Add the following to the main
method of the InboundCall
class:
/*
* Route to answer incoming call.
*/
Route answerRoute = (req, res) -> {
String from = req.queryParams("from").replace("", " ");
TalkAction message = TalkAction
.builder(String.format("Thank you for calling from %s", from))
.build();
res.type("application/json");
return new Ncco(message).toJson();
};
/*
* Route to print out call event info.
*/
Route eventRoute = (req, res) -> {
System.out.println(req.body());
return "";
};
Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/events", eventRoute);
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.vonage.quickstart.voice
with the package containing InboundCall
:
gradle run -Pmain=com.vonage.quickstart.voice.InboundCall
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Vonage CLI (see below) or via the dashboard. To learn more about applications see our Vonage 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 Vonage 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
Vonage 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 "Receive Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install-Package Vonage
Create a file named ReceiveInboundCallController.cs
and add the following code:
using Vonage.Voice.AnswerWebhooks;
using Vonage.Voice.EventWebhooks;
using Vonage.Voice.Nccos;
Write the code
Add the following to ReceiveInboundCallController.cs
:
[HttpGet("webhooks/answer")]
public string Answer()
{
var talkAction = new TalkAction()
{
Text = $"Thank you for calling from " +
$"{string.Join(" ", Request.Query["from"].ToString().ToCharArray())}"
};
var ncco = new Ncco(talkAction);
return ncco.ToString();
}
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Vonage CLI (see below) or via the dashboard. To learn more about applications see our Vonage 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 Vonage 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
Vonage 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 "Receive Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
composer require slim/slim:^3.8 vonage/client
Create a file named index.php
and add the following code:
use Laminas\Diactoros\Response\JsonResponse;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
Add the following to index.php
:
require 'vendor/autoload.php';
$app = new \Slim\App();
Write the code
Add the following to index.php
:
$app->get('/webhooks/answer', function (Request $request, Response $response) {
/** @var \Vonage\Voice\Webhook\Answer $call */
$call = \Vonage\Voice\Webhook\Factory::createFromRequest($request);
$fromSplitIntoCharacters = implode(" ", str_split($call->getFrom()));
$ncco = new \Vonage\Voice\NCCO\NCCO();
$ncco->addAction(
new \Vonage\Voice\NCCO\Action\Talk('Thank you for calling from ' . $fromSplitIntoCharacters)
);
return new JsonResponse($ncco);
});
$app->run();
Run your code
Save this file to your machine and run it:
php -S localhost:3000 -t .
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Vonage CLI (see below) or via the dashboard. To learn more about applications see our Vonage 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 Vonage 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
Vonage 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 "Receive Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
pip install 'flask>=1.0'
Create a file named receive-an-inbound-call.py
and add the following code:
from flask import Flask, request, jsonify
app = Flask(__name__)
Write the code
Add the following to receive-an-inbound-call.py
:
@app.route("/webhooks/answer")
def answer_call():
for param_key, param_value in request.args.items():
print("{}: {}".format(param_key, param_value))
from_ = request.args['from']
return jsonify([
{
"action": "talk",
"text": "Thank you for calling from " + " ".join(from_)
}
])
Run your code
Save this file to your machine and run it:
python3 receive-an-inbound-call.py
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Vonage CLI (see below) or via the dashboard. To learn more about applications see our Vonage 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 Vonage 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
Vonage 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 "Receive Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
gem install sinatra sinatra-contrib
Create a file named inbound_tts_call.rb
and add the following code:
require 'sinatra'
require 'sinatra/multi_route'
require 'json'
helpers do
def parsed_body
JSON.parse(request.body.read)
end
end
before do
content_type :json
end
Write the code
Add the following to inbound_tts_call.rb
:
route :get, :post, '/webhooks/answer' do
from = params['from'] || parsed_body['from']
from_split_into_characters = from.split('').join(' ')
[{
action: 'talk',
text: "Thank you for calling from #{from_split_into_characters}"
}].to_json
end
set :port, 3000
Run your code
Save this file to your machine and run it:
ruby inbound_tts_call.rb
Try it out
When you call your Vonage Number you will hear a text-to-speech message.