Node.js

DTMFウェブフックの作成

を作成する。 /webhooks/dtmf ルートの下に以下のコードを入力する。 /webhooks/answer のルートだ:

app.post('/webhooks/dtmf', (req, res) => {
	let actions = [];
	let ncco = [];
	switch (req.body.dtmf.digits) {
		case '1':
			actions.push({
				action: 'talk',
				text: `It is ${new Intl.DateTimeFormat(undefined, {
					dateStyle: 'full',
					timeStyle: 'long',
				}).format(Date.now())}`,
			});
			break;
		case '2':
			actions.push({
				action: 'stream',
				streamUrl: [
					'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3',
				],
			});
	}
	ncco = actions.concat(mainMenu(req));

	console.log(ncco);

	res.json(ncco);
});

このコードでは、ユーザーがどの桁の数字を入力したかを確認するためにリクエストを調べます。 req.body.dtmf)を押し、適切なアクションを既存のNCCOに追加する。ユーザーが 1を追加する。 talk アクションで現在の日付と時刻を読み出す。ユーザーが 2を使用して、通話にオーディオファイルを再生します。 stream アクションを実行します。ユーザが他のキーを押した場合、そのキーは無視され、関数は元のNCCOを mainMenu 関数である。