Ruan Bekker's Blog

From a Curious mind to Posts on Github

Send SMS Messages With Python and Twilio via Their API

This post will guide you through the steps on how to send SMS messages with Python and Twilio. We will use talaikis.com API to get a random quote that we will include in the body of the sms.

Signup for a Trail Account:

Sign up for a trail account at Twilio then create a number, which I will refer to as the sender number, take note of your accountid and token.

Create the Config:

Create the config, that will keep the accountid, token, sender number and recipient number:

config.py
1
2
3
4
5
6
secrets = {
    'account': 'xxxxxxxx',
    'token': 'xxxxxxx',
    'sender': '+1234567890',
    'receiver': '+0987654321'
}

Create the Client:

We will get a random quote via talaikis.com’s API which we will be using for the body of our text message, and then use twilio’s API to send the text message:

sms_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from config import secrets
from twilio.rest import Client
import requests

twilio_acountid = secrets['account']
twilio_token = secrets['token']
twilio_receiver = secrets['receiver']
twilio_sender = secrets['sender']

quote_response = requests.get('https://talaikis.com/api/quotes/random').json()

client = Client(
    twilio_acountid,
    twilio_token
)

message = client.messages.create(
    to=twilio_receiver,
    from_=twilio_sender,
    body=quote_response['quote']
)

Message Preview:

Then within a couple of seconds your message should look something more or less like this:

For more info, have a look at their docs: - https://www.twilio.com/docs/