着信コールを受ける
このコード・スニペットでは、着信コールを受信する方法を示している。
例
Prerequisites
npm install expressWrite 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:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'Write the code
Add the following to the main method of the ReceiveInboundCall class:
embeddedServer(Netty, port = 8000) {
routing {
get("/webhooks/answer") {
val from = call.request.queryParameters["from"]?.replace("", "")
call.response.header("Content-Type", "application/json")
call.respond(
Ncco(
talkAction("Thank you for calling from $from")
).toJson()
)
}
}
}.start(wait = true)Run your code
We can use the アプリケーション 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.kt.voice with the package containing ReceiveInboundCall:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'
implementation 'com.sparkjava:spark-core:2.9.4'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 アプリケーション 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:
Prerequisites
Install-Package VonageWrite 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
composer require slim/slim:^3.8 vonage/clientWrite 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
pip install vonage python-dotenv fastapi[standard]Write the code
Add the following to receive-an-inbound-call.py:
from fastapi import FastAPI, Query
from vonage_voice import Talk
app = FastAPI()
@app.get('/webhooks/answer')
async def answer_call(from_: str = Query(..., alias='from')):
from_ = '-'.join(from_)
return [
Talk(text=f'Thank you for calling from {from_}').model_dump(
by_alias=True, exclude_none=True
)
]Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribRun your code
Save this file to your machine and run it:
試してみる
Vonage番号に電話をかけると、音声合成メッセージが流れます。
さらに読む
- インタラクティブ・ボイス・レスポンス(IVR) - ユーザーがキーパッドで情報を入力し、音声応答を聞くための自動電話システムを構築する。
- Googleダイアログフローによる音声ボット - このガイドでは、Vonage Voice APIを使用したDialogflowボットのサンプルコードを使って、Dialogflowボットと対話する方法を説明します。
- マスクド・コーリング - ユーザー同士の通話を可能にし、実番号を非公開にする。
- 電話会議 - このガイドでは、Vonageが通話に関連付ける2つの概念、レグと会話について説明します。
- コールトラッキング - キャンペーンごとに異なる番号を使用し、着信を追跡することで、どのキャンペーンがうまくいっているかを把握できます。このガイドでは、着信を処理し、別の番号に接続し、Vonageの各番号に電話をかけた電話番号を追跡する方法を説明します。