[PYTHON] Try sending a message with Twilio's SMS service

Purpose

After investigating whether GCP's service could replace sending messages with Amazon SMS, Google recommended a third-party service called Twilio, so I tried it.

· Twilio's use of SMS and voice services (https://cloud.google.com/appengine/docs/flexible/nodejs/using-sms-and-voice-services-via-twilio?hl=ja)

About Twilio

Many people don't know what Twilio looks like. Simply put, it's a cloud API service with features such as voice calls, SMS, chat, and video. As a feature **, you can control phone calls, SMS, faxes, etc. with a program **, so I think you should consider it when you want to create a service related to an application or system.

If you're curious, Twilio's partner KDDI has posted more information on the web. https://twilio.kddi-web.com/

Create a Twilio account

To use Twilio, you first need to create an account.

https://twilio.kddi-web.com/

Register for an account by signing up for free at the link above. Follow the instructions on the site to complete the account creation. It's a general procedure, so I don't think it's particularly difficult.

Get an American phone number

Get the phone number you want to use with your Twilio account. Please note that if you want to use SMS, you need to get ** American phone number ** starting with country code 1 (because Japanese phone number does not support SMS).

Below is the procedure for getting a phone number

1. From the home screen after login, select the #tab on the left side of the screen and click Get Started.

スクリーンショット 2019-11-27 10.10.31.jpg

2. Click Get First Twilio Phone Number

スクリーンショット 2019-11-27 10.23.39.png

3. Since the US phone number is assigned by default, just click "Select this number"

You have now obtained your phone number and are ready to use SMS.

Let's send a message by SMS! !!

Now let's actually send a message using Twilio's Programmable SMS service. The following site (English) was helpful. https://www.twilio.com/docs/sms/quickstart

スクリーンショット 2019-11-27 14.41.42.png

As shown in the image, the contents are organized by programming language, so I think you should refer to the language that suits your taste. This time I will try with ** Python **.

1. Preparing the Python environment

If you have not installed python in your local environment, you can install it with the following command.

python


pip install python;

2. Install the Twilio library

Install the library with the following command.

python


pip install twilio;

If pip: command not found </ font> is displayed, execute the following command.

python


easy_install twilio

Reference URL: https://jp.twilio.com/docs/libraries/python

3. Create a Python file to request SMS for your Twilio account

Check the ACCOUNT SID and AUTH TOKEN in the links below. https://jp.twilio.com/console/project/settings

Copy the code below into an editor or notepad.

sms_message.py



# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client


# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages \
                .create(
                     body="Hello from Python!",
                     from_='+Caller's phone number (including country code)',
                     to='+Message destination phone number (including country code)'
                 )

print(message.sid)
  1. Enter the confirmed information in the account_sid and auth_token parts
  2. Enter the message you want to send to the body, Twilio's registered phone number in from, and the phone number of the device you want to send the message to to.
  3. Save as send_sms.py

4. Hit the file with the command and send the message

When you hit the Python file you just created with the command, a message will be sent.

python send_sms.py

5. SMS received by the terminal

IMG_0365.jpeg

I was able to confirm the message safely.

At the end

The above is the flow of the list until sending SMS. I'll summarize the pricing system and Twilio services other than SMS if there is a next time or later.

Recommended Posts

Try sending a message with Twilio's SMS service
Try programming with a shell!
I tried sending an SMS with Twilio
Try sending a SYN packet in Python
Try drawing a normal distribution with matplotlib
Try HTML scraping with a Python library
Create a web service with Docker + Flask
Try drawing a map with python + cartopy 0.18.0
Try TensorFlow RNN with a basic model
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
Try Tensorflow with a GPU instance on AWS
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
[2020 version] I compared popular SMS sending service companies
Try drawing a map with Python's folium package
Try creating a FizzBuzz problem with a shell program
Try making a simple website with responder and sqlite3
Try to make a command standby tool with python
Looking back on creating a web service with Django 1
Try to dynamically create a Checkbutton with Python's Tkinter
Try embedding Python in a C ++ program with pybind11
[GCP] Try a sample to authenticate users with Firebase
Looking back on creating a web service with Django 2
A sample to try Factorization Machines quickly with fastFM