Node.jsで電話会議を作成する
このガイドでは、node.jsのWebアプリケーションでプログラマブルVoiceを使って電話会議を作成、管理する方法についてご紹介します。 また会議をモニタリングしたり、通話の最中に会議に参加する方法についても触れていきます。 このガイドでのコード例は、Node.jsのバージョン6以降のモダンなJavaScript言語機能を使用して書かれており、以下のモジュールを利用しています。
準備はOK? さぁ、はじめましょう!
TwiML Binを使用する
A handy tool we provide to host static TwiML from the Twilio Console is called TwiML Bin.
Just go to the TwiML Bin page in the Developer Center and click the plus button to create a new TwiML Bin. You can then add any static TwiML you want to host. Here's an example:
「保存」をクリックすれば、お手持ちのTwilio電話番号でTwiML Binを使う準備は完了です。
TwiML Binで作成、設置されたTwiMLで、電話番号を設定してみましょう。
電話番号の購入と設定
Twilioコンソール上で、世界中の電話番号を検索、購入できます。 音声通話の可能な番号は、地球上のおよそどこからでも通話を発信および着信することができます。
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.
さぁ、この番号に電話をかけてみましょう。 最初につながった通話では保留用のBGMが流れ、他の電話がかかってくると電話会議が開始されます。
TwiML Binは単純な電話会議回線を設定するのに最適ですが、JavaScriptのパワーを借りればより多くのことが可能になります。 その方法を見ていきましょう。
Webアプリケーションを設定する
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 your web application asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that instructs Twilio on what to do with the call. Those instructions can direct 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がHTTPリクエストを発行できるように、Webアプリケーションをインターネットからアクセスできるようにします
司会者のいる動的な電話会議
ここからがいよいよお楽しみ - 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 the Twilio Node.js SDK to generate our TwiML.
このサンプルでは、「司会者」となる特定の参加者が電話会議をよりよくコントロールできるよう、<Conference> の上級機能をいくつか使用します。
- startConferenceOnEnter will keep all other callers on hold until the moderator joins
- endConferenceOnExit will cause Twilio to end the call for everyone as soon as the moderator leaves
発信者が司会者か、あるいは通常の参加者か確認するために、TwilioのWebhookのリクエスト中の "From" 属性をを使用します。
このコードサンプル上のWebhookが機能するためには、インターネット経由でTwilioがWebアプリケーションにHTTPリクエストを送信できなければなりません。 当然、アプリケーションはTwilioから到達できるURLまたはIPアドレスを持っている必要があるということです。
In production you probably have a public URL, but you probably don't during development. That's where ngrok comes in. ngrok gives you a public URL for a local port on your development machine, which you can use to configure your Twilio webhooks as described above.
いったんngrokがインストールされたなら、コマンドラインからWebアプリケーションが実行されているいかなるポートへもトンネルを作成することができます。 たとえば、この例ではポート3000番をリスニングするWebアプリケーション用のURLを作成します。
ngrok http 3000
After executing that command, you will see that ngrok has given your application a public URL that you can use in your webhook configuration in the Twilio console.
Grab your ngrok public URL and head back to the phone number you configured earlier. Now let's switch it from using a TwiML Bin to use your new ngrok URL. Don't forget to append the URL path to your actual TwiML logic! ("http://<your ngrok subdomain>.ngrok.io/voice" for example)
これでExpressアプリケーションで動的な電話会議をホストする準備が整いました。 何人かお友達を誘って試してみましょう!
関連トピック
このガイドがお役に立ったなら、プログラマブル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.