Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions caller-id-forwarding/functions/forward-call.protected.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
/**
* Handles incoming calls, performs a lookup for caller information, sends a message with the caller details, and forwards the call to a specified number.
*
* @param {Object} context - The context object containing environment variables and Twilio client.
* @param {Object} event - The event object containing details of the incoming call.
* @param {Function} callback - The callback function to return the TwiML response.
*/
exports.handler = async function (context, event, callback) {
try {
const client = context.getTwilioClient();

const lookup = await client.lookups.v1
/*
* Lookup the incoming phone number
* Request both the "caller_name" and "line_type_intelligence" data packages
*/
const lookup = await client.lookups.v2
.phoneNumbers(event.From)
.fetch({ type: ['carrier', 'caller-name'] });
.fetch({ fields: 'caller_name,line_type_intelligence' });

const callerName = lookup.callerName.caller_name;
const carrierName = lookup.carrier.name;
const carrierName = lookup.lineTypeIntelligence.carrier_name;

const body = `Incoming call from ${event.From}
Name: ${callerName === null ? 'Unknown' : callerName}
Carrier: ${carrierName} (${lookup.carrier.type})`;
Carrier: ${carrierName} (${lookup.lineTypeIntelligence.type})`;

// Send a message to your phone number with the caller details
await client.messages.create({
body,
from: context.TWILIO_PHONE_NUMBER,
Expand All @@ -21,7 +33,7 @@ Carrier: ${carrierName} (${lookup.carrier.type})`;
} catch (error) {
console.error(error);
} finally {
// forward the call even if caller ID fails
// Forward the call even if caller ID fails
const twiml = new Twilio.twiml.VoiceResponse();
twiml.dial(context.MY_PHONE_NUMBER);
return callback(null, twiml);
Expand Down
36 changes: 18 additions & 18 deletions caller-id-forwarding/tests/forward-call.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable camelcase */
const helpers = require('../../test/test-helper');
const forwardCall = require('../functions/forward-call.protected').handler;

const mockFetch = jest.fn().mockResolvedValue({
carrier: {
name: 'Verizon',
lineTypeIntelligence: {
carrier_name: 'Verizon',
type: 'mobile',
},
callerName: {
// eslint-disable-next-line camelcase
caller_name: 'Lottie Matthews',
},
});

const mockClient = {
lookups: {
v1: {
v2: {
phoneNumbers: jest.fn(() => ({
fetch: mockFetch,
})),
Expand Down Expand Up @@ -56,11 +56,11 @@ test('forwards the call to the number from the context', (done) => {

test('looks up the incoming phone number', (done) => {
const expectedParams = {
type: ['carrier', 'caller-name'],
fields: 'caller_name,line_type_intelligence',
};
const callback = (_err, result) => {
expect(result).toBeDefined();
expect(mockClient.lookups.v1.phoneNumbers).toHaveBeenCalledWith(event.From);
expect(mockClient.lookups.v2.phoneNumbers).toHaveBeenCalledWith(event.From);
expect(mockFetch).toHaveBeenCalledWith(expectedParams);
done();
};
Expand All @@ -69,28 +69,28 @@ test('looks up the incoming phone number', (done) => {
});

test('sets the callername to Unknown if not returned', (done) => {
const callback = (_err, result) => {
expect(result).toBeDefined();
const expectedParams = {
body: `Incoming call from 54321
const expectedParams = {
body: `Incoming call from 54321
Name: Unknown
Carrier: Verizon (mobile)`,
to: context.MY_PHONE_NUMBER,
from: context.TWILIO_PHONE_NUMBER,
};
to: context.MY_PHONE_NUMBER,
from: context.TWILIO_PHONE_NUMBER,
};

const callback = (_err, result) => {
expect(result).toBeDefined();
expect(mockClient.messages.create).toHaveBeenCalledWith(expectedParams);
done();
};

mockFetch.mockResolvedValueOnce({
lineTypeIntelligence: {
carrier_name: 'Verizon',
type: 'mobile',
},
callerName: {
// eslint-disable-next-line camelcase
caller_name: null,
},
carrier: {
name: 'Verizon',
type: 'mobile',
},
});

forwardCall(context, event, callback);
Expand Down
Loading