音声とタッチトーン入力によるシンプルなIVRの構築方法

アン インタラクティブ・ボイス・レスポンス(IVR) は、発信者にオプションメニューを提供することで、電話応対を自動化します。従来のIVRがキーパッド(DTMF)の入力を含むことが多い。 音声合成 (ASR) より自然なユーザーエクスペリエンスのために。以下のリンクを参照してください。 高度なIVR のガイドを参照されたい。

このガイドでは、通話に応答し、ユーザーにキーを押すか話すかを促すNode.jsアプリケーションを構築します。アプリケーションは、その入力を繰り返し発信者に返します。

前提条件

  • Vonage APIアカウント。 無料会員登録.
  • Node.js あなたのマシンにインストールされています。
  • ングロク あなたのマシンにインストールされています。

プロジェクトフォルダを初期化する

Vonageリソースを設定する前に、コードのホームを作成してください。これにより、後でセキュリティ・キーの保存先を確保できます。

mkdir simple-ivr && cd simple-ivr npm init -y npm install express body-parser

ローカルサーバーの公開

Vonage needs to send webhooks to your local machine. Use ngrok to expose your server:

ngrok http 3000

ngrok will forward your local port 3000 to a public URL like https://{random-id}.ngrok.app.

Important: Keep this terminal window open while developing and testing. If you close ngrok, you’ll need to update your webhook URLs with the new address.

Copy this URL. You’ll need it when configuring your Vonage application in the next step.

Note: The free version of ngrok generates a new random URL each time you restart it. For a consistent URL during development, consider using ngrok reserved domains or upgrading to a paid plan.

Vonageリソースのプロビジョニング

を使用して環境を設定します。 Vonage APIダッシュボード.

音声アプリケーションの作成

  1. に移動する。 Applications > 新規アプリケーションの作成.
  2. アプリケーションに名前を付けます(例:Simple-IVR-Speech-DTMF)。
  3. アンダー 能力有効にする .
  4. を設定する。 回答URL を ngrok URL に追加します。 /webhooks/answer を追加した。例 https://{random-id}.ngrok.app/webhooks/answer.メソッドを
    GET
    に設定する。
  5. を設定する。 イベントURL を ngrok URL に追加します。 /webhooks/events を追加した。例 https://{random-id}.ngrok.app/webhooks/events.メソッドを
    POST
    に設定します。
  6. クリック 公開鍵と秘密鍵の生成.
  7. ダウンロードした private.key ファイルをsimple-ivrのプロジェクトフォルダに追加します。
  8. クリック 変更を保存する.

仮想番号をリンクする

  1. に移動する。 Numbers > ナンバーズを買う でナンバーを借りる。 の能力がある。
  2. 戻る アプリケーションIVRアプリケーションを選択し、新しい番号をリンクします。

インバウンドコールへの対応

ユーザーがあなたの番号に電話をかけると、VonageはNCCO(呼び出し制御オブジェクトという名前のファイルを作成します。という名前のファイルを作成します。 index.js そして以下のコードを追加する:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

// 1. Initial greeting and input request
app.get('/webhooks/answer', (req, res) => {
  const ncco = [
    {
      action: 'talk',
      text: 'Hello. Please enter a digit or say something.',
      bargeIn: true
    },
    {
      action: 'input',
      type: ['dtmf', 'speech'],
      dtmf: { maxDigits: 1 },
      speech: { language: 'en-us' },
      eventUrl: [`${req.protocol}://${req.get('host')}/webhooks/input`]
    }
  ];

  res.json(ncco);
});

音声およびDTMF入力の処理

入力ハンドラを index.js ユーザがメニューを操作すると、Vonageが送り返すペイロードを処理する。

// 2. Handle the user's response
app.post('/webhooks/input', (req, res) => {
  let responseText = "I'm sorry, I didn't catch that.";

  if (req.body.dtmf && req.body.dtmf.digits) {
    responseText = `You pressed ${req.body.dtmf.digits}.`;
  }
  else if (req.body.speech && req.body.speech.results) {
    const transcript = req.body.speech.results[0].text;
    responseText = `You said: ${transcript}.`;
  }

  res.json([{ action: 'talk', text: responseText }]);
});

// 3. Log call events
app.post('/webhooks/events', (req, res) => {
  res.sendStatus(200);
});

app.listen(3000, () => console.log(`IVR server running on port 3000`));

IVRのテスト

  1. サーバーを起動してください: node index.js.
  2. Vonageの番号に電話する:
  • キーパッド1を押します。IVRが表示されます、 あなたは1を押した。
  • スピーチと言う こんにちは。 IVRはこう言うべきだ、 あなたは言った:こんにちは。

次のステップ

  1. 電話転送:を追加する connect を介したユーザー入力に基づいて、発信者を関連部門に接続するアクション。 電話番号、SIPエンドポイント、またはWebアプリケーション Client SDKを使用します。
  2. 通話録音: 記録と転写 との通話 record アクションだ。
  3. 音声AI: ユーザーの入力をAIエージェントに渡す 通話相手に貴重な情報を提供する。
  4. カスタマイズ:変更 音声合成 を使うか ストリーム アクションで、音声合成の代わりに録音済みのMP3ファイルを再生します。