-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
58 lines (52 loc) · 2 KB
/
number.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package twilio
import (
"encoding/json"
"github.com/grokify/gophonenumbers"
"github.com/grokify/mogo/net/http/httputilmore"
)
type NumberInfo struct {
CallerName map[string]string `json:"caller_name,omitempty"`
CountryCode string `json:"country_code,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
NationalFormat string `json:"national_format,omitempty"`
URL string `json:"url,omitempty"`
Carrier Carrier `json:"carrier,omitempty"`
APIResponseInfo httputilmore.ResponseInfo `json:"api_response_info,omitempty"`
}
func (num *NumberInfo) Canonical() (gophonenumbers.Number, error) {
canNum := gophonenumbers.NewNumber()
canNum.E164Number = num.PhoneNumber
canNum.CountryCode = num.CountryCode
canNum.CarrierNumberInfo = num.Carrier.Canonical()
canNum.CarrierNumberInfo.E164Number = canNum.E164Number
lookup := gophonenumbers.Lookup{
CarrierNumberInfo: canNum.CarrierNumberInfo,
LookupSource: gophonenumbers.SourceTwilio,
LookupTime: num.APIResponseInfo.Time.UTC()}
if len(num.APIResponseInfo.Body) > 0 {
msi := map[string]interface{}{}
err := json.Unmarshal([]byte(num.APIResponseInfo.Body), &msi)
if err != nil {
return canNum, err
}
lookup.LookupResponse = msi
}
canNum.Lookups.Add(lookup)
return canNum, nil
}
type Carrier struct {
MobileCountryCode string `json:"mobile_country_code,omitempty"`
MobileNetworkCode string `json:"mobile_network_code,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
}
func (car *Carrier) Canonical() gophonenumbers.CarrierNumberInfo {
return gophonenumbers.CarrierNumberInfo{
E164Number: "",
MobileCountryCode: car.MobileCountryCode,
MobileNetworkCode: car.MobileNetworkCode,
Name: car.Name,
LineType: car.Type,
ErrorCode: car.ErrorCode}
}