Sending Text Messages with Python and Twilo

So this is a major departure from shaders, vfx and art tools but I’ve started playing around with sending SMS messages from python and wanted to keep a record.

I have a love of spooky internet mysteries, conspiracies and ARGs, and have been thinking about how I would make my own game like this. Being able to send texts to a users real phone seemed like a much cooler method of delivery than an android game that looked like an SMS app (though a spooky app as part of the game would not go remiss!).

Twilio

A quick google search turned up that my best bet for something like this is Twilio, a service that gives you a phone number and allows you to send messages directly from their web page, or using their python module with your account’s authentication in your own scripts.

I was able to use the sample script they provide in their python quickstart tutorial to send myself a message! Numbers that you can send to must be registered, and there is a charge for each text. Thankfully their trial version gives you $15 of credit, and a text is $0.08, so I’m good for now.

twilio

from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
account_sid = ‘ACb430d70d81ec0db4a6d563726a2cb121’
auth_token = ‘your_auth_token’
client = Client(account_sid, auth_token)

message = client.messages \
.create(
body=”Testing….testing…123.”,
from_=’phonenumber’,
to=’phonenumber’
)

print(message.sid)

Virtual Environment 

A virtual environment allows you to keep your base python instillation vanilla, while installing modules and updates per project. This prevents updates for new projects affecting older ones still in use.

Twilio suggests setting this up manually, but luckily for me, PyCharm does this by default when creating a new project.

virtual

Flask

Flask is a web application module for python and I used this to setup a tiny web app that will be used to take incoming requests. This allows me to see when a reply to an sms has come through, and do something in return.

I started off by just making the app.

1

from flask import Flask

app = Flask(__name__)

@app.route("/sms")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(debug = True)

Ngrok

In order to have my flask application and twilio talk, I needed to make my app available over the internet. To do this I used a tool called Ngrok.

To get this working, I had to edit my PATH environment var to include the folder where Ngrok lives. Once that was done, I could access it via command line.

23

Running this with the port that I was using with flask, I got a public tunnel to the server. Accessing this showed me the hello world script I’d written earlier.

45

Sending a Reply

Now that all the web stuff was setup, all I needed to do was use the twilio module to send a reply!

They have a message response class already setup, so all I had to do was import that and extend the original script to give it a string to reply with.

Screenshot_20190213-212533_Messages

from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route(“/sms”, methods=[‘GET’, ‘POST’])
def sms_reply():
reply = MessagingResponse()
reply.message(“Thankyou. It has been noted.”)
return str(reply)

if __name__ == “__main__”:
app.run(debug = True)

This is super cool! Hopefully I can use this to do something fun and ARG like!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s