[PYTHON] I tried sending an SMS with Twilio

Prerequisites / Limits

Get a phone number

From the Phone Numbers page, click ** Buy Phone Numbers ** to go to the purchase page.

Twilio_User_-_Account_Phone_Numbers_Incoming.png

You need a ** US phone number ** to send an SMS.

Twilio_User_-_Account_Phone_Numbers_Search.png

Select a purpose and search for a phone number.

Twilio_User_-_Account_Phone_Numbers_Search.png

A list of phone numbers and capabilities will be displayed, so purchase an appropriate one. The amount is about $ 1 to $ 2. For some reason, you can purchase with a trial account without registering a card. Strange.

Twilio_User_-_Account_Phone_Numbers_Search_Buy_Results.png

Send SMS

Once you have the phone number, send an SMS. Fill in the form with API Explorer and click ** Issue Request ** at the bottom of the screen. Click to send.

Twilio_User_-_Account_Developer_Tools_API_Explorer_Message_Create.png

To send programmatically, get the SID and AuthToken from the account settings page and send them using Support Library on github. The credentials are like this.

Twilio_User_-_Account_Settings.png

Copy and use the sample code presented in API Explorer.

from twilio.rest import TwilioRestClient 
 
# put your own credentials here 
ACCOUNT_SID = "[SID]" 
AUTH_TOKEN = "[AuthToken]" 
 
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) 
 
client.messages.create(
	to="12345678", 
	from_="87654321", 
	body="hello! it's a twilio!",  
)

At the time of success

If the request is successful, twilio.rest.resources.messages.Message will be returned. result.status seems to be queue.

At the time of failure

If the request fails, TwilioRestException is thrown. Useful because the error message has a detailed reason.

TwilioRestException:
HTTP Error Your request was:

POST https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Messages.json

Twilio returned the following information:

Message body is required.

More information may be available here:

https://www.twilio.com/docs/errors/21602

Recommended Posts

I tried sending an SMS with Twilio
I tried sending an email with python.
I tried sending an email with SendGrid + Python
I sent an SMS with Python
I tried sending an email from Amazon SES with Python
I tried sending an email from the Sakura server with flask-mail
I tried to detect an object with M2Det!
I tried fp-growth with python
I tried scraping with Python
I tried Learning-to-Rank with Elasticsearch!
I tried clustering with PyCaret
I tried Smith standardizing an integer matrix with Numpy
I tried to implement an artificial perceptron with python
I tried gRPC with Python
I tried scraping with python
I tried to make an OCR application with PySimpleGUI
I tried to find an alternating series with tensorflow
I tried to create an article in Wiki.js with SQLAlchemy
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
I tried machine learning with liblinear
I tried web scraping with python.
I tried moving food with SinGAN
I tried implementing DeepPose with PyTorch
I tried face detection with MTCNN
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
I tried face recognition with OpenCV
I tried to make an image similarity function with Python + OpenCV
I get an error with import pandas.
I tried multiple regression analysis with polynomial regression
I tried using Amazon SQS with django-celery
I tried to implement Autoencoder with TensorFlow
I tried linebot with flask (anaconda) + heroku
I tried to visualize AutoEncoder with TensorFlow
I tried to get started with Hy
I tried scraping Yahoo News with Python
I tried using Selenium with Headless chrome
I tried factor analysis with Titanic data!
I tried learning with Kaggle's Titanic (kaggle②)
When I get an error with PyInstaller
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried batch normalization with PyTorch (+ note)
I tried recursion with Python ② (Fibonacci sequence)
I tried implementing DeepPose with PyTorch PartⅡ
I tried to implement CVAE with PyTorch
I tried playing with the image with Pillow
I tried to solve TSP with QAOA
I tried simple image recognition with Jupyter
I tried CNN fine tuning with Resnet
I tried natural language processing with transformers.
#I tried something like Vlookup with Python # 2
I tried to make an open / close sensor (Twitter cooperation) with TWE-Lite-2525A
I tried to predict next year with AI
I tried "smoothing" the image with Python + OpenCV
I got an error when saving with OpenCV
I tried hundreds of millions of SQLite with python
I tried to detect Mario with pytorch + yolov3