0

I have very simple code and I just can't figure out what's going wrong.

I have a Twilio trial account and a trial phone number and I am trying to send an SMS using it.

from os import environ
from twilio.rest import Client

client = Client(environ['ACCOUNT_SID'], environ['AUTH_TOKEN'])

message = client.messages.create(body = f'The verification code for Treal is {code}', from_ = "+1**********", to = "+91**********")

The one in the from is my Twilio trial number and the one in the "to" is a number I want to send to. I am getting an error:

twilio.base.exceptions.TwilioRestException:
HTTP Error Your request was:

POST /Accounts/AC*******1830e0****a3403dfd16d****/Messages.json

Twilio returned the following information:

Unable to create record: A 'To' phone number is required.

More information may be available here:

https://www.twilio.com/docs/errors/21604
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Datajack
  • 88
  • 3
  • 16
  • Are you sure the phone number you gave is valid? – tomerpacific Feb 09 '22 at 09:15
  • @tomerpacific Yes. There are no asterisks in the real phone_numbers, I just added that to hide them since it is unsafe to disclose my phone number. – Datajack Feb 09 '22 at 09:17
  • I understand there are no real asterisks in the real phone number, but since I cannot see the full phone number, I suggested that it might not be a valid one. – tomerpacific Feb 09 '22 at 11:59
  • Yeah, I get it. I had upvoted your answer – Datajack Feb 09 '22 at 12:48
  • As far as I can see, the code you shared isn't the full code you are running, as the `code` variable is only mentioned in the formatted string. I appreciate cutting an example down, but I wonder if you cut out where the bug is here. Is that `to` number being fed from somewhere else in your code? Are you able to run [this example code from the Twilio docs](https://www.twilio.com/docs/libraries/python?code-sample=code-python-helper-library-sms-test&code-language=Python&code-sdk-version=default#test-your-installation) successfully? – philnash Feb 10 '22 at 02:24
  • @philnash Yes, it works! Twilio's example code works! But mine does not. Here is all of my code, uploaded to a repl: https://replit.com/@nixdonut/JitteryAdolescentCubase. I used this tutorial by twilio to build it: https://www.twilio.com/blog/verify-phone-numbers-django-twilio-verify – Datajack Feb 10 '22 at 06:34
  • If you hard code a phone number in the `to` argument does it work? – philnash Feb 10 '22 at 06:47
  • Yes, it does. Sorry for the late reply. I updated the code and managed to get it work for hardcoded phone number. It seems that the form field is returning a null value, there is no problem with twilio. – Datajack Feb 10 '22 at 07:56
  • @philnash Twilio is sending weird messages, with strange characters though, which show up as question marks and in strange sizes and fonts – Datajack Feb 10 '22 at 08:47
  • That is probably worth asking a new question about. If you can include the text you’re trying to send and an example of the strange characters, that would be great. Thanks! – philnash Feb 10 '22 at 08:53
  • @philnash Oh, never mind. I figured that out as well. Everything is okay now. I had accidentally mistyped one of the field names. – Datajack Feb 10 '22 at 08:55
  • Oh, good! Glad to hear it’s all working now! It might help others if you were to share why your form was giving you null results for the phone number too. – philnash Feb 10 '22 at 08:56
  • Because I accidentally passed `form.cleaned_data["phone"]` to the `verify.send()` function instead of `form.cleaned_data["phone_number"]`. The field's name is phone_number, not phone. So it returned None. Thank you for responding so quickly. – Datajack Feb 10 '22 at 08:59

2 Answers2

1

With a trial account, in Twilio, you can send a message only to the phone you signed in with or verified so if I provided the phone number "1234" I can only send messages to "1234".

Maybe you didn't provide a phone number when you signed up?

Twiilo docs docs pic

Dharman
  • 30,962
  • 25
  • 85
  • 135
omercotkd
  • 493
  • 1
  • 6
  • 13
  • I did, but I am trying to sms a different phone number. Let me check if it works with my phone number which I registered with. – Datajack Feb 09 '22 at 09:21
  • ok let me know if it works – omercotkd Feb 09 '22 at 09:23
  • nope. still doesn't work. The error message say the to field was absent, so I think that's it, it's not receiving the 'to' field. – Datajack Feb 09 '22 at 09:30
  • I have the same code and it's working for me maybe test again if the number was verified, or test if the phone prefix is right. – omercotkd Feb 09 '22 at 09:41
1

Check for hidden control characters using a site like https://www.soscisurvey.de/tools/view-chars.php, and simply using a regular string, and see if it works or not and then work to failure.

Alan
  • 10,465
  • 2
  • 8
  • 9