Introduction
In this article, we will learn how to send text messages using Python and a non-A2P 10DLC service. Specifically, we will be using the Twilio API to send SMS messages through a toll-free number. Before we begin, it is important to note that Twilio requires business information such as a business name and company URL for toll-free verification, but A2P 10DLC registration asks for additional information such as a business name, vertical, and campaign description.
Prerequisites
- Python 3.6 or higher
- Twilio account with a toll-free number
Setting up your Twilio account
To set up your Twilio account, follow these steps:
- Go to the Twilio website and sign up for a free account.
- Purchase a toll-free number by going to the Phone Numbers section of your account.
- Note down your Account SID and Auth Token, which can be found in the Project Settings section of your account.
Installing the Twilio Python library
To install the Twilio Python library, open your terminal or command prompt and type the following command:
pip install twilio
Writing the Python code
Here's the code to send a text message using the Twilio API:
from twilio.rest import Client
def send_sms(to, message):
client = Client(account_sid='YOUR_ACCOUNT_SID', auth_token='YOUR_AUTH_TOKEN')
message = client.messages.create(
body=message,
from_='+12345678901', # Your Twilio number
to=to)
print(message.sid)
to = '+09876543210' # Destination number
message = 'Hello from Twilio!'
send_sms(to, message)
In the above code, replace the placeholder values for Account SID, Auth Token, and Twilio number with your own values. Also, replace the destination number with the number you want to send the message to.
How it works
Here is a breakdown of what the code does:
- We import the
Clientclass from thetwilio.restmodule. - We define a function called
send_smsthat takes in two parameters:toandmessage. - Inside the function, we create a Twilio client using our Account SID and Auth Token.
- We use the
messages.createmethod of the Twilio client to send an SMS message. The parameters for this method are: body: The message to sendfrom_: The Twilio number, with a '+' sign and the country code at the beginningto: The destination number, with a '+' sign and the country code at the beginning
Once the message is sent, the unique message SID is printed to the console.
- Twilio requires business information for toll-free verification, but A2P 10DLC registration asks for additional information.
- The Twilio Python library can be installed using pip.
- The Twilio API can be used in Python to send text messages through a toll-free number.