Skip to content

Commit

Permalink
upgrade twilio lookup to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
engelhartrueben committed Sep 30, 2024
1 parent 6407b60 commit 9bf586c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/extensions/service-vendors/twilio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,10 @@ export async function getContactInfo({
return {};
}
const twilio = await exports.getTwilio(organization);
const types = ["carrier"];
const types = { fields: "line_type_intelligence" };
if (lookupName) {
// caller-name is more expensive
types.push("caller-name");
types.fields = ("line_type_intelligence,caller-name");
}
const contactInfo = {
contact_number: contactNumber,
Expand All @@ -646,31 +646,33 @@ export async function getContactInfo({
try {
const phoneNumber = await twilio.lookups.v1
.phoneNumbers(contactNumber)
.fetch({ type: types });
.fetch(types);

if (phoneNumber.carrier) {
contactInfo.carrier = phoneNumber.carrier.name;
if (phoneNumber.lineTypeIntelligence.carrier_name) {
contactInfo.carrier = phoneNumber.lineTypeIntelligence.carrier_name;
}
if (phoneNumber.carrier.error_code) {
if (phoneNumber.lineTypeIntelligence.error_code) {
// e.g. 60600: Unprovisioned or Out of Coverage
contactInfo.status_code = -2;
contactInfo.last_error_code = phoneNumber.carrier.error_code;
contactInfo.last_error_code = phoneNumber.lineTypeIntelligence.error_code;
} else if (
phoneNumber.carrier.type &&
phoneNumber.carrier.type === "landline"
// mobile or landline
phoneNumber.lineTypeIntelligence.type &&
phoneNumber.lineTypeIntelligence.type === "landline"
) {
// landline (not mobile or voip)
contactInfo.status_code = -1;
} else if (
// example: US
phoneNumber.countryCode &&
getConfig("PHONE_NUMBER_COUNTRY", organization) &&
getConfig("PHONE_NUMBER_COUNTRY", organization) !==
phoneNumber.countryCode
) {
contactInfo.status_code = -3; // wrong country
} else if (
phoneNumber.carrier.type &&
phoneNumber.carrier.type !== "landline"
phoneNumber.lineTypeIntelligence.type &&
phoneNumber.lineTypeIntelligence.type !== "landline"
) {
// mobile, voip
contactInfo.status_code = 1;
Expand Down

0 comments on commit 9bf586c

Please sign in to comment.