From 9612e664c3950b1dfac1e97759c780e0a0e6c54a Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Wed, 8 May 2024 19:14:47 +0000 Subject: [PATCH] Update to latest gocommon --- assets/static/channel.go | 2 +- cmd/docgen/docs/editor_support.go | 9 ++-- cmd/transferairtime/main.go | 2 +- contactql/parser.go | 2 +- contactql/visitor.go | 2 +- flows/actions/base.go | 7 ++- flows/actions/transfer_airtime.go | 6 +-- flows/channel.go | 4 +- flows/contact.go | 6 +-- flows/contact_test.go | 1 - .../definition/legacy/expressions/context.go | 11 ++--- flows/engine/testdata/templates.json | 4 +- flows/modifiers/urns.go | 3 +- flows/resumes/testdata/_assets.json | 2 +- flows/resumes/testdata/dial.json | 2 +- flows/routers/cases/tests.go | 3 +- flows/routers/cases/tests_test.go | 2 +- flows/routers/waits/dial.go | 3 +- flows/runs/run_test.go | 4 +- flows/urn.go | 10 ++--- go.mod | 18 ++++---- go.sum | 44 +++++++++---------- .../runner/ivr_dial.invalid_phone.json | 4 +- utils/phone.go | 7 +-- 24 files changed, 78 insertions(+), 80 deletions(-) diff --git a/assets/static/channel.go b/assets/static/channel.go index ec8af243f..fc860375f 100644 --- a/assets/static/channel.go +++ b/assets/static/channel.go @@ -38,7 +38,7 @@ func NewTelChannel(uuid assets.ChannelUUID, name string, address string, roles [ UUID_: uuid, Name_: name, Address_: address, - Schemes_: []string{urns.TelScheme}, + Schemes_: []string{urns.Phone.Prefix}, Roles_: roles, Features_: []assets.ChannelFeature{}, Country_: country, diff --git a/cmd/docgen/docs/editor_support.go b/cmd/docgen/docs/editor_support.go index b4ed9421d..a95154ec9 100644 --- a/cmd/docgen/docs/editor_support.go +++ b/cmd/docgen/docs/editor_support.go @@ -156,11 +156,10 @@ func createContextPathListFile(outputDir string, c *completion.Completion) error } func createURNsType(gettext func(string) string) completion.Type { - properties := make([]*completion.Property, 0, len(urns.ValidSchemes)) - for k := range urns.ValidSchemes { - name := strings.Title(k) - help := strings.ReplaceAll(gettext("{type} URN for the contact"), "{type}", name) - properties = append(properties, completion.NewProperty(k, help, "text")) + properties := make([]*completion.Property, 0, len(urns.Schemes)) + for _, s := range urns.Schemes { + help := strings.ReplaceAll(gettext("{type} URN for the contact"), "{type}", s.Name) + properties = append(properties, completion.NewProperty(s.Prefix, help, "text")) } sort.SliceStable(properties, func(i, j int) bool { return properties[i].Key < properties[j].Key }) diff --git a/cmd/transferairtime/main.go b/cmd/transferairtime/main.go index 4bfd81e9d..20814c897 100644 --- a/cmd/transferairtime/main.go +++ b/cmd/transferairtime/main.go @@ -35,7 +35,7 @@ func main() { os.Exit(1) } - destination, err := urns.NewTelURNForCountry(args[0], "") + destination, err := urns.ParsePhone(args[0], "") if err != nil { fmt.Printf("%s isn't a valid phone number\n", args[0]) os.Exit(1) diff --git a/contactql/parser.go b/contactql/parser.go index 52d271dfb..ea2e6dab2 100644 --- a/contactql/parser.go +++ b/contactql/parser.go @@ -343,7 +343,7 @@ func ParseQuery(env envs.Environment, text string, resolver Resolver) (*ContactQ // if query is a valid number, rewrite as a tel = query if env.RedactionPolicy() != envs.RedactionPolicyURNs { - if number := utils.ParsePhoneNumber(text, string(env.DefaultCountry())); number != "" { + if number := utils.ParsePhoneNumber(text, env.DefaultCountry()); number != "" { text = fmt.Sprintf(`tel = %s`, number) } } diff --git a/contactql/visitor.go b/contactql/visitor.go index 66ff5611b..8122ef466 100644 --- a/contactql/visitor.go +++ b/contactql/visitor.go @@ -102,7 +102,7 @@ func (v *visitor) VisitImplicitCondition(ctx *gen.ImplicitConditionContext) any } else if implicitIsPhoneNumberRegex.MatchString(value) { value = cleanPhoneNumberRegex.ReplaceAllLiteralString(value, "") - return NewCondition(urns.TelScheme, PropertyTypeScheme, OpContains, value) + return NewCondition(urns.Phone.Prefix, PropertyTypeScheme, OpContains, value) } // convert to contains condition only if we have the right tokens, otherwise make equals check diff --git a/flows/actions/base.go b/flows/actions/base.go index 48c98c4f7..6b807b152 100644 --- a/flows/actions/base.go +++ b/flows/actions/base.go @@ -245,13 +245,12 @@ func (a *otherContactsAction) resolveRecipients(run flows.Run, logEvent flows.Ev // next up try it as a URN urn := urns.URN(evaluatedLegacyVar) if urn.Validate() == nil { - urn = urn.Normalize(string(run.Session().MergedEnvironment().DefaultCountry())) - urnList = append(urnList, urn) + urnList = append(urnList, urn.Normalize()) } else { // if that fails, try to parse as phone number - parsedTel := utils.ParsePhoneNumber(evaluatedLegacyVar, string(run.Session().MergedEnvironment().DefaultCountry())) + parsedTel := utils.ParsePhoneNumber(evaluatedLegacyVar, run.Session().MergedEnvironment().DefaultCountry()) if parsedTel != "" { - urn, _ := urns.NewURNFromParts(urns.TelScheme, parsedTel, "", "") + urn, _ := urns.New(urns.Phone, parsedTel) urnList = append(urnList, urn) } else { logEvent(events.NewErrorf("'%s' couldn't be resolved to a contact, group or URN", evaluatedLegacyVar)) diff --git a/flows/actions/transfer_airtime.go b/flows/actions/transfer_airtime.go index 319116b38..be3a41c82 100644 --- a/flows/actions/transfer_airtime.go +++ b/flows/actions/transfer_airtime.go @@ -66,15 +66,15 @@ func (a *TransferAirtimeAction) transfer(run flows.Run, logEvent flows.EventCall contact := run.Contact() // fail if the contact doesn't have a tel URN - telURNs := contact.URNs().WithScheme(urns.TelScheme) + telURNs := contact.URNs().WithScheme(urns.Phone.Prefix) if len(telURNs) == 0 { return nil, errors.New("can't transfer airtime to contact without a tel URN") } - // if contact's preferred channel is tel, use that as the sender + // if contact's preferred channel is a phone number, use that as the sender var sender urns.URN channel := contact.PreferredChannel() - if channel != nil && channel.SupportsScheme(urns.TelScheme) { + if channel != nil && channel.SupportsScheme(urns.Phone.Prefix) { sender, _ = urns.Parse("tel:" + channel.Address()) } diff --git a/flows/channel.go b/flows/channel.go index 1d78fed72..f34240f38 100644 --- a/flows/channel.go +++ b/flows/channel.go @@ -103,13 +103,13 @@ func (s *ChannelAssets) GetForURN(urn *ContactURN, role assets.ChannelRole) *Cha } // tel is a special case because we do number based matching - if urn.URN().Scheme() == urns.TelScheme { + if urn.URN().Scheme() == urns.Phone.Prefix { countryCode := i18n.DeriveCountryFromTel(urn.URN().Path()) candidates := make([]*Channel, 0) for _, ch := range s.all { // skip if not tel and not sendable - if !ch.SupportsScheme(urns.TelScheme) || !ch.HasRole(role) { + if !ch.SupportsScheme(urns.Phone.Prefix) || !ch.HasRole(role) { continue } // skip if international and channel doesn't allow that diff --git a/flows/contact.go b/flows/contact.go index 8cdbbecf7..48885f951 100644 --- a/flows/contact.go +++ b/flows/contact.go @@ -176,7 +176,7 @@ func (c *Contact) Country() i18n.Country { } for _, u := range c.urns { - if u.urn.Scheme() == urns.TelScheme { + if u.urn.Scheme() == urns.Phone.Prefix { c := i18n.DeriveCountryFromTel(u.urn.Path()) if c != i18n.NilCountry { return c @@ -262,7 +262,7 @@ func (c *Contact) RemoveURN(urn urns.URN) bool { // HasURN checks whether the contact has the given URN func (c *Contact) HasURN(urn urns.URN) bool { - urn = urn.Normalize("") + urn = urn.Normalize() for _, u := range c.urns { if u.URN().Identity() == urn.Identity() { @@ -433,7 +433,7 @@ func (c *Contact) UpdatePreferredChannel(channel *Channel) bool { for _, urn := range c.urns { // tel URNs can be re-assigned, other URN schemes are considered channel specific - if urn.URN().Scheme() == urns.TelScheme && channel.SupportsScheme(urns.TelScheme) { + if urn.URN().Scheme() == urns.Phone.Prefix && channel.SupportsScheme(urns.Phone.Prefix) { urn.SetChannel(channel) } diff --git a/flows/contact_test.go b/flows/contact_test.go index 762728ef5..6f230ec06 100644 --- a/flows/contact_test.go +++ b/flows/contact_test.go @@ -125,7 +125,6 @@ func TestContact(t *testing.T) { "mailto": nil, "rocketchat": nil, "slack": nil, - "teams": nil, "tel": flows.NewContactURN(urns.URN("tel:+12024561111?channel=294a14d4-c998-41e5-a314-5941b97b89d7"), nil).ToXValue(env), "telegram": nil, "twitter": flows.NewContactURN(urns.URN("twitter:joey"), nil).ToXValue(env), diff --git a/flows/definition/legacy/expressions/context.go b/flows/definition/legacy/expressions/context.go index 8eaa24b7e..92a9d9501 100644 --- a/flows/definition/legacy/expressions/context.go +++ b/flows/definition/legacy/expressions/context.go @@ -16,9 +16,9 @@ type mapping struct { var mappings []mapping func init() { - schemes := make([]string, 0, len(urns.ValidSchemes)) - for s := range urns.ValidSchemes { - schemes = append(schemes, s) + schemes := make([]string, len(urns.Schemes)) + for i, s := range urns.Schemes { + schemes[i] = s.Prefix } schemesRe := strings.Join(schemes, `|`) @@ -103,8 +103,9 @@ func MigrateContextReference(path string, rawDates bool) string { var numericLookupRegex = regexp.MustCompile(`\.\d+\w*`) // fixes property lookups -// .1 => ["1"] -// .1foo => ["1foo"] +// +// .1 => ["1"] +// .1foo => ["1foo"] func fixLookups(path string) string { return numericLookupRegex.ReplaceAllStringFunc(path, func(lookup string) string { return `["` + lookup[1:] + `"]` diff --git a/flows/engine/testdata/templates.json b/flows/engine/testdata/templates.json index 5267cb885..6b5c20e7e 100644 --- a/flows/engine/testdata/templates.json +++ b/flows/engine/testdata/templates.json @@ -102,7 +102,7 @@ }, { "template": "@urns", - "output": "{discord: , ext: , facebook: , fcm: , freshchat: , instagram: , jiochat: , line: , mailto: mailto:foo@bar.com, rocketchat: , slack: , teams: , tel: tel:+12024561111, telegram: , twitter: , twitterid: twitterid:54784326227#nyaruka, viber: , vk: , webchat: , wechat: , whatsapp: }" + "output": "{discord: , ext: , facebook: , fcm: , freshchat: , instagram: , jiochat: , line: , mailto: mailto:foo@bar.com, rocketchat: , slack: , tel: tel:+12024561111, telegram: , twitter: , twitterid: twitterid:54784326227#nyaruka, viber: , vk: , webchat: , wechat: , whatsapp: }" }, { "template": "@urns.tel", @@ -813,7 +813,6 @@ "mailto": "mailto:foo@bar.com", "rocketchat": null, "slack": null, - "teams": null, "tel": "tel:+12024561111", "telegram": null, "twitter": null, @@ -910,7 +909,6 @@ "mailto": null, "rocketchat": null, "slack": null, - "teams": null, "tel": "tel:+12024562222", "telegram": null, "twitter": null, diff --git a/flows/modifiers/urns.go b/flows/modifiers/urns.go index cf61f2bcb..d1331ab1b 100644 --- a/flows/modifiers/urns.go +++ b/flows/modifiers/urns.go @@ -54,8 +54,7 @@ func (m *URNsModifier) Apply(eng flows.Engine, env envs.Environment, sa flows.Se } for _, urn := range m.URNs { - // normalize the URN - urn := urn.Normalize(string(env.DefaultCountry())) + urn := urn.Normalize() if err := urn.Validate(); err != nil { log(events.NewErrorf("'%s' is not valid URN", urn)) diff --git a/flows/resumes/testdata/_assets.json b/flows/resumes/testdata/_assets.json index a495ea113..44f19e82c 100644 --- a/flows/resumes/testdata/_assets.json +++ b/flows/resumes/testdata/_assets.json @@ -110,7 +110,7 @@ "type": "switch", "wait": { "type": "dial", - "phone": "(206)5551212" + "phone": "1(206)5551212" }, "result_name": "Redirect", "categories": [ diff --git a/flows/resumes/testdata/dial.json b/flows/resumes/testdata/dial.json index f9d316454..4ea928d6e 100644 --- a/flows/resumes/testdata/dial.json +++ b/flows/resumes/testdata/dial.json @@ -13,7 +13,7 @@ "flow_uuid": "0af51032-1caa-40fe-9907-ab7c6af2bf01", "wait": { "type": "dial", - "phone": "(206)5551212" + "phone": "1(206)5551212" }, "resume": { "type": "dial", diff --git a/flows/routers/cases/tests.go b/flows/routers/cases/tests.go index aa4ebb556..e517ce33a 100644 --- a/flows/routers/cases/tests.go +++ b/flows/routers/cases/tests.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/nyaruka/gocommon/dates" + "github.com/nyaruka/gocommon/i18n" "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/goflow/envs" "github.com/nyaruka/goflow/excellent/functions" @@ -542,7 +543,7 @@ func HasPhone(env envs.Environment, text *types.XText, args ...types.XValue) typ } // try to find a phone number - numbers := utils.FindPhoneNumbers(text.Native(), country.Native()) + numbers := utils.FindPhoneNumbers(text.Native(), i18n.Country(country.Native())) if len(numbers) == 0 { return FalseResult } diff --git a/flows/routers/cases/tests_test.go b/flows/routers/cases/tests_test.go index bde22fbf9..31d20b114 100644 --- a/flows/routers/cases/tests_test.go +++ b/flows/routers/cases/tests_test.go @@ -622,7 +622,7 @@ func TestHasPhone(t *testing.T) { {"0954 1053 7962", "ID", "+6295410537962"}, // Invalid but possible Indonesian number {"0811-1005-611", "ID", "+628111005611"}, // Valid with 11 digits {"10000", "US", ""}, - {"12067799294", "BW", ""}, + {"12067799294", "BW", "+12067799294"}, {"oui", "CD", ""}, } diff --git a/flows/routers/waits/dial.go b/flows/routers/waits/dial.go index bbd7e0546..f5b76d435 100644 --- a/flows/routers/waits/dial.go +++ b/flows/routers/waits/dial.go @@ -62,8 +62,9 @@ func (w *DialWait) AllowedFlowTypes() []flows.FlowType { // Begin beings waiting at this wait func (w *DialWait) Begin(run flows.Run, log flows.EventCallback) bool { phone, _ := run.EvaluateTemplate(w.phone, log) + country := run.Session().MergedEnvironment().DefaultCountry() - urn, err := urns.NewTelURNForCountry(phone, string(run.Session().MergedEnvironment().DefaultCountry())) + urn, err := urns.ParsePhone(phone, country) if err != nil { log(events.NewError(err)) return false diff --git a/flows/runs/run_test.go b/flows/runs/run_test.go index daa602f1d..e9acfe314 100644 --- a/flows/runs/run_test.go +++ b/flows/runs/run_test.go @@ -199,7 +199,7 @@ func TestRunContext(t *testing.T) { }, { `@(json(urns))`, - `{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":"mailto:foo@bar.com","rocketchat":null,"slack":null,"teams":null,"tel":"tel:+12024561111","telegram":null,"twitter":null,"twitterid":"twitterid:54784326227#nyaruka","viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`, + `{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":"mailto:foo@bar.com","rocketchat":null,"slack":null,"tel":"tel:+12024561111","telegram":null,"twitter":null,"twitterid":"twitterid:54784326227#nyaruka","viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`, }, { `@(json(results.favorite_color))`, @@ -215,7 +215,7 @@ func TestRunContext(t *testing.T) { }, { `@(json(parent.urns))`, - `{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":null,"rocketchat":null,"slack":null,"teams":null,"tel":"tel:+12024562222","telegram":null,"twitter":null,"twitterid":null,"viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`, + `{"discord":null,"ext":null,"facebook":null,"fcm":null,"freshchat":null,"instagram":null,"jiochat":null,"line":null,"mailto":null,"rocketchat":null,"slack":null,"tel":"tel:+12024562222","telegram":null,"twitter":null,"twitterid":null,"viber":null,"vk":null,"webchat":null,"wechat":null,"whatsapp":null}`, }, { `@(json(parent.fields))`, diff --git a/flows/urn.go b/flows/urn.go index 37eca7451..6d82cca3c 100644 --- a/flows/urn.go +++ b/flows/urn.go @@ -90,7 +90,7 @@ func (u *ContactURN) SetChannel(channel *Channel) { parsedQuery.Del("channel") } - urn, _ := urns.NewURNFromParts(scheme, path, parsedQuery.Encode(), display) + urn, _ := urns.NewFromParts(scheme, path, parsedQuery.Encode(), display) u.urn = urn } @@ -111,7 +111,7 @@ func (u *ContactURN) withoutQuery(redact bool) urns.URN { return urns.URN(fmt.Sprintf("%s:%s", scheme, redacted)) } - urn, _ := urns.NewURNFromParts(scheme, path, "", display) + urn, _ := urns.NewFromParts(scheme, path, "", display) return urn } @@ -207,9 +207,9 @@ func (l URNList) MapContext(env envs.Environment) map[string]types.XValue { } // and add nils for all other schemes - for scheme := range urns.ValidSchemes { - if _, seen := byScheme[scheme]; !seen { - byScheme[scheme] = nil + for _, scheme := range urns.Schemes { + if _, seen := byScheme[scheme.Prefix]; !seen { + byScheme[scheme.Prefix] = nil } } diff --git a/go.mod b/go.mod index c366ab5de..638214d7a 100644 --- a/go.mod +++ b/go.mod @@ -8,16 +8,16 @@ require ( github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20221202181307-76fa05c21b12 github.com/blevesearch/segment v0.9.1 github.com/buger/jsonparser v1.1.1 - github.com/go-playground/validator/v10 v10.19.0 - github.com/nyaruka/gocommon v1.53.2 + github.com/go-playground/validator/v10 v10.20.0 + github.com/nyaruka/gocommon v1.54.6 github.com/olivere/elastic/v7 v7.0.32 github.com/pkg/errors v0.9.1 github.com/sergi/go-diff v1.3.1 github.com/shopspring/decimal v1.4.0 github.com/stretchr/testify v1.9.0 - golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 - golang.org/x/net v0.24.0 - golang.org/x/text v0.14.0 + golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 + golang.org/x/net v0.25.0 + golang.org/x/text v0.15.0 ) require ( @@ -32,11 +32,11 @@ require ( github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/nyaruka/null/v2 v2.0.3 // indirect - github.com/nyaruka/phonenumbers v1.3.4 // indirect + github.com/nyaruka/phonenumbers v1.3.5 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/crypto v0.22.0 // indirect - golang.org/x/sys v0.19.0 // indirect - google.golang.org/protobuf v1.33.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/sys v0.20.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 653702577..ce8230bf3 100644 --- a/go.sum +++ b/go.sum @@ -23,16 +23,16 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4= -github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= -github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= +github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= +github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -46,12 +46,12 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/nyaruka/gocommon v1.53.2 h1:DKXlvNUcim/+4X0Dz8BjdDHmQBNVU60giEOVu2faRCE= -github.com/nyaruka/gocommon v1.53.2/go.mod h1:1E8KKcJ4r+FPWQm5ImGjiH6K96dKInjLM9BkcD4zOUQ= +github.com/nyaruka/gocommon v1.54.6 h1:cVe+d1upTLdNl/jIvpJhjcxrwsTYU7qGvhPckJsK1ig= +github.com/nyaruka/gocommon v1.54.6/go.mod h1:rWkEIpYIK98zL9Qm6PeMXJ+84WcWlArf01RfuWWCYvQ= github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw= github.com/nyaruka/null/v2 v2.0.3/go.mod h1:OCVeCkCXwrg5/qE6RU0c1oUVZBy+ZDrT+xYg1XSaIWA= -github.com/nyaruka/phonenumbers v1.3.4 h1:bF1Wdh++fxw09s3surhVeBhXEcUKG07pHeP8HQXqjn8= -github.com/nyaruka/phonenumbers v1.3.4/go.mod h1:Ut+eFwikULbmCenH6InMKL9csUNLyxHuBLyfkpum11s= +github.com/nyaruka/phonenumbers v1.3.5 h1:WZLbQn61j2E1OFnvpUTYbK/6hViUgl6tppJ55/E2iQM= +github.com/nyaruka/phonenumbers v1.3.5/go.mod h1:Ut+eFwikULbmCenH6InMKL9csUNLyxHuBLyfkpum11s= github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E= github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -66,18 +66,18 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= -golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc= -golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= -google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/test/testdata/runner/ivr_dial.invalid_phone.json b/test/testdata/runner/ivr_dial.invalid_phone.json index da35f3795..493df175c 100644 --- a/test/testdata/runner/ivr_dial.invalid_phone.json +++ b/test/testdata/runner/ivr_dial.invalid_phone.json @@ -5,7 +5,7 @@ { "created_on": "2018-07-06T12:30:02.123456789Z", "step_uuid": "8720f157-ca1c-432f-9c0b-2014ddc77094", - "text": "scheme or path cannot be empty", + "text": "the phone number supplied is not a number", "type": "error" }, { @@ -60,7 +60,7 @@ { "created_on": "2018-07-06T12:30:02.123456789Z", "step_uuid": "8720f157-ca1c-432f-9c0b-2014ddc77094", - "text": "scheme or path cannot be empty", + "text": "the phone number supplied is not a number", "type": "error" }, { diff --git a/utils/phone.go b/utils/phone.go index f2922c3e0..7b05691ae 100644 --- a/utils/phone.go +++ b/utils/phone.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" + "github.com/nyaruka/gocommon/i18n" "github.com/nyaruka/gocommon/urns" ) @@ -11,14 +12,14 @@ var possiblePhone = regexp.MustCompile(`\+?[\d \.\-\(\)]{5,}`) var onlyPhone = regexp.MustCompile(`^` + possiblePhone.String() + `$`) // ParsePhoneNumber tries to parse the given string as a phone number. If successful, it returns it formatted as E164. -func ParsePhoneNumber(s, country string) string { +func ParsePhoneNumber(s string, country i18n.Country) string { s = strings.TrimSpace(s) if !onlyPhone.MatchString(s) { return "" } - formatted, err := urns.ParseNumber(s, country) + formatted, err := urns.ParseNumber(s, country, false) if err != nil { return "" } @@ -27,7 +28,7 @@ func ParsePhoneNumber(s, country string) string { } // FindPhoneNumbers finds phone numbers anywhere in the given string -func FindPhoneNumbers(s, country string) []string { +func FindPhoneNumbers(s string, country i18n.Country) []string { nums := make([]string, 0) for _, candidate := range possiblePhone.FindAllString(s, -1) { if num := ParsePhoneNumber(candidate, country); num != "" {