Instructor: Now that we've got the hard part
out of the way,
figuring out when it's actually gonna rain,
the next step is to figure out (text message dings)
how we can send this as an alert
to our phone number using an SMS service.
So, for that, we're gonna need to use
a different API, the Twilio API.
Twilio is an API service that allows us
to send text messages, or phone calls,
or have a virtual phone number in any country,
and if you take a look at their front page,
you can see there's actually a whole bunch of things
you can do with the Twilio API.
You could build an ordering system,
you could create a video app,
you could have SMS to email,
a whole bunch of things, basically.
Now, in order to create the service,
they obviously had to build a lot of infrastructure,
and they've invested a lot into making this
seamless and work well,
so that's why if you look at the pricing section,
they do charge for their service if you're using it a lot.
Now, in our case,
we're going to get started using some trial credit.
So, they're gonna give us $10 of trial credit,
and we can test out all of the API,
and use any of their features.
First, go ahead and click on, "Get started for free,"
and you'll need to sign up to get a free Twilio account,
no credit card required.
Now, if you don't wanna learn
how to send a text message to yourself,
that's also perfectly fine.
You don't have to sign up,
you can just do what we did previously, using email.
Once you've signed up with Twilio,
you'll have to confirm your email,
and also confirm your actual phone number.
Once you've completed confirming
your email and your phone number,
then it's gonna take you to the console,
and it's gonna ask you a few questions.
"Do you write code?"
Yes, of course we do.
"What's your preferred language," Python,
and, "What's your goal today?"
Well, we're gonna use Twilio in a project,
and the first thing we wanna do is to send or receive SMS.
So, now, it's gonna take us to our Twilio project Dashboard,
and it starts off by giving us some trial money,
so this is the money that we can use to test out
and use the Twilio API.
Let's first start by clicking on, "Get a Trial Number,"
and it's gonna generate for you
an automatic number from the U.S.
Now, if you want to, you can buy a number
from a different country,
and you can start typing any of these numbers,
but in our case,
it doesn't actually matter where the number is from,
we can simply just get the default number,
and click on, "Choose this Number."
So, now, we have a new phone number based in the U.S.,
we're ready to click done and start using that Twilio number
to send ourselves some SMS messages.
Now, we're gonna head over to Docs,
and we're gonna go over to Quickstarts.
Now, what we wanna do is to follow this guide,
the programmable SMS Quickstart,
in order to start sending SMS messages,
so we're gonna click on
the Python section of that Quickstart,
and you can see, on the right,
it gives us the example code that we need to implement
in order to make this work.
The first thing we need to do
is to download the Twilio Library,
in order to use it to import this client class.
Let's go ahead and copy this into our project,
so right below where we've got, "import requests,"
let's go ahead and paste this line of code that we copied,
and you'll notice that it has some red underlines.
This is because we need to install this Twilio module,
so, clicking on that underlined Twilio,
we can click on the red light bulb and install this package.
Once that's done, all of the errors should go away,
and we can now start using this client class.
Going back to the example code,
we need to copy down our account SID and our auth token.
We can get these details from the Twilio Dashboard.
Notice, here, that this account SID
is basically the identification for my Twilio accounts,
and the auth token we'll need to get from the dashboard.
If we go to the console here, on the top right,
and we copy this auth token here,
or you can click on, "Show," to see it,
then we can paste it in here and replace this auth token,
and just for consistency's sake,
I'm gonna change this single quote to double quote.
So, these are both strings.
Now, as I mentioned before,
these IDs that you see on-screen are not gonna work for you,
so make sure that you've actually logged on
and confirmed all your details,
so that you can get your own account SID and auth token.
Now, let's head back to our Quickstart,
and we've done all of this.
What we need to do now is to set up a Twilio client,
using our account SID and auth token.
Let's go ahead and create our client
at the point where we want to send ourselves a SMS message,
so I'm gonna delete all of this,
and instead of the print statement,
we're gonna set up our Twilio client.
Next, we're going to create our message.
So, this is the message that we're going to send ourselves.
The part that we need to change is the body,
so this is the text that we're gonna send ourselves,
and then we're gonna change the, "from," field
to the telephone number that we got from Twilio,
and the, "to," number to the telephone number
that we signed up with Twilio with,
so let's do one thing at a time.
The message that I want to send myself is.
As always, on a Mac, you can go to, "Edit,"
"Emojis & Symbols," to pull up the emoji keyboard.
If you're on Windows or Linux,
you can simply just head over to Google,
and search for an umbrella emoji, and paste it in here.
That's our message, done.
The next step is to figure out what is the telephone number
that is going to be the sender's telephone number.
Going back to our Twilio Dashboard,
we're going to copy this trial number here,
and we're gonna paste it in the, "from," field as a string.
The next thing is the, "to,"
so this is who the message is going to be sent to.
This has to be the phone number that you used
to sign up to Twilio.
On the Twilio trial account,
you can send messages and make calls to verified numbers,
and you can add verified numbers
by clicking on this plus button,
and you can enter somebody's phone number,
so your friends or family,
but you have to be able to receive a message
or receive a call, in order to verify
that you actually own that number,
or you have access to that number.
Any of the numbers that you see here,
under, "Verified Caller IDs," can be used in your code.
Go ahead and replace this part, where it says, "to,"
with your verified number.
Now, finally, it asks us to print the message
that we created here, .sid,
and basically, what it means is that once this message
has an ID created, it means it was sent successfully.
Instead of printing the message SID,
we're going to print the message status instead,
just to make sure that it was actually sent successfully.
All of this code is indented inside the, "will_rain," block,
and this is what's gonna be triggered
if, indeed, it will rain today.
Now, let's go ahead and test this,
but before we can test it,
we have to find somewhere that's actually raining.
It seems like people in burn
are getting quite a bit of rainfall,
so let's update our latitude and longitude
to burn in Switzerland.
Now, that we've got somewhere that's raining,
let's go ahead and run our code,
and let's take a look at what happens on the phone.
There you have it.
We've got our message sent from our Twilio trial account,
"It's going to rain today.
"Remember to bring an umbrella."
Now, of course, you'll be able to get rid of this part,
where it says, "Sent from your Twilio trial account,"
if you sign up for an actual account,
but for all intents and purposes, this is working perfectly,
and we're now able to receive an SMS
when the weather condition,
in the next 12 hours, contains some rain.
So, the very last thing we have to do
is set up our Python script to run every morning at 7:00 AM.