Skip to content

Commit

Permalink
refactor: updated voice snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Feb 28, 2025
1 parent d427330 commit 705f937
Show file tree
Hide file tree
Showing 30 changed files with 179 additions and 418 deletions.
18 changes: 10 additions & 8 deletions voice/answer-webhook-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

require('dotenv').config({ path: __dirname + '/../.env' });
const Express = require('express');
const bodyParser = require('body-parser');

const app = new Express();
const port = process.env.PORT || 3000;

const app = require('express')();
app.set('port', (process.env.PORT || 5000));
app.use(require('body-parser').urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/answer-webhook', function (req, res) {
app.get('/webhook/answer', (_, res) => {
// At this point you build an NCCO that fulfills your use case.
// For the purposes of an example we'll just read out some text.
const ncco = [
Expand All @@ -20,6 +21,7 @@ app.get('/answer-webhook', function (req, res) {
res.status(200);
});

app.listen(app.get('port'), function() {
console.log('Example app listening on port', app.get('port'));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});

16 changes: 9 additions & 7 deletions voice/conference-call.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
require('dotenv').config({ path: __dirname + '/../.env' });
const CONF_NAME = process.env.CONF_NAME;

const Express = require('express');

const app = new Express();
const bodyParser = require('body-parser');

const app = new Express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());

const onInboundCall = (request, response) => {
const VOICE_CONF_NAME = process.env.VOICE_CONF_NAME;

const onInboundCall = (_, response) => {
const ncco = [
{
action: 'talk',
text: 'Please wait while we connect you to the conference',
},
{
action: 'conversation',
name: CONF_NAME,
name: VOICE_CONF_NAME,
},
];

Expand All @@ -25,4 +25,6 @@ const onInboundCall = (request, response) => {

app.get('/webhooks/answer', onInboundCall);

app.listen(3000);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
15 changes: 9 additions & 6 deletions voice/connect-an-inbound-call.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
require('dotenv').config({ path: __dirname + '/../.env' });
const YOUR_SECOND_NUMBER = process.env.YOUR_SECOND_NUMBER;
const VONAGE_NUMBER = process.env.VONAGE_NUMBER;

const Express = require('express');

const app = new Express();
const port = process.env.PORT || 3000;

const VONAGE_NUMBER = process.env.VONAGE_NUMBER;
const VONAGE_VIRTUAL_NUMBER = process.env.VONAGE_VIRTUAL_NUMBER;

const onInboundCall = (request, response) => {
const onInboundCall = (_, response) => {
const ncco = [
{
action: 'connect',
from: VONAGE_NUMBER,
endpoint: [
{
type: 'phone',
number: YOUR_SECOND_NUMBER,
number: VONAGE_VIRTUAL_NUMBER,
},
],
},
Expand All @@ -25,4 +26,6 @@ const onInboundCall = (request, response) => {

app.get('/webhooks/answer', onInboundCall);

app.listen(3000);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
38 changes: 14 additions & 24 deletions voice/connect-synchronous.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
'use strict';

require('dotenv').config({ path: __dirname + '/../.env' });

const VONAGE_FROM_NUMBER = process.env.VONAGE_FROM_NUMBER;
const VONAGE_TO_NUMBER = process.env.VONAGE_TO_NUMBER;
const VONAGE_ALT_NUMBER = process.env.VONAGE_ALT_NUMBER;

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

const app = new Express();
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));

app.get('/answer', function (req, res) {
const port = process.env.PORT || 3000;
const VONAGE_VIRTUAL_NUMBER = process.env.VONAGE_VIRTUAL_NUMBER;
const VOICE_TO_NUMBER = process.env.VOICE_TO_NUMBER;

const serverHost = req.protocol + '://' + req.get('host');
app.get('/answer', (req, res) => {
const serverHost = req.protocol + '://' + req.host;

const ncco = [
{
action: 'connect',
from: VONAGE_FROM_NUMBER,
from: VONAGE_VIRTUAL_NUMBER,
timeout: 5,
eventType: 'synchronous',
eventUrl: [`${serverHost}/connect-event`],
endpoint: [
{
type: 'phone',
number: VONAGE_ALT_NUMBER,
number: VONAGE_VIRTUAL_NUMBER,
},
],
},
Expand All @@ -39,23 +35,17 @@ app.get('/answer', function (req, res) {

});

app.post('/connect-event', function(req, res) {
app.post('/connect-event', (req, res) => {
const ncco = [];

if(req.body.status === 'timeout') {
// Note: you cannot presently do this
// ncco.push({
// action: 'talk',
// text: 'Sorry, the attempt to connect your call timed out.'
// });

ncco.push({
action: 'connect',
from: VONAGE_FROM_NUMBER,
from: VONAGE_VIRTUAL_NUMBER,
endpoint: [
{
type: 'phone',
number: VONAGE_TO_NUMBER,
number: VOICE_TO_NUMBER,
},
],
});
Expand All @@ -69,6 +59,6 @@ app.post('/events', function(req, res) {
res.status(200);
});

const server = app.listen(process.env.PORT || 4004, () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
13 changes: 6 additions & 7 deletions voice/download-a-recording.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
require('dotenv').config({ path: __dirname + '/../.env' });
const { FileClient } = require('@vonage/server-client');

const VONAGE_PRIVATE_KEY = __dirname +'/../'+ process.env.VONAGE_PRIVATE_KEY;
const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;

const RECORDING_URL = process.env.RECORDING_URL;
const FILE_PATH = process.env.FILE_PATH;

const { FileClient } = require('@vonage/server-client');
const VOICE_RECORDING_URL = process.env.VOICE_RECORDING_URL;
const VOICE_RECORDING_DESTINATION = process.env.VOICE_RECORDING_DESTINATION;

const fileClient = new FileClient({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});

fileClient.downloadFile(
RECORDING_URL,
FILE_PATH,
VOICE_RECORDING_URL,
VOICE_RECORDING_DESTINATION,
)
.then(() => console.log(`File Downloaded to ${FILE_PATH}`))
.then(() => console.log(`File Downloaded to ${VOICE_RECORDING_DESTINATION}`))
.catch((error) => console.error(error));

12 changes: 5 additions & 7 deletions voice/earmuff-a-call.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
require('dotenv').config({ path: __dirname + '/../.env' });
const { Vonage } = require('@vonage/server-sdk');

const VONAGE_PRIVATE_KEY = __dirname +'/../'+ process.env.VONAGE_PRIVATE_KEY;
const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;

const UUID = process.env.UUID;

const { Vonage } = require('@vonage/server-sdk');
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;
const VOICE_CALL_ID = process.env.VOICE_CALL_ID;

const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});

vonage.voice.earmuffCall(UUID)
vonage.voice.earmuffCall(VOICE_CALL_ID)
.then(() => console.log('Call earmuffed'))
.catch((error) => console.error(error));

const unearmuff = () => {
vonage.voice.unearmuffCall(UUID)
vonage.voice.unearmuffCall(VOICE_CALL_ID)
.then(() => console.log('Unearmuffed call'))
.catch((error) => console.error(error));
};
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions voice/asr.js → voice/handle-user-input.asr.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require('dotenv').config({ path: __dirname + '/../.env' });
const Express = require('express');
const bodyParser = require('body-parser');

const app = new Express();
app.use(bodyParser.json());
const port = process.env.PORT || 3000;

const onInboundCall = (request, response) => {
const ncco = [
Expand Down Expand Up @@ -42,4 +42,6 @@ app
.get('/webhooks/answer', onInboundCall)
.post('/webhooks/asr', onInput);

app.listen(3000);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
39 changes: 0 additions & 39 deletions voice/ivr-menu.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
require('dotenv').config({ path: __dirname + '/../.env' });

const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
const VONAGE_APPLICATION_PRIVATE_KEY_PATH = __dirname + '/../' + process.env.VONAGE_APPLICATION_PRIVATE_KEY_PATH;

const TO_NUMBER = process.env.TO_NUMBER;
const VONAGE_NUMBER = process.env.VONAGE_NUMBER;

const { Vonage } = require('@vonage/server-sdk');
const { NCCOBuilder, Talk } = require('@vonage/voice');

const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;
const VOICE_TO_NUMBER = process.env.VOICE_TO_NUMBER;
const VONAGE_VIRTUAL_NUMBER = process.env.VONAGE_VIRTUAL_NUMBER;

const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH,
privateKey: VONAGE_PRIVATE_KEY,
});

const builder = new NCCOBuilder();
Expand All @@ -22,11 +20,11 @@ vonage.voice.createOutboundCall({
to: [
{
type: 'phone',
number: TO_NUMBER,
number: VOICE_TO_NUMBER,
},
{
type: 'phone',
number: VONAGE_NUMBER,
number: VONAGE_VIRTUAL_NUMBER,
},
],
})
Expand Down
29 changes: 29 additions & 0 deletions voice/make-an-outbound-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require('dotenv').config({ path: __dirname + '/../.env' });
const { Vonage } = require('@vonage/server-sdk');

const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;
const VOICE_TO_NUMBER = process.env.VOICE_TO_NUMBER;
const VONAGE_VIRTUAL_NUMBER = process.env.VONAGE_VIRTUAL_NUMBER;
const VOICE_ANSWER_URL = process.env.VOICE_ANSWER_URL;

const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});

vonage.voice.createOutboundCall({
to: [
{
type: 'phone',
number: VOICE_TO_NUMBER,
},
],
from: {
type: 'phone',
number: VONAGE_VIRTUAL_NUMBER,
},
answer_url: [VOICE_ANSWER_URL],
})
.then((resp) => console.log(resp))
.catch((error) => console.error(error));
31 changes: 0 additions & 31 deletions voice/make-call.js

This file was deleted.

Loading

0 comments on commit 705f937

Please sign in to comment.