31
loading...
This website collects cookies to deliver better user experience
The Proxy API connects two parties together, allowing them to communicate and keep personal information private.
twilio-service.js
import Twilio from 'twilio'
const TWILIO_SERVICE_ID = 'YOUR_SERVICE_ID' // create a service here: https://console.twilio.com/us1/develop/proxy/services
const TWILIO_ACCOUNT_SID= 'YOUR_ACCOUNT_ID'
const TWILIO_AUTH_TOKEN = 'YOUR_AUTH_TOKEN'
const twilioService = function () {
let client
if (!client) {
client = new Twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, {
logLevel: 'debug'
})
delete client.constructor
}
return {
_client: client,
/**
* Creates new session to prepare a call
* docs: https://www.twilio.com/docs/proxy/api/session
* @returns
*/
createSession: async function () {
return client.proxy.services(TWILIO_SERVICE_ID).sessions.create({
mode: 'voice-only'
})
},
/**
* Delete a call sesion
* @param {string} sessionId The id of session
* docs: https://www.twilio.com/docs/proxy/api/session#delete-a-session-resource
* @returns
*/
deleteSession: async function (sessionId) {
return client
.proxy
.services(TWILIO_SERVICE_ID)
.sessions(sessionId)
.remove()
},
/**
* Add new participant to the call
* Maximum 2 participants per call are allowed
* docs: https://www.twilio.com/docs/proxy/api/participant
*
* @param {string} sessionId The id of session
* @param {string} phoneNumber The phone number of participant to call
* @returns
*/
addParticipantToSession: async function (sessionId, phoneNumber) {
return client
.proxy
.services(TWILIO_SERVICE_ID)
.sessions(sessionId)
.participants
.create({
identifier: phoneNumber
})
}
}
}
export default twilioService()
identifier
(their real phone number) and has assigned a new property proxyIdentifier
(the masked phone number).proxyIdentifier
, Twilio match their masked number with the masked number of the recipient, and call then the real number of the recipient.
import TwilioService from '../services/twilio';
const participant1Number = '+14156789012';
const participant2Number = '+17012345678';
const session = await TwilioService.createSession()
// session = {
// ...
// "sid": "KCaa270143d7a1ef87f743313a07d4069d",
// ...
// }
const participantFrom = await TwilioService.addParticipantToSession(session.sid,participant1Number)
// participantFrom = {
// "sid": "KPa4947c3d7b8ca7b0138dbc8181f12e16",
// "sessionSid": "KCaa270143d7a1ef87f743313a07d4069d",
// "identifier": "+14156789012",
// "proxyIdentifier": "+14153749231",
// }
const participantTo = await TwilioService.addParticipantToSession(session.sid, participant2Number)
participantTo = {
// "sid": "KP48f197284b3f50c5b31891ecc8377c20",
// "sessionSid": "KCaa270143d7a1ef87f743313a07d4069d",
// "identifier": "+17012345678",
// "proxyIdentifier": "+40371246711",
// }
proxyIdentifier
, +14153749231 , Twilio identifies your identity and match your masked phone number with the other paticipant, and call their real name +17012345678 , but you never know which is the real number of your companion speaker. To make the call from the other side, do the same.await TwilioService.deleteSession(session.sid);
31