twilio / twilio-ruby

A Ruby gem for communicating with the Twilio API and generating TwiML
MIT License
1.35k stars 462 forks source link

Wrong parameter names for postal code and locality in AvailablePhoneNumbers #611

Closed AxelTheGerman closed 2 years ago

AxelTheGerman commented 2 years ago

Issue Summary

I am trying to search available phone numbers by postal code and locality and am not getting any results.

I believe this is due to wrong parameter names here: https://github.com/twilio/twilio-ruby/blob/a2033d3eaa3f9899b1d7886615ed70e5a0c5af5e/lib/twilio-ruby/rest/api/v2010/account/available_phone_number/local.rb#L304

And here https://github.com/twilio/twilio-ruby/blob/a2033d3eaa3f9899b1d7886615ed70e5a0c5af5e/lib/twilio-ruby/rest/api/v2010/account/available_phone_number/local.rb#L308

Thanks for looking into this, some other params such as region and lata seem to work but might be worth double checking all of them.

Steps to Reproduce

Use the API directly (via curl) to verify numbers are available - with correct parameter name PostalCode

$ curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/'$TWILIO_ACCOUNT_SID'/AvailablePhoneNumbers/CA/Local.json?PostalCode=l5b3z3&PageSize=1' -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Returned numbers:

{"available_phone_numbers": [{"friendly_name": "(343) 338-4777", "phone_number": "+13433384777", "lata": "888", "rate_center": "MERRICKVL", "latitude": "44.916700", "longitude": "-75.833300", "locality": "Merrickville", "region": "ON", "postal_code": "K0G 1N0", "iso_country": "CA", "address_requirements": "none", "beta": false, "capabilities": {"voice": true, "SMS": true, "MMS": true}}], "uri": "/2010-04-01/Accounts/AC71633d4a75accf70400952b29edb000b/AvailablePhoneNumbers/CA/Local.json?PageSize=1&PostalCode=l5b3z3"}

Use the API with the parameter name used in the gem - InPostalCode

$ curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/'$TWILIO_ACCOUNT_SID'/AvailablePhoneNumbers/CA/Local.json?InPostalCode=l5b3z3&PageSize=1' -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

No numbers are returned

{"available_phone_numbers": [], "uri": "/2010-04-01/Accounts/AC71633d4a75accf70400952b29edb000b/AvailablePhoneNumbers/CA/Local.json?InPostalCode=l5b3z3&PageSize=20"}

I'm having the same issue for InLocality vs Locality:

$ curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/'$TWILIO_ACCOUNT_SID'/AvailablePhoneNumbers/CA/Local.json?InLocality=Vancouver&PageSize=1' -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

{"available_phone_numbers": [], "uri": "/2010-04-01/Accounts/AC71633d4a75accf70400952b29edb000b/AvailablePhoneNumbers/CA/Local.json?PageSize=1&InLocality=Vancouver"}

$ curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/'$TWILIO_ACCOUNT_SID'/AvailablePhoneNumbers/CA/Local.json?Locality=Vancouver&PageSize=1' -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

{"available_phone_numbers": [{"friendly_name": "(819) 272-9733", "phone_number": "+18192729733", "lata": "888", "rate_center": "KUUJJUAQ", "latitude": "58.100000", "longitude": "-68.400000", "locality": "Kuujjuaq", "region": "QC", "postal_code": null, "iso_country": "CA", "address_requirements": "none", "beta": false, "capabilities": {"voice": true, "SMS": true, "MMS": true}}], "uri": "/2010-04-01/Accounts/AC71633d4a75accf70400952b29edb000b/AvailablePhoneNumbers/CA/Local.json?PageSize=1&Locality=Vancouver"}

Code Snippet

> client.available_phone_numbers('CA').local.list(in_postal_code: 'l5b3z3')
 => []

> client.available_phone_numbers('CA').local.list(in_locality: 'Vancouver')
 => [] 

Using postal_code or locality results in ArgumentError (unknown keyword: :postal_code)

Exception/Log

n/a

Technical details:

AxelTheGerman commented 2 years ago

Somehow I seem to be getting weird results with either PostalCode or InPostalCode 😭

claudiachua commented 2 years ago

Hi @AxelTheGerman , thanks for opening a github issues. While trying to recreate your issue, we believe that this is the library expected behavior. The library only returns exact matches, as there are no available numbers under postal code l5b3z3 nor locality Vancouver, hence it's returning an empty array.

Looking at the results from the CURL request, the api returns the number from the same country but not necessarily the exact postal code/locality.

AxelTheGerman commented 2 years ago

Yes, thank you for looking into this @claudiachua !

I think I need to bring this back to support as it's not the fault of the Ruby SDK but in general the API behaves differently from the Twilio Console "Buy a number"

Just to document this here in case others have similar issues:

  1. Searching for Vancouver numbers from Ruby:
client.available_phone_numbers('CA').local.list(in_locality: 'Vanco
uver')
 => [] 
  1. Searching for Vancouver numbers from CURL:
$ curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/'$TWILIO_ACCOUNT_SID'/AvailablePhoneNumbers/CA/Local.json?InLocality=Vancouver&PageSize=20' -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
{"available_phone_numbers": [], "uri": "/2010-04-01/Accounts/AC891d08c136b401ce1a77b90924a2da4e/AvailablePhoneNumbers/CA/Local.json?PageSize=20&InLocality=Vancouver"}

Note: changing the InLocality argument to Locality does - as you mentioned - simply return random Canadian numbers as it's an invalid argument and therefore doesn't filter the numbers at all.

  1. Searching for Vancouver in Twilio Console:
Screen Shot 2022-07-12 at 4 05 39 PM

☝️ this does give me a whole bunch of numbers in the Vancouver area that I'm not getting via the API

  1. Searching for a specific number that I know is available:
2.7.5 :006 > numbers = client.available_phone_numbers('CA').local.list(contains:
 '2367085606')
 => [<Twilio.Api.V2010.LocalInstance>] 
2.7.5 :007 > numbers.first.phone_number
 => "+12367085606" 
2.7.5 :008 > numbers.first.locality
 => "West Vancouver" 
  1. Searching with the specific "locality" of that number:
> client.available_phone_numbers('CA').local.list(in_locality: 'West 
Vancouver')
 => [<Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>, <Twilio.Api.V2010.LocalInstance>] 

So TLDR when I search on twilio.com/console for "Vancouver" I'm getting numbers (expected) when I search via the API I don't - this is not expected but will take it up with support as this is the same for Ruby SDK and CURL