|
| 1 | +package sip |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/livekit/protocol/livekit" |
| 5 | + "github.com/nyaruka/phonenumbers" |
| 6 | +) |
| 7 | + |
| 8 | +// ExtractAreaCode extracts the area code from a phone number using the phonenumbers library |
| 9 | +func ExtractAreaCode(phoneNumber string) string { |
| 10 | + // Parse the phone number without defaulting to any country |
| 11 | + num, err := phonenumbers.Parse(phoneNumber, "") |
| 12 | + if err != nil { |
| 13 | + // If parsing fails, fall back to empty string |
| 14 | + return "" |
| 15 | + } |
| 16 | + |
| 17 | + // Get the country code |
| 18 | + countryCode := phonenumbers.GetRegionCodeForNumber(num) |
| 19 | + |
| 20 | + // Only handle US numbers for now |
| 21 | + if countryCode != "US" { |
| 22 | + return "" |
| 23 | + } |
| 24 | + |
| 25 | + // Get the national number and extract first 3 digits (area code for US) |
| 26 | + nationalNumber := phonenumbers.GetNationalSignificantNumber(num) |
| 27 | + if len(nationalNumber) < 3 { |
| 28 | + return "" |
| 29 | + } |
| 30 | + return nationalNumber[:3] |
| 31 | +} |
| 32 | + |
| 33 | +// DetermineNumberType determines the phone number type using the phonenumbers library |
| 34 | +func DetermineNumberType(phoneNumber string) livekit.PhoneNumberType { |
| 35 | + // Parse the phone number without defaulting to any country |
| 36 | + num, err := phonenumbers.Parse(phoneNumber, "") |
| 37 | + if err != nil { |
| 38 | + // If parsing fails, fall back to unknown |
| 39 | + return livekit.PhoneNumberType_PHONE_NUMBER_TYPE_UNKNOWN |
| 40 | + } |
| 41 | + |
| 42 | + numberType := phonenumbers.GetNumberType(num) |
| 43 | + |
| 44 | + // We are excluding a bunch of number types for now |
| 45 | + switch numberType { |
| 46 | + case phonenumbers.MOBILE: |
| 47 | + return livekit.PhoneNumberType_PHONE_NUMBER_TYPE_MOBILE |
| 48 | + case phonenumbers.FIXED_LINE: |
| 49 | + return livekit.PhoneNumberType_PHONE_NUMBER_TYPE_LOCAL |
| 50 | + case phonenumbers.TOLL_FREE: |
| 51 | + return livekit.PhoneNumberType_PHONE_NUMBER_TYPE_TOLL_FREE |
| 52 | + default: |
| 53 | + return livekit.PhoneNumberType_PHONE_NUMBER_TYPE_UNKNOWN |
| 54 | + } |
| 55 | +} |
0 commit comments