课程: Twilio Essentials Unit 1: Introducing Twilio and Programmable Messaging

Send an SMS using the CLI

- In my opinion, the best way to explore any API is by using a CLI, or a command line interface. Well, that is if they provide one. Good news is that Twilio has a pretty awesome CLI that is wonderful for learning. If you aren't sure what a command line is, make sure you check the notes and we'll get you all sorted out. Let's get the Twilio CLI installed and configured, and then send a text message from the comfort of your command line. So the installation instructions are a little different for Mac and Windows, and I'm on a Mac at the moment, but there are instructions here. If you click between, if you're on Windows, you do the Windows. If you're on Linux, you do the Linux. So I'm going to use the instructions on this macOS and the instructions are at twil.io/cli. And the link is in the notes as well. So why don't you go ahead and pause me, and get the CLI installed in your computer, whatever operating system you have. And after you've got it installed, unpause me. Ready? You got this. Pause me. (bright music) All right. How'd it go? So I got it installed, and I can tell because if I type Twilio, at my command line, we'll now see that it comes back and it says, "Unleash the power of Twilio from your command prompt." Let's do that. Let's unleash the power. So the first thing that we need to do is we need to set up it to map to our account. So we're going to do Twilio profiles:add. And it's going to ask for our Account SID, again, that's like our username. So SID there is the string identifier. And if I go to my console, I go to console.twilio.com. I head over there and you'll see here I have this Account SID. So I'm going to copy that, this little copy button here. I'm going to flip back and I am going to paste. And then it's asking for our Auth Token. Now this is like a password. You'll notice that it's hidden. I can show it if I wanted to, but I don't want to. I trust you, but not quite that much. That's like giving you the keys to my car, or actually, that's like leaving the cars on the front seat of your car. You don't do that. Do you? That'd be weird. All right, so I'm going to paste this here. So we're going to paste the password here, Auth Token. and you'll notice that it's hidden. So you won't see it. And then I'm going to do a shorthand for my, so I can remember it later 'cause you could have multiple accounts, as you'll see. So maybe you have different projects and things like that. So I'm going to type essentials. We'll call it that. And I can just make sure that it's active. You could have a bunch remember. So we could say Twilio profiles:list. And you'll see that it is active. You'd have multiple ones here, and you'll see that it's active and it says true. Otherwise, it would say false if it wasn't. So now that we've got the account set up and authenticated to our client, we can send a text message. Let's do that. So if we flip back over to the docs, and we come in here, you'll see, again, we're at SMS API reference message resource in the Twilio CLI here for create a message. We can see it. And I'm just going to go ahead. I'm going to copy that. I'm going to copy that and I'm going to paste it into my terminal. And you'll see that we have stuff set up here. So I'm going to go ahead. And instead of from, we're going to put our phone number there. So I'm going to get rid of this. And the phone number is actually on the console as well at this point. So this is the trial number. I'm going to go ahead and copy this 'cause we only have one. So it's there on the console, and I'll paste that. Awesome. And now instead of, "Hi there," we want this to say, "Ahoy, world." That's the "Hello, world" of Twilio apps, "Ahoy, world." All right, so we did that. And then I'm going to put in my phone number 'cause we want to text it. Now, the reason why we can text to my phone number is 'cause I validated that when I set it up. Now I can only send to myself. Remember, that's the one of the limitations here. And it's going to have, as you'll see here in a second, it's going to have a little message on the front of it. Awesome. And I got it. And you'll notice it says, "Sent from your Twilio trial account -," and then what we did, "Ahoy, World". So that's the prefix that we're talking about. Any message that's sent out to a verified number during trial will have that. That's okay. We can learn to ignore it. That of course, if we upgrade, will go away, but this still works for what we're trying to do. And remember, it worked because we sent it to my number, which I verified when we set up the account. Now, one of my favorite things about the CLI is if I, I'm going to press up arrow, and I'm going to get my message back. And at the end of this, I'm also going to pass a --help. So if we were going to pass a help command here, and what will happen is we'll see notes about what we can do. And let's see what else is in here. So we could, wow, there's a bunch of stuff. Oh, here's one, media-url. The URL of a media, the media can be of type GIF, PNG, or JPEG. I've got just the media URL for us. So I'm going to go ahead. This is in the notes if you want it. I'm going to copy this. This is from the GIPHY site. I'm going to press the up arrow again. So we're going to get that back. And at the end here, what I'm going to do is I'm going to use media-url. Media-url, and I'm going to paste the GIF there. All right, so here we go. Awesome. And I got it. So exciting! And you'll see that by simply adding that image, we sent an MMS. You'll note that the MM here at the start of this SID that comes back, this MM, the one before that we sent, had an SM on it. So you'll see here, this is SM. So that's the SMS, but by just adding the media URL, we automatically converted to use the MMS. Ooh, I know what we should do. Let's talk about autocomplete. So I'm going to go ahead. I'm going to type Twilio autocomplete. And see that I'm running zsh, so I need to run this here, this command. I'm going to run and get the autocomplete setup in my zsh shell. So there we go. And now if I do, I might need to do a new window still, api:core:me. Yes. So, oh, there we go. So see how I can do messages and I can do tab, and it will show me what I can do now. If I press tab, it will automatically create. And then so I could do two, and then I could, if I needed to do that media-url, I'll do --media-url will automatically complete when I press tab. That's super powerful. Okay, so we have this create, but we can also do a list if we want to. We can list things that are here, and we can specify what properties we'd like to see. So I want to see, let's see, I want to see the SID. I want to see the body. And why don't we show the status so that we can see if it was delivered or not? So I'm going to take a look at the the messages that we sent using the API here. And I chose the specific properties. And there we go, we've got body us and the status. We've got delivered and delivered on those. Cool. And so the other thing I can do is on top of, if I wanted to get those, I could also get 'em in JSON format. So if I do this -o json, you'll see that I can get back everything in the JSON format here. Check the notes for more on how to work with CLI output if you're interested. Awesome, we got the Twilio CLI all set up, and we've sent an SMS and an MMS message. And it felt pretty much like the messaging apps on our phones. We just had to choose who to send it to, and the API did all the work for us. Now, remember, the CLI is just wrapping around the API, just like our helper libraries. You can now send a message from any of your existing application, and it's just as straightforward. You provide the to, the from, and the body. So now that we got the sending out of the way, let's take a quick break. And when you come back, let's take a look at how our number can receive a text message.

内容