SMS - Tutorial: Send an SMS in Python with a Telesign REST API
This tutorial teaches how to use the Telesign Engage API to send an SMS message. Go to GitHub to see the complete sample code.
Before you begin
Make sure you have the following before you start:
- Authentication credentials: Your Customer ID and API Key. If you need help finding these items, go to the support article How do I find my Customer ID and API Key.
- Testing device: A mobile phone on which you can receive SMS.
NOTE:
This tutorial uses Python 3.8.1. Please modify accordingly if you are using a different version of Python.
This tutorial use a Mac, please modify accordingly if you are using a different system.
Set up your project
- Create a new directory for your project. If you plan to create multiple Python projects that use Telesign, we recommend that you group them within a
telesign_integrations
directory. Create a directory for each Telesign product, and within that, for each use case.
cd ~/code/local/telesign_integrations
mkdir sms; mkdir sms/send_arn
- Create a "shared" directory and enter it. This is where you will download utilities that are used across all Telesign projects.
mkdir shared
cd shared
-
Copy the
ts_auth.py
file from Telesign's GitHub site to this directory. This script will handle generating a Basic authentication header for your integration. -
Go back up a level and then enter the directory for your project.
cd ../sms/send_arn
- Create a new file for the script that will send an SMS, and open it in your code editor.
touch send.py
Create code to import your dependencies
- Add code to import the dependencies.
from requests import Request, Session
import os
import sys
- Add the directory that contains
ts_util.py
to the locations your script pulls dependencies from.
sys.path.append('../../shared/')
- Add code to import the
ts_auth
dependency.
import ts_auth
Create code to define your request contents
- Define variables to store your Telesign authentication credentials; either replace the defaults below or set them as environment variables.
customer_id = os.getenv('CUSTOMER_ID', 'ABC1DE23-A12B-1234-56AB-AB1234567890')
api_key = os.getenv('API_KEY', 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==')
- Set the REST API URL.
url = "https://rest-ww.telesign.com/v1/messaging"
- Create variables to store the request inputs. Change the default below to your test phone number or set it as an environment variable. In your production code, update the phone number dynamically for each transaction. Be sure to include the country code in your test phone number and in production to avoid any errors.
phone_number = os.getenv('PHONE_NUMBER', '11234567890')
message = "Hello world"
message_type = "ARN"
NOTE:
In your production integration, have phone_number pull from your recipient database instead of hardcoding it. Make sure you include the appropriate country code for each number in your database.
- Set the request headers.
headers = {
"Content-Type": "application/x-www-form-urlencoded",
'Date': ts_auth.format_current_date()
}
- Create the payload for the request.
payload = f"phone_number={phone_number}&message={message}&message_type={message_type}"
Create code to send the SMS request
- Create the session and prepped request.
s = Session()
req = Request('POST', url, data=payload, headers=headers)
prepped_request = req.prepare()
- Add the authentication header to the prepped request.
request_properties = {
"method": prepped_request.method,
"headers": prepped_request.headers,
"body": prepped_request.body,
"url": prepped_request.url
}
prepped_request.headers = ts_auth.add_basic(request_properties, customer_id, api_key)
- Make the request and capture the response.
response = s.send(prepped_request)
- Display the request and the response in the console for debugging purposes. In your production code, you would likely remove this.
ts_auth.pretty_print_request(prepped_request)
print(f"Response:\n{response.text}\n")
Sample code
The complete sample code for this tutorial can be found on GitHub.
Updated about 1 year ago