WEBVTT

0
00:00.270 --> 00:01.710
Now that we've got the hard part

1
00:01.710 --> 00:02.543
out of the way,

2
00:02.543 --> 00:05.130
figuring out when it's actually going to rain,

3
00:05.130 --> 00:06.780
the next step is to figure out
(text message dings)

4
00:06.780 --> 00:09.180
how we can send this as an alert

5
00:09.180 --> 00:13.080
to our phone number using an SMS service.

6
00:13.080 --> 00:15.030
So, for that, we're going to need to use

7
00:15.030 --> 00:18.547
a different API, the Twilio API.

8
00:18.547 --> 00:21.480
Twilio is an API service that allows us

9
00:21.480 --> 00:25.590
to send text messages, or phone calls,

10
00:25.590 --> 00:29.100
or have a virtual phone number in any country,

11
00:29.100 --> 00:31.440
and if you take a look at their front page,

12
00:31.440 --> 00:33.660
you can see there's actually a whole bunch of things

13
00:33.660 --> 00:36.570
you can do with the Twilio API.

14
00:36.570 --> 00:38.850
You could build an ordering system,

15
00:38.850 --> 00:41.280
you could create a video app,

16
00:41.280 --> 00:43.080
you could have SMS to email,

17
00:43.080 --> 00:45.030
a whole bunch of things, basically.

18
00:45.030 --> 00:46.860
Now, in order to create the service,

19
00:46.860 --> 00:49.830
they obviously had to build a lot of infrastructure,

20
00:49.830 --> 00:52.290
and they've invested a lot into making this

21
00:52.290 --> 00:54.390
seamless and work well,

22
00:54.390 --> 00:57.360
so that's why if you look at the pricing section,

23
00:57.360 --> 01:02.100
they do charge for their service if you're using it a lot.

24
01:02.100 --> 01:03.210
Now, in our case,

25
01:03.210 --> 01:06.870
we're going to get started using some trial credit.

26
01:06.870 --> 01:09.300
So, they're going to give us $10 of trial credit,

27
01:09.300 --> 01:11.880
and we can test out all of the API,

28
01:11.880 --> 01:14.730
and use any of their features.

29
01:14.730 --> 01:17.967
First, go ahead and click on, "Get started for free,"

30
01:19.410 --> 01:23.520
and you'll need to sign up to get a free Twilio account,

31
01:23.520 --> 01:24.933
no credit card required.

32
01:26.130 --> 01:27.180
Now, if you don't want to learn

33
01:27.180 --> 01:29.250
how to send a text message to yourself,

34
01:29.250 --> 01:30.660
that's also perfectly fine.

35
01:30.660 --> 01:31.800
You don't have to sign up,

36
01:31.800 --> 01:36.270
you can just do what we did previously, using email.

37
01:36.270 --> 01:37.920
Once you've signed up with Twilio,

38
01:37.920 --> 01:39.660
you'll have to confirm your email,

39
01:39.660 --> 01:42.843
and also confirm your actual phone number.

40
01:43.950 --> 01:46.110
Once you've completed confirming

41
01:46.110 --> 01:48.360
your email and your phone number,

42
01:48.360 --> 01:50.610
then it's going to take you to the console,

43
01:50.610 --> 01:52.567
and it's going to ask you a few questions.

44
01:52.567 --> 01:53.520
"Do you write code?"

45
01:53.520 --> 01:54.787
Yes, of course we do.

46
01:54.787 --> 01:57.600
"What's your preferred language," Python,

47
01:57.600 --> 01:59.100
and, "What's your goal today?"

48
01:59.100 --> 02:01.980
Well, we're going to use Twilio in a project,

49
02:01.980 --> 02:05.703
and the first thing we want to do is to send or receive SMS.

50
02:06.840 --> 02:11.670
So, now, it's going to take us to our Twilio project Dashboard,

51
02:11.670 --> 02:15.990
and it starts off by giving us some trial money,

52
02:15.990 --> 02:19.410
so this is the money that we can use to test out

53
02:19.410 --> 02:22.590
and use the Twilio API.

54
02:22.590 --> 02:26.910
Let's first start by clicking on, "Get a Trial Number,"

55
02:26.910 --> 02:28.650
and it's going to generate for you

56
02:28.650 --> 02:32.100
an automatic number from the U.S.

57
02:32.100 --> 02:34.230
Now, if you want to, you can buy a number

58
02:34.230 --> 02:36.120
from a different country,

59
02:36.120 --> 02:39.420
and you can start typing any of these numbers,

60
02:39.420 --> 02:40.380
but in our case,

61
02:40.380 --> 02:43.380
it doesn't actually matter where the number is from,

62
02:43.380 --> 02:47.100
we can simply just get the default number,

63
02:47.100 --> 02:49.587
and click on, "Choose this Number."

