Skip to content

Commit 1818f02

Browse files
Adding utilities for phonenumber functionality (#1206)
* Adding utilities for phonenumber functionality 1. DB prefixes 2. Helper utilities for area code and number type * Adding changeset * Adding enum for phone number type * generated protobuf --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 8495364 commit 1818f02

8 files changed

Lines changed: 269 additions & 132 deletions

File tree

.changeset/legal-paths-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"github.com/livekit/protocol": minor
3+
---
4+
5+
Utilities for phone number functionality

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
github.com/nats-io/nats.go v1.43.0 // indirect
6464
github.com/nats-io/nkeys v0.4.11 // indirect
6565
github.com/nats-io/nuid v1.0.1 // indirect
66+
github.com/nyaruka/phonenumbers v1.6.5 // indirect
6667
github.com/pion/datachannel v1.5.10 // indirect
6768
github.com/pion/dtls/v3 v3.0.6 // indirect
6869
github.com/pion/ice/v4 v4.0.10 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0=
117117
github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE=
118118
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
119119
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
120+
github.com/nyaruka/phonenumbers v1.6.5 h1:aBCaUhfpRA7hU6fsXk+p7KF1aNx4nQlq9hGeo2qdFg8=
121+
github.com/nyaruka/phonenumbers v1.6.5/go.mod h1:7gjs+Lchqm49adhAKB5cdcng5ZXgt6x7Jgvi0ZorUtU=
120122
github.com/onsi/gomega v1.36.1 h1:bJDPBO7ibjxcbHMgSCoo4Yj18UWbKDlLwX1x9sybDcw=
121123
github.com/onsi/gomega v1.36.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
122124
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=

livekit/livekit_phone_number.pb.go

Lines changed: 129 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit/livekit_phone_number.twirp.go

Lines changed: 66 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobufs/livekit_phone_number.proto

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ enum PhoneNumberStatus {
1616
PHONE_NUMBER_STATUS_RELEASED = 3; // Number has been released
1717
}
1818

19+
// Phone number type enumeration
20+
enum PhoneNumberType {
21+
PHONE_NUMBER_TYPE_UNKNOWN = 0; // Default value - unknown or parsing error
22+
PHONE_NUMBER_TYPE_MOBILE = 1; // Mobile phone number
23+
PHONE_NUMBER_TYPE_LOCAL = 2; // Local/fixed line number
24+
PHONE_NUMBER_TYPE_TOLL_FREE = 3; // Toll-free number
25+
}
26+
1927
// Public Phone Number Service - External API for phone number management
2028
service PhoneNumberService {
2129
// Search available phone numbers in inventory
@@ -123,7 +131,7 @@ message PhoneNumber {
123131
string e164_format = 2; // Phone number in E.164 format (e.g., "+14155552671")
124132
string country_code = 3; // Country code (e.g., "US")
125133
string area_code = 4; // Area code (e.g., "415")
126-
string number_type = 5; // Number type (e.g., local, toll-free, national, mobile)
134+
PhoneNumberType number_type = 5; // Number type (mobile, local, toll-free, unknown)
127135
string locality = 6; // City/locality (e.g., "San Francisco")
128136
string region = 7; // State/region (e.g., "CA")
129137
double spam_score = 8; // Spam score for fraud detection

sip/phonenumber.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

utils/guid/id.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const (
6060
CloudAgentSecretPrefix = "CAS_"
6161
CloudAgentWorkerPrefix = "CAW_"
6262
AgentGatewayPrefix = "GW_"
63+
CarrierPrefix = "CR_"
64+
PhoneNumberPrefix = "PN_"
6365
)
6466

6567
var guidGeneratorPool = sync.Pool{

0 commit comments

Comments
 (0)