Receive an inbound call
In this code snippet you see how to receive an inbound call.
Example
Prerequisites
Write the code
Add the following to receive-an-inbound-call.js:
const Express = require('express');
const app = new Express();
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);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
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:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.voice with the package containing ReceiveInboundCall:
Prerequisites
Add the following to build.gradle:
Write the code
Add the following to the main method of the InboundCall file:
/*
* 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:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.voice with the package containing InboundCall:
Prerequisites
Prerequisites
Write the code
Add the following to index.php:
require 'vendor/autoload.php';
$app = new \Slim\App();
$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:
Prerequisites
Run your code
Save this file to your machine and run it:
Prerequisites
Run your code
Save this file to your machine and run it:
Try it out
When you call your Vonage Number you will hear a text-to-speech message.
Further Reading
- Interactive Voice Response (IVR) - Build an automated phone system for users to input information with the keypad and hear a spoken response.
- Voice Bot with Google Dialogflow - This guide will help you to start with an example Dialogflow bot and interact with it from phone calls using provided sample reference codes using Vonage Voice API.
- Masked Calling - Enable users to call each other, keeping their real numbers private.
- Conference Calling - This guide explains the two concepts Vonage associates with a call, a leg and a conversation.
- Call Tracking - Keep track of which campaigns are working well by using different numbers for each one and tracking the incoming calls. This guide shows you how to handle incoming calls, connect them to another number, and track the phone numbers that called each of your Vonage numbers.