インバウンドコールに接続する
このコードスニペットでは、インバウンドコールをアウトバウンドコールで別の相手に接続する方法を説明します。
例
サンプルコードの以下の変数を置き換える:
| キー | 説明 |
|---|---|
VONAGE_VIRTUAL_NUMBER | Your Vonage Number. E.g. |
VOICE_TO_NUMBER | The recipient number to call, e.g. |
Prerequisites
npm install expressWrite the code
Add the following to connect-an-inbound-call.js:
const Express = require('express');
const app = new Express();
const onInboundCall = (_, response) => {
const ncco = [
{
action: 'connect',
from: VONAGE_VIRTUAL_NUMBER,
endpoint: [
{
type: 'phone',
number: VOICE_TO_NUMBER,
},
],
},
];
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 ConnectInboundCall class:
embeddedServer(Netty, port = 8000) {
routing {
route("/webhooks/answer") {
handle {
call.response.header("Content-Type", "application/json")
call.respond(
Ncco(
connectToPstn(VOICE_TO_NUMBER) {
from(VONAGE_VIRTUAL_NUMBER)
}
).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 ConnectInboundCall:
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 ConnectInboundCall class:
/*
* Route to answer incoming calls with an NCCO response.
*/
Route answerRoute = (req, res) -> {
ConnectAction connect = ConnectAction.builder()
.endpoint(PhoneEndpoint.builder(VOICE_TO_NUMBER).build())
.from(VONAGE_VIRTUAL_NUMBER)
.build();
res.type("application/json");
return new Ncco(connect).toJson();
};
Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/answer", answerRoute);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 ConnectInboundCall:
Prerequisites
Install-Package VonageWrite the code
Add the following to ConnectInboundCallController.cs:
[Route("webhooks/answer")]
public string Answer()
{
var VOICE_TO_NUMBER = Environment.GetEnvironmentVariable("VOICE_TO_NUMBER") ?? "VOICE_TO_NUMBER";
var VONAGE_VIRTUAL_NUMBER = Environment.GetEnvironmentVariable("VONAGE_VIRTUAL_NUMBER") ?? "VONAGE_VIRTUAL_NUMBER";
var talkAction = new TalkAction()
{
Text = "Thank you for calling",
Language = "en-gb",
Style = 2
};
var secondNumberEndpoint = new PhoneEndpoint() { Number=VOICE_TO_NUMBER};
var connectAction = new ConnectAction() { From=VONAGE_VIRTUAL_NUMBER, Endpoint= new[] { secondNumberEndpoint } };
var ncco = new Ncco(talkAction,connectAction);
return ncco.ToString();
}Prerequisites
composer require slim/slim:^3.8 vonage/clientRun 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 connect-an-inbound-call.py:
import os
from os.path import dirname, join
from dotenv import load_dotenv
from fastapi import FastAPI
from vonage_voice import Connect, PhoneEndpoint
dotenv_path = join(dirname(__file__), '../.env')
load_dotenv(dotenv_path)
VONAGE_VIRTUAL_NUMBER = os.environ.get('VONAGE_VIRTUAL_NUMBER')
VOICE_TO_NUMBER = os.environ.get('VOICE_TO_NUMBER')
app = FastAPI()
@app.get('/webhooks/answer')
async def inbound_call():
ncco = [
Connect(
endpoint=[PhoneEndpoint(number=VOICE_TO_NUMBER)],
from_=VONAGE_VIRTUAL_NUMBER,
).model_dump(by_alias=True, exclude_none=True)
]
return nccoRun 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番号に電話をかけると、自動的に指定した番号につながります。
の代わりに指定した番号に自動的に接続されます。 VOICE_TO_NUMBER.
さらに読む
- インタラクティブ・ボイス・レスポンス(IVR) - ユーザーがキーパッドで情報を入力し、音声応答を聞くための自動電話システムを構築する。
- Googleダイアログフローによる音声ボット - このガイドでは、Vonage Voice APIを使用したDialogflowボットのサンプルコードを使って、Dialogflowボットと対話する方法を説明します。
- マスクド・コーリング - ユーザー同士の通話を可能にし、実番号を非公開にする。
- 電話会議 - このガイドでは、Vonageが通話に関連付ける2つの概念、レグと会話について説明します。
- コールトラッキング - キャンペーンごとに異なる番号を使用し、着信を追跡することで、どのキャンペーンがうまくいっているかを把握できます。このガイドでは、着信を処理し、別の番号に接続し、Vonageの各番号に電話をかけた電話番号を追跡する方法を説明します。