64
02:51.840 --> 02:55.380
So, now, we have a new phone number based in the U.S.,

65
02:55.380 --> 02:59.610
we're ready to click done and start using that Twilio number

66
02:59.610 --> 03:02.940
to send ourselves some SMS messages.

67
03:02.940 --> 03:04.830
Now, we're going to head over to DOCS,

68
03:04.830 --> 03:06.873
and we're going to go over to Quickstarts.

69
03:08.280 --> 03:11.490
Now, what we want to do is to follow this guide,

70
03:11.490 --> 03:14.040
the Programmable SMS Quickstart,

71
03:14.040 --> 03:17.370
in order to start sending SMS messages,

72
03:17.370 --> 03:18.210
so we're going to click on

73
03:18.210 --> 03:21.150
the Python section of that Quickstart,

74
03:21.150 --> 03:22.680
and you can see, on the right,

75
03:22.680 --> 03:26.070
it gives us the example code that we need to implement

76
03:26.070 --> 03:28.860
in order to make this work.

77
03:28.860 --> 03:30.330
The first thing we need to do

78
03:30.330 --> 03:32.970
is to download the Twilio Library,

79
03:32.970 --> 03:36.930
in order to use it to import this client class.

80
03:36.930 --> 03:40.530
Let's go ahead and copy this into our project,

81
03:40.530 --> 03:44.130
so right below where we've got, "import requests,"

82
03:44.130 --> 03:48.480
let's go ahead and paste this line of code that we copied,

83
03:48.480 --> 03:51.390
and you'll notice that it has some red underlines.

84
03:51.390 --> 03:55.140
This is because we need to install this Twilio module,

85
03:55.140 --> 03:57.600
so, clicking on that underlined Twilio,

86
03:57.600 --> 04:01.860
we can click on the red light bulb and install this package.

87
04:01.860 --> 04:05.070
Once that's done, all of the errors should go away,

88
04:05.070 --> 04:08.400
and we can now start using this client class.

89
04:08.400 --> 04:10.890
Going back to the example code,

90
04:10.890 --> 04:15.890
we need to copy down our account_sid and our auth_token.

91
04:16.560 --> 04:20.550
We can get these details from the Twilio Dashboard.

92
04:20.550 --> 04:23.100
Notice, here, that this account_sid

93
04:23.100 --> 04:27.900
is basically the identification for my Twilio accounts,

94
04:27.900 --> 04:31.560
and the auth_token we'll need to get from the dashboard.

95
04:31.560 --> 04:34.263
If we go to the Console here, on the top right,

96
04:35.850 --> 04:39.210
and we copy this auth_token here,

97
04:39.210 --> 04:42.120
or you can click on, "Show," to see it,

98
04:42.120 --> 04:47.120
then we can paste it in here and replace this auth_token,

99
04:47.310 --> 04:49.530
and just for consistency's sake,

100
04:49.530 --> 04:52.953
I'm going to change this single quote to double quote.

101
04:54.180 --> 04:56.850
So, these are both strings.

102
04:56.850 --> 04:58.440
Now, as I mentioned before,

103
04:58.440 --> 05:03.090
these IDs that you see on-screen are not going to work for you,

104
05:03.090 --> 05:06.090
so make sure that you've actually logged on

105
05:06.090 --> 05:08.220
and confirmed all your details,

106
05:08.220 --> 05:11.733
so that you can get your own account_sid and auth_token.

107
05:13.110 --> 05:15.630
Now, let's head back to our Quickstart,

108
05:15.630 --> 05:18.000
and we've done all of this.

109
05:18.000 --> 05:21.630
What we need to do now is to set up a Twilio client,

110
05:21.630 --> 05:25.620
using our account_sid and auth_token.

111
05:25.620 --> 05:27.960
Let's go ahead and create our client

112
05:27.960 --> 05:32.190
at the point where we want to send ourselves an SMS message,

113
05:32.190 --> 05:34.920
so I'm going to delete all of this,

114
05:34.920 --> 05:36.720
and instead of the print statement,

115
05:36.720 --> 05:39.390
we're going to set up our Twilio client.

116
05:39.390 --> 05:41.820
Next, we're going to create our message.

117
05:41.820 --> 05:45.600
So, this is the message that we're going to send ourselves.

118
05:45.600 --> 05:48.060
The part that we need to change is the body,

119
05:48.060 --> 05:51.270
so this is the text that we're going to send ourselves,

120
05:51.270 --> 05:53.970
and then we're going to change the, "from," field

121
05:53.970 --> 05:57.240
to the telephone number that we got from Twilio,

122
05:57.240 --> 06:00.150
and the, "to," number to the telephone number

123
06:00.150 --> 06:02.370
that we signed up with Twilio with,

124
06:02.370 --> 06:04.350
so let's do one thing at a time.

125
06:04.350 --> 06:06.963
The message that I want to send myself is...

