Node.jsで通話を録音する
In this guide we'll show you how to use Programmable Voice to record phone calls with your Node.js web application. You can tell Twilio to record part of a phone call or the entire thing. The code snippets in this guide are written using modern JavaScript language features in Node.js version 6 or higher, and make use of the following modules:
はじめましょう!
Webアプリケーションを設定する
While it might be fun to program a robot to answer a physical phone, Twilio makes answering a phone call as easy as responding to an HTTP request. When a phone number you have bought through Twilio receives an incoming call, Twilio will send an HTTP request to a server you control, asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that tells Twilio to read out a message, play an MP3 file, make a recording, and much more.
通話への応答を開始するには、以下のことが必要になります:
- Buy and configure a Twilio-powered phone number capable of making and receiving phone calls, and point it at your web application
- Webアプリケーションのコードを記述し、(TwiMLを使った)着信通話の処理方法をTwilioに指示します。
- 通話の着信があったときにTwilioがWebhookのリクエストを送信できるよう、Webアプリケーションをインターネットからアクセス可能にしておきます
電話番号の購入と設定
In the console, you can search for and buy phone numbers in dozens of different countries, capable of calling (and being called by) just about every phone on the planet.
Once you purchase a number, you'll need to configure that number to send a request to your web application. This callback mechanism is called a webhook. This can be done in the number's configuration page.
Webhookとは何か
A webhook is a callback mechanism that allows two systems to communicate events to one another over the Internet using HTTP requests. In this case, Twilio is sending a webhook request to your web application whenever a phone number you control receives an incoming call. You'll see this webhook mechanism used in many Twilio APIs for handling event notifications like this.
インターネットからアクセスできるサーバーで作業をしていないですって? ローカルの開発マシンをインターネット上からアクセスできるようにする方法については、後ほどこのガイドのご紹介します。 続いて、通話が着信通話があったときに実行されるサーバー側のコードを書く必要があります。
通話の一部を録音する
ここからがいよいよお楽しみ - Twilioから受信されてきたHTTPリクエストを処理するコードの記述です!
In this example we'll use the Express web framework for Node.js to respond to Twilio's request and we'll use TwiML to tell Twilio how to handle the call.
TwiML is a set of XML tags that tell Twilio how to handle an incoming call (or SMS). In this example we tell Twilio to read some instructions to the caller and then record whatever the caller says next.
You can listen to your recordings in your Twilio Console or access them directly through Twilio's REST API.
録音データのテキスト化 (日本語未対応)
録音データのテキスト化、つまり発信者が話した内容のテキスト表現を取得するようTwilioに指示することもできます。
ここでは、応答に "transcribe: true" を追加して、録音の終了後、それをテキスト化するようTwilioに指示します。 また、録音の長さ(既定では1時間)を制限するため、 "maxLength"引数も渡します。
<Record> のリファレンス・ドキュメントを参照して、録音データをカスタマイズできるすべてのパラメーターについてご覧ください。
発信通話全体を録音する
Twilio REST APIで通話を発信する際、通話の始めから終わりまでの通話全体を録音するよう、Twilioに指示できます。
Twilioアカウントの認証情報を取得する
First, you'll need to get your Twilio account credentials. They can be found on the home page of the console.
通話の発信と録音
"client.calls.create()" に、追加で "record" 引数を渡すだけで、Twilioは通話全体を録音します。
Once the call is complete, you can listen to your recordings in your Twilio Console or access them directly through Twilio's REST API.
You can also gain access to the recording as soon as the call is complete by including a recordingStatusCallback
with your client.calls.create
method. When your recording is ready, Twilio will send a request to the URL you specified, and that request will include a link to the recording's audio file.
You can learn more about the RecordingStatusCallback
parameter in the Call Resource API documentation.
関連トピック
このガイドがお役に立ったなら、プログラマブルVoiceとNode.js向けの以下のチュートリアルも、ご興味を持たれるかもしれません。 チュートリアルではこうしたTwilioの活用事例を実装しているサンプルアプリケーションについて順を追って紹介しています。
楽しいプログラミングを!
ヘルプが必要ですか?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.