#100DaysOfWeb in Python Transcripts
Chapter: Day 51: Twilio
Lecture: Write a quick and easy Twilio SMS script

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now you will think I'm a bit crazy
0:01 but this is going to be the world's simplest script.
0:05 Twilio really makes it easy for us to send
0:07 SMS's with Python, using their API.
0:10 So let's pop in to a file
0:12 we'll call it twilio_sms.py.
0:18 And surprisingly, we have to import Twilio.
0:21 No, I'm kidding, that's not a surprise at all.
0:23 Let's do from Twilio dot rest
0:25 because we're using their rest API.
0:28 Import client.
0:30 That is the only function that we need to import.
0:34 It's pretty crazy, right?
0:36 Now remember those two IDs that we collected before
0:39 the SID and your authentication token?
0:43 This is where you specify that.
0:46 So we could use an OS variable
0:49 we could just throw this in there, whatever.
0:52 I'm obviously not going to put that in right now.
0:55 And let's actually put in auth token
0:59 as our second object here.
1:01 You'll pop your token in there as well.
1:05 Now, the really really difficult simple part.
1:09 We have to set up the client object
1:11 using the client function that we imported at the top.
1:16 To do that, we call it client
1:18 and we give it the account SID.
1:22 So I was being sarcastic when I said this was difficult
1:25 because it's really not difficult.
1:28 Next, we call on client, and this time
1:31 we're going to say use messages.create.
1:35 Really simple, right?
1:37 Now let's just make this human readable.
1:39 We could do this all on one line, but let's just pop down
1:43 give us our four spaces
1:45 and the first thing that we need to set is to.
1:49 So who are we sending this to?
1:52 So remember I said you needed to use a phone number here?
1:54 This is pretty much it.
1:56 Now keep in mind, for this to work
1:59 we need to use the phone number
2:01 that we put in when we created our account.
2:05 Remember it asked you to verify your phone number?
2:09 Well, that wasn't just to prove you're a person
2:11 with a two-factor key.
2:13 This is also to allow you to receive messages.
2:17 So if I wanted to, perhaps, spam Bob right now
2:21 with a few text messages
2:23 Twilio wouldn't allow me to do that.
2:25 And that's because Bob has not verified his phone number
2:29 with this Twilio project.
2:31 I'll explain that in another video, but for now
2:34 just enter in the phone number that you put in there.
2:37 I'm obviously not putting my number in right now
2:40 for the sake of this video, but we'll put in my area code.
2:43 I'll edit this off recording.
2:47 Now we also need to put a from object in here.
2:51 Now this is the phone number that our application used.
2:55 So mine was +61, this is the free number that
2:59 Twilio set up, four eight zero zero one five three two five.
3:06 And you'll do the same for yours.
3:09 Syntactical error there, forgot to put the comma.
3:12 Remember this is still technically one line.
3:15 And then we have the body of the text message we're sending.
3:19 So what do we want to say?
3:20 We want to say, oh I don't know, welcome to the
3:24 one hundred days of web course using Twilio.
3:29 That's it, nice and simple, right?
3:32 Then we can close that off, and that is our script.
3:38 Your job here is to give me two seconds
3:41 I'll use the magic of the recording tool
3:45 to add my stuff in here without you seeing it
3:47 and then, we'll run it on the command line.
3:50 And with my SID, my auth token, and my phone number
3:54 inserted into that, we can simply run python twilio_sms
4:00 and we should actually not get anything
4:03 but a returned empty line.
4:06 And that will mean that our script
4:08 successfully ran or had no errors.
4:11 And then, at this exact moment
4:13 my phone should be going off and it is.
4:16 And I have a text message on my mobile saying
4:19 sent from your Twilio trial account
4:22 welcome to the one hundred days of web course using Twilio.
4:26 And that's literally it.
4:28 We just used the Twilio REST API with Python
4:33 to send ourselves a text message.
4:36 Really simple, really effective
4:38 and a lot of stuff we can do with this.