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


Talk Python's Mastodon Michael Kennedy's Mastodon