Skip to content

Instantly share code, notes, and snippets.

@cweems
Created February 17, 2022 18:23
Show Gist options
  • Save cweems/1e0b2380c98f410f105956ebaf68eb02 to your computer and use it in GitHub Desktop.
Save cweems/1e0b2380c98f410f105956ebaf68eb02 to your computer and use it in GitHub Desktop.
/*
This function takes a phone number and conversations SID
as inputs and checks if there is an existing session
defined between the recipient's number and the proxy address.
If there is an existing session, it queries phone numbers and
picks a new proxy address.
@param string address - the user's phone number (urlencoded)
@param string conversationSid - the SID of the conversation
*/
exports.handler = async function(context, event, callback) {
try {
const client = context.getTwilioClient();
let address = decodeURI(event.address);
// Get an array of Twilio numbers that are currently
// being used by the customer as proxy_addresses
const inUseAddresses = await client.conversations.participantConversations
.list({address: address})
.then(participantConversations => {
let proxyAddresses = participantConversations.map(p => {
console.log(p.participantMessagingBinding.proxy_address);
return p.participantMessagingBinding.proxy_address;
})
console.log(proxyAddresses);
return proxyAddresses;
});
// Query Twilio numbers and find one that is
// not being used as a proxy_address
const newProxyAddress = await client.incomingPhoneNumbers
.list()
.then(incomingPhoneNumbers => {
let phoneNumber = "";
for (let el of incomingPhoneNumbers) {
if (!inUseAddresses.includes(el.phoneNumber)) {
phoneNumber = el.phoneNumber;
break;
}
}
return phoneNumber;
});
// Add the conversation participant with the new Proxy Address
const participantSid = await client.conversations.conversations(event.conversationSid)
.participants
.create({
'messagingBinding.address': address,
'messagingBinding.proxyAddress': newProxyAddress
})
.then(participant => participant.sid);
callback(null, participantSid);
} catch (err) {
callback(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment