分割音声で通話を録音
着信に応答し、次のように設定する方法を示すコード・スニペット。
通話を接続する。通話が完了すると
が完了すると eventUrl で指定した record NCCOのアクション
のアクションは、ダウンロード用の録画URLを含むウェブフックを受け取ります。
例
サンプルコードの以下の変数を置き換える:
| キー | 説明 |
|---|---|
VONAGE_VIRTUAL_NUMBER | Your Vonage Number. E.g. |
VOICE_TO_NUMBER | The recipient number to call, e.g. |
Prerequisites
npm install express body-parserWrite the code
Add the following to record-a-call-with-split-audio.js:
const Express = require('express');
const bodyParser = require('body-parser');
const app = new Express();
app.use(bodyParser.json());
const onInboundCall = (request, response) => {
const ncco = [
{
action: 'record',
split: 'conversation',
channels: 2,
eventUrl: [`${request.protocol}://${request.get('host')}/webhooks/recordings`],
},
{
action: 'connect',
from: VONAGE_VIRTUAL_NUMBER,
endpoint: [
{
type: 'phone',
number: VOICE_TO_NUMBER,
},
],
},
];
response.json(ncco);
};
const onRecording = (request, response) => {
const recording_url = request.body.recording_url;
console.log(`Recording URL = ${recording_url}`);
response.status(204).send();
};
app
.get('/webhooks/answer', onInboundCall)
.post('/webhooks/recordings', onRecording);
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:
Write the code
Add the following to the main method of the RecordCallSplitAudio class:
Run your code
We can use the アプリケーション 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 RecordCallSplitAudio:
Prerequisites
Add the following to build.gradle:
Write the code
Add the following to the main method of the RecordCallSplitAudio class:
/*
* Route to answer and connect incoming calls with recording.
*/
Route answerRoute = (req, res) -> {
String recordingUrl = String.format("%s://%s/webhooks/recordings", req.scheme(), req.host());
RecordAction record = RecordAction.builder()
.eventUrl(recordingUrl)
.channels(2)
.build();
ConnectAction connect = ConnectAction.builder(PhoneEndpoint.builder(VOICE_TO_NUMBER).build())
.from(VONAGE_VIRTUAL_NUMBER).build();
res.type("application/json");
return new Ncco(record, connect);
};
/*
* Route which prints out the recording URL it is given to stdout.
*/
Route recordingRoute = (req, res) -> {
System.out.println(EventWebhook.fromJson(req.body()).getRecordingUrl());
res.status(204);
return "";
};
Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/recordings", recordingRoute);Run your code
We can use the アプリケーション 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 RecordCallSplitAudio:
Prerequisites
Install-Package VonagePrerequisites
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]Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribWrite the code
Add the following to record-a-call-with-split-audio.rb:
require 'dotenv/load'
require 'sinatra'
require 'sinatra/multi_route'
require 'json'
VOICE_TO_NUMBER = ENV['VOICE_TO_NUMBER']
VONAGE_VIRTUAL_NUMBER = ENV['VONAGE_VIRTUAL_NUMBER']
before do
content_type :json
end
helpers do
def parsed_body
JSON.parse(request.body.read)
end
end
route :get, :post, '/webhooks/answer' do
[
{
"action": "record",
"split": "conversation",
"channels": 2,
"eventUrl": ["#{request.base_url}/webhooks/recordings"]
},
{
"action": "connect",
"from": VONAGE_VIRTUAL_NUMBER,
"endpoint": [
{
"type": "phone",
"number": VOICE_TO_NUMBER
}
]
}
].to_json
end
route :get, :post, '/webhooks/recordings' do
recording_url = params['recording_url'] || parsed_body['recording_url']
puts "Recording URL = #{recording_url}"
halt 204
end
set :port, 3000Run your code
Save this file to your machine and run it:
試してみる
必要なのは
- 通話に応答し、音声を分割して録音する(このコードスニペット)。
- 録画をダウンロードするを見る 録音をダウンロードする のコード・スニペットで説明している。