126
06:11.670 --> 06:13.657
As always, on a Mac, you can go to, "Edit,"

127
06:13.657 --> 06:16.890
"Emojis &amp; Symbols," to pull up the Emoji keyboard.

128
06:16.890 --> 06:18.150
If you're on Windows or Linux,

129
06:18.150 --> 06:20.220
you can simply just head over to Google,

130
06:20.220 --> 06:24.000
and search for an umbrella emoji, and paste it in here.

131
06:24.000 --> 06:25.830
That's our message, done.

132
06:25.830 --> 06:29.880
The next step is to figure out what is the telephone number

133
06:29.880 --> 06:33.720
that is going to be the sender's telephone number.

134
06:33.720 --> 06:36.630
Going back to our Twilio Dashboard,

135
06:36.630 --> 06:40.320
we're going to copy this trial number here,

136
06:40.320 --> 06:45.320
and we're going to paste it in the, "from," field as a string.

137
06:45.660 --> 06:47.430
The next thing is the, "to,"

138
06:47.430 --> 06:51.150
so this is who the message is going to be sent to.

139
06:51.150 --> 06:54.330
This has to be the phone number that you used

140
06:54.330 --> 06:56.520
to sign up to Twilio.

141
06:56.520 --> 06:57.870
On the Twilio trial account,

142
06:57.870 --> 07:02.040
you can send messages and make calls to verified numbers,

143
07:02.040 --> 07:03.870
and you can add verified numbers

144
07:03.870 --> 07:05.940
by clicking on this plus button,

145
07:05.940 --> 07:08.370
and you can enter somebody's phone number,

146
07:08.370 --> 07:10.200
so your friends or family,

147
07:10.200 --> 07:12.720
but you have to be able to receive a message

148
07:12.720 --> 07:15.450
or receive a call, in order to verify

149
07:15.450 --> 07:17.310
that you actually own that number,

150
07:17.310 --> 07:19.503
or you have access to that number.

151
07:21.120 --> 07:22.770
Any of the numbers that you see here,

152
07:22.770 --> 07:27.630
under, "Verified Caller IDs," can be used in your code.

153
07:27.630 --> 07:31.290
Go ahead and replace this part, where it says, "to,"

154
07:31.290 --> 07:32.673
with your verified number.

155
07:33.780 --> 07:37.680
Now, finally, it asks us to print the message

156
07:37.680 --> 07:39.063
that we created here, .sid,

157
07:40.530 --> 07:43.650
and basically, what it means is that once this message

158
07:43.650 --> 07:47.400
has an ID created, it means it was sent successfully.

159
07:47.400 --> 07:49.800
Instead of printing the message SID,

160
07:49.800 --> 07:53.610
we're going to print the message status instead,

161
07:53.610 --> 07:57.210
just to make sure that it was actually sent successfully.

162
07:57.210 --> 08:01.170
All of this code is indented inside the, "will_rain," block,

163
08:01.170 --> 08:03.120
and this is what's going to be triggered

164
08:03.120 --> 08:05.163
if, indeed, it will rain today.

165
08:06.450 --> 08:08.610
Now, let's go ahead and test this,

166
08:08.610 --> 08:10.290
but before we can test it,

167
08:10.290 --> 08:13.620
we have to find somewhere that's actually raining.

168
08:13.620 --> 08:15.630
It seems like people in Bern

169
08:15.630 --> 08:17.220
are getting quite a bit of rainfall,

170
08:17.220 --> 08:20.730
so let's update our latitude and longitude

171
08:20.730 --> 08:23.883
to Bern in Switzerland.

172
08:28.230 --> 08:30.060
Now, that we've got somewhere that's raining,

173
08:30.060 --> 08:32.940
let's go ahead and run our code,

174
08:32.940 --> 08:36.540
and let's take a look at what happens on the phone.

175
08:36.540 --> 08:37.650
There you have it.

176
08:37.650 --> 08:41.137
We've got our message sent from our Twilio trial account,

177
08:41.137 --> 08:42.247
"It's going to rain today.

178
08:42.247 --> 08:44.790
Remember to bring an umbrella."

179
08:44.790 --> 08:47.130
Now, of course, you'll be able to get rid of this part,

180
08:47.130 --> 08:49.440
where it says, "Sent from your Twilio trial account,"

181
08:49.440 --> 08:52.200
if you sign up for an actual account,

182
08:52.200 --> 08:56.100
but for all intents and purposes, this is working perfectly,

183
08:56.100 --> 08:58.560
and we're now able to receive an SMS

184
08:58.560 --> 08:59.640
when the weather condition,

185
08:59.640 --> 09:03.450
in the next 12 hours, contains some rain.

186
09:03.450 --> 09:05.460
So, the very last thing we have to do

187
09:05.460 --> 09:10.460
is set up our Python script to run every morning at 7:00 AM.