NCCOにアウトバウンドコールをかける
このコード・スニペットは、アウトバウンドコールを発信し、通話に応答すると音声合成メッセージを再生します。
音声合成メッセージを再生します。このコードでは
サーバーで answer_url このコード・スニペットを実行するには、リクエストの一部として
リクエストの一部として
例
サンプルコードの以下の変数を置き換える:
| キー | 説明 |
|---|---|
VONAGE_VIRTUAL_NUMBER | Your Vonage Number. E.g. |
VOICE_TO_NUMBER | The recipient number to call, e.g. |
Prerequisites
Execute the following command at your terminal prompt to create the JWT for authentication:
export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)Write the code
Add the following to make-an-outbound-call-with-ncco.sh:
curl -X POST https://api.nexmo.com/v1/calls\
-H "Authorization: Bearer $JWT"\
-H "Content-Type: application/json"\
-d '{"to":[{"type": "phone","number": "'$VOICE_TO_NUMBER'"}],
"from": {"type": "phone","number": "'$VONAGE_VIRTUAL_NUMBER'"},
"ncco": [
{
"action": "talk",
"text": "This is a text to speech call from Vonage"
}
]}'
Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdk @vonage/voiceCreate a file named make-an-outbound-call-with-ncco.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const { NCCOBuilder, Talk } = require('@vonage/voice');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Write the code
Add the following to make-an-outbound-call-with-ncco.js:
const builder = new NCCOBuilder();
builder.addAction(new Talk('This is a text to speech call from Vonage'));
vonage.voice.createOutboundCall({
to: [
{
type: 'phone',
number: VOICE_TO_NUMBER,
},
],
from: {
type: 'phone',
number: VONAGE_VIRTUAL_NUMBER,
},
ncco: builder.build(),
})
.then((result) => console.log(result))
.catch((error) => console.error(error));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'Create a class named OutboundTextToSpeechCallWithNcco and add the following code to the main method:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Write the code
Add the following to the main method of the OutboundTextToSpeechCallWithNcco class:
val callEvent = client.voice.createCall {
toPstn(VOICE_TO_NUMBER)
from(VONAGE_VIRTUAL_NUMBER)
ncco(
talkAction("This is a text to speech call from Vonage")
)
}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 OutboundTextToSpeechCallWithNcco:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named OutboundTextToSpeechWithNcco and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the OutboundTextToSpeechWithNcco class:
Ncco ncco = new Ncco(TalkAction.builder("This is a text to speech call from Vonage").build());
client.getVoiceClient().createCall(new Call(VOICE_TO_NUMBER, VONAGE_VIRTUAL_NUMBER, ncco.getActions()));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 OutboundTextToSpeechWithNcco:
Prerequisites
Install-Package VonageWrite the code
Add the following to MakeCallWithNcco.cs:
var client = new VonageClient(creds);
var toEndpoint = new PhoneEndpoint() { Number = VOICE_TO_NUMBER };
var fromEndpoint = new PhoneEndpoint() { Number = VONAGE_VIRTUAL_NUMBER };
var extraText = "";
for (var i = 0; i < 50; i++)
extraText += $"{i} ";
var talkAction = new TalkAction() { Text = "This is a text to speech call from Vonage " + extraText };
var ncco = new Ncco(talkAction);
var command = new CallCommand() { To = new Endpoint[] { toEndpoint }, From = fromEndpoint, Ncco = ncco };
var response = await client.VoiceClient.CreateCallAsync(command);
Prerequisites
composer require vonage/clientRun your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenvWrite the code
Add the following to make-outbound-call-ncco.py:
from vonage import Auth, Vonage
from vonage_voice import CreateCallRequest, Phone, Talk, ToPhone
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY,
)
)
response = client.voice.create_call(
CreateCallRequest(
ncco=[Talk(text='This is a text to speech call from Vonage.')],
to=[ToPhone(number=VOICE_TO_NUMBER)],
from_=Phone(number=VONAGE_VIRTUAL_NUMBER),
)
)
pprint(response)Run your code
Save this file to your machine and run it:
Prerequisites
gem install vonageRun your code
Save this file to your machine and run it:
試してみる
コードを実行すると VOICE_TO_NUMBER が呼び出され、通話に応答すると音声合成メッセージが流れます。
さらに読む
- 音声通知 - このガイドでは、電話でリストに連絡し、メッセージを伝え、誰がメッセージを受け取ったかを確認する方法を学びます。このような音声ベースのクリティカルアラートは、テキストメッセージよりも持続性があるため、あなたのメッセージに気づいてもらえる可能性が高くなります。さらに、受信者の確認により、メッセージが届いたことを確認することができます。
- 電話会議 - このガイドでは、Vonageが通話に関連付ける2つの概念、レグと会話について説明します。
- Googleダイアログフローによる音声ボット - このガイドでは、Vonage Voice APIを使用したDialogflowボットのサンプルコードを使って、Dialogflowボットと対話する方法を説明します。