Skip to content

Commit

Permalink
Merge pull request #15 from nexmo-community/update-project
Browse files Browse the repository at this point in the history
Updates project
  • Loading branch information
amdcavallaro authored May 16, 2023
2 parents 036331b + 784afee commit fdc7325
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
11 changes: 5 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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...')
Expand Down

0 comments on commit fdc7325

Please sign in to comment.