From 784afee7bf2095952acbc360107a0fdcfd91fba1 Mon Sep 17 00:00:00 2001 From: Amanda Cavallaro Date: Tue, 16 May 2023 13:46:16 +0100 Subject: [PATCH] Updates project --- README.md | 45 +++++++++++++++++++++++++++++++++------------ package.json | 2 +- server.js | 11 +++++------ 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 870d351..360dd26 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,37 @@ -This is the sample code for the "Streaming Audio into a Call with Node.js" blog post. +# Streaming Audio into a Call with Node.js -## Running the application +This is the sample code for the [Streaming Audio into a Call with Node.js](https://developer.vonage.com/en/blog/stream-audio-into-a-phone-call-with-node-js) blog post. +This project showcases how to stream audio into a call using Node.js. It uses the Vonage Server SDK to make a call to a specified number, then streams audio into the call. -1. Create a new voice app and link an existing number to it, or purchase a new one. Make a note of the application id. -2. Download the private key to your application directory. -3. Run `ngrok http 3000` to start tunneling. -4. Change the hostname of the `silence.mp3` URL in `/public/answer.json` to match your `ngrok` hostname. -5. Copy `example.env` to `.env` and configure your API key, secret, private key file path, to/from numbers and `ngrok` URL. -6. Run `npm install` to install dependencies. -7. Run `node server.js` in the root directory to start the app. -8. Make a `GET` request to `http://localhost:3000/call` to call the configured `TO_NUMBER`. +## Prerequisites -If all goes well, you should hear a message followed by silence. A few seconds later you will hear some music for 30 seconds which then stops. +- Node.js and npm installed. +- A Vonage account with a Voice API application and linked number. +- Ngrok installed and set up. +- A public domain music file. -**Note**: The music file this application uses (*City Sunshine* by Kevin MacLeod) is in the public domain. See https://freepd.com. +## Setup and Installation + +Follow the steps below to set up and run this application: + +1. **Create a Voice API application:** Use your Vonage account to create a new voice app. Link an existing number to it, or purchase a new one. Note down the application id. + +2. **Download the private key:** Download the private key associated with your voice app to your application directory. + +3. **Start an ngrok tunnel:** Run `ngrok http 3000` in your terminal to start an ngrok tunnel. + +4. **Update the silence.mp3 URL:** In the `/public/answer.json` file, change the hostname of the `silence.mp3` URL to match your ngrok hostname. + +5. **Configure environment variables:** Rename the `example.env` file to `.env` and update the variables with your API key, secret, private key file path, and your ngrok URL. Also, specify the 'to' and 'from' numbers for making the call. + +6. **Install dependencies:** Run `npm install` in your terminal from the root directory of the project. + +7. **Start the application:** Run `node server.js` to start the app. + +## Usage + +To initiate a call, make a `GET` request to `http://localhost:3000/call`. This will call the configured `TO_NUMBER`. + +If all goes well, you will first hear a message, followed by silence. After a few seconds, music will play for 30 seconds and then stop. + +**Note**: The music file this application uses (*City Sunshine* by Kevin MacLeod) is in the public domain. For more information, visit [FreePD](https://freepd.com). diff --git a/package.json b/package.json index a063222..5d6ea90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "voice-stream-audio-node", - "version": "1.0.0", + "version": "1.0.1", "description": "Play audio into a call", "main": "server.js", "scripts": { diff --git a/server.js b/server.js index 9fa23b4..d9a60b1 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,9 @@ require('dotenv').config({ path: '.env' }) -const path = require('path') const express = require('express') const app = express() -const bodyParser = require('body-parser') +const path = require('path') const Vonage = require('@vonage/server-sdk') -app.use(bodyParser.json()) -app.use(bodyParser.urlencoded({ extended: true })) - const TO_NUMBER = process.env.TO_NUMBER const VONAGE_NUMBER = process.env.VONAGE_NUMBER const BASE_URL = process.env.BASE_URL @@ -24,12 +20,15 @@ const vonage = new Vonage({ privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH }) +app.use(express.json()) +app.use(express.urlencoded({ extended: true })) + // Serve contents of public folder in the /audio path app.use('/audio', express.static(path.join(__dirname, 'public'))) const answer_url = BASE_URL + '/audio/answer.json' const audio_url = BASE_URL + '/audio/music.mp3' -const event_url = BASE_URL + `/webhooks/events` +const event_url = BASE_URL + '/webhooks/events' const makeOutboundCall = (req, res) => { console.log('Making the outbound call...')