1
00:00:00,270 --> 00:00:01,710
Instructor: Now that we've got the hard part

2
00:00:01,710 --> 00:00:02,543
out of the way,

3
00:00:02,543 --> 00:00:05,130
figuring out when it's actually gonna rain,

4
00:00:05,130 --> 00:00:06,780
the next step is to figure out
(text message dings)

5
00:00:06,780 --> 00:00:09,180
how we can send this as an alert

6
00:00:09,180 --> 00:00:13,080
to our phone number using an SMS service.

7
00:00:13,080 --> 00:00:15,030
So, for that, we're gonna need to use

8
00:00:15,030 --> 00:00:18,547
a different API, the Twilio API.

9
00:00:18,547 --> 00:00:21,480
Twilio is an API service that allows us

10
00:00:21,480 --> 00:00:25,590
to send text messages, or phone calls,

11
00:00:25,590 --> 00:00:29,100
or have a virtual phone number in any country,

12
00:00:29,100 --> 00:00:31,440
and if you take a look at their front page,

13
00:00:31,440 --> 00:00:33,660
you can see there's actually a whole bunch of things

14
00:00:33,660 --> 00:00:36,570
you can do with the Twilio API.

15
00:00:36,570 --> 00:00:38,850
You could build an ordering system,

16
00:00:38,850 --> 00:00:41,280
you could create a video app,

17
00:00:41,280 --> 00:00:43,080
you could have SMS to email,

18
00:00:43,080 --> 00:00:45,030
a whole bunch of things, basically.

19
00:00:45,030 --> 00:00:46,860
Now, in order to create the service,

20
00:00:46,860 --> 00:00:49,830
they obviously had to build a lot of infrastructure,

21
00:00:49,830 --> 00:00:52,290
and they've invested a lot into making this

22
00:00:52,290 --> 00:00:54,390
seamless and work well,

23
00:00:54,390 --> 00:00:57,360
so that's why if you look at the pricing section,

24
00:00:57,360 --> 00:01:02,100
they do charge for their service if you're using it a lot.

25
00:01:02,100 --> 00:01:03,210
Now, in our case,

26
00:01:03,210 --> 00:01:06,870
we're going to get started using some trial credit.

27
00:01:06,870 --> 00:01:09,300
So, they're gonna give us $10 of trial credit,

28
00:01:09,300 --> 00:01:11,880
and we can test out all of the API,

29
00:01:11,880 --> 00:01:14,730
and use any of their features.

30
00:01:14,730 --> 00:01:17,967
First, go ahead and click on, "Get started for free,"

31
00:01:19,410 --> 00:01:23,520
and you'll need to sign up to get a free Twilio account,

32
00:01:23,520 --> 00:01:24,933
no credit card required.

33
00:01:26,130 --> 00:01:27,180
Now, if you don't wanna learn

34
00:01:27,180 --> 00:01:29,250
how to send a text message to yourself,

35
00:01:29,250 --> 00:01:30,660
that's also perfectly fine.

36
00:01:30,660 --> 00:01:31,800
You don't have to sign up,

37
00:01:31,800 --> 00:01:36,270
you can just do what we did previously, using email.

38
00:01:36,270 --> 00:01:37,920
Once you've signed up with Twilio,

39
00:01:37,920 --> 00:01:39,660
you'll have to confirm your email,

40
00:01:39,660 --> 00:01:42,843
and also confirm your actual phone number.

41
00:01:43,950 --> 00:01:46,110
Once you've completed confirming

42
00:01:46,110 --> 00:01:48,360
your email and your phone number,

43
00:01:48,360 --> 00:01:50,610
then it's gonna take you to the console,

44
00:01:50,610 --> 00:01:52,567
and it's gonna ask you a few questions.

45
00:01:52,567 --> 00:01:53,520
"Do you write code?"

46
00:01:53,520 --> 00:01:54,787
Yes, of course we do.

47
00:01:54,787 --> 00:01:57,600
"What's your preferred language," Python,

48
00:01:57,600 --> 00:01:59,100
and, "What's your goal today?"

49
00:01:59,100 --> 00:02:01,980
Well, we're gonna use Twilio in a project,

50
00:02:01,980 --> 00:02:05,703
and the first thing we wanna do is to send or receive SMS.

51
00:02:06,840 --> 00:02:11,670
So, now, it's gonna take us to our Twilio project Dashboard,

52
00:02:11,670 --> 00:02:15,990
and it starts off by giving us some trial money,

53
00:02:15,990 --> 00:02:19,410
so this is the money that we can use to test out

54
00:02:19,410 --> 00:02:22,590
and use the Twilio API.

55
00:02:22,590 --> 00:02:26,910
Let's first start by clicking on, "Get a Trial Number,"

56
00:02:26,910 --> 00:02:28,650
and it's gonna generate for you

57
00:02:28,650 --> 00:02:32,100
an automatic number from the U.S.

58
00:02:32,100 --> 00:02:34,230
Now, if you want to, you can buy a number

59
00:02:34,230 --> 00:02:36,120
from a different country,

60
00:02:36,120 --> 00:02:39,420
and you can start typing any of these numbers,

61
00:02:39,420 --> 00:02:40,380
but in our case,

62
00:02:40,380 --> 00:02:43,380
it doesn't actually matter where the number is from,

63
00:02:43,380 --> 00:02:47,100
we can simply just get the default number,

64
00:02:47,100 --> 00:02:49,587
and click on, "Choose this Number."

65
00:02:51,840 --> 00:02:55,380
So, now, we have a new phone number based in the U.S.,

66
00:02:55,380 --> 00:02:59,610
we're ready to click done and start using that Twilio number

67
00:02:59,610 --> 00:03:02,940
to send ourselves some SMS messages.

68
00:03:02,940 --> 00:03:04,830
Now, we're gonna head over to Docs,

69
00:03:04,830 --> 00:03:06,873
and we're gonna go over to Quickstarts.

70
00:03:08,280 --> 00:03:11,490
Now, what we wanna do is to follow this guide,

71
00:03:11,490 --> 00:03:14,040
the programmable SMS Quickstart,

72
00:03:14,040 --> 00:03:17,370
in order to start sending SMS messages,

73
00:03:17,370 --> 00:03:18,210
so we're gonna click on

74
00:03:18,210 --> 00:03:21,150
the Python section of that Quickstart,

75
00:03:21,150 --> 00:03:22,680
and you can see, on the right,

76
00:03:22,680 --> 00:03:26,070
it gives us the example code that we need to implement

77
00:03:26,070 --> 00:03:28,860
in order to make this work.

78
00:03:28,860 --> 00:03:30,330
The first thing we need to do

79
00:03:30,330 --> 00:03:32,970
is to download the Twilio Library,

80
00:03:32,970 --> 00:03:36,930
in order to use it to import this client class.

81
00:03:36,930 --> 00:03:40,530
Let's go ahead and copy this into our project,

82
00:03:40,530 --> 00:03:44,130
so right below where we've got, "import requests,"

83
00:03:44,130 --> 00:03:48,480
let's go ahead and paste this line of code that we copied,

84
00:03:48,480 --> 00:03:51,390
and you'll notice that it has some red underlines.

85
00:03:51,390 --> 00:03:55,140
This is because we need to install this Twilio module,

86
00:03:55,140 --> 00:03:57,600
so, clicking on that underlined Twilio,

87
00:03:57,600 --> 00:04:01,860
we can click on the red light bulb and install this package.

88
00:04:01,860 --> 00:04:05,070
Once that's done, all of the errors should go away,

89
00:04:05,070 --> 00:04:08,400
and we can now start using this client class.

90
00:04:08,400 --> 00:04:10,890
Going back to the example code,

91
00:04:10,890 --> 00:04:15,890
we need to copy down our account SID and our auth token.

92
00:04:16,560 --> 00:04:20,550
We can get these details from the Twilio Dashboard.

93
00:04:20,550 --> 00:04:23,100
Notice, here, that this account SID

94
00:04:23,100 --> 00:04:27,900
is basically the identification for my Twilio accounts,

95
00:04:27,900 --> 00:04:31,560
and the auth token we'll need to get from the dashboard.

96
00:04:31,560 --> 00:04:34,263
If we go to the console here, on the top right,

97
00:04:35,850 --> 00:04:39,210
and we copy this auth token here,

98
00:04:39,210 --> 00:04:42,120
or you can click on, "Show," to see it,

99
00:04:42,120 --> 00:04:47,120
then we can paste it in here and replace this auth token,

100
00:04:47,310 --> 00:04:49,530
and just for consistency's sake,

101
00:04:49,530 --> 00:04:52,953
I'm gonna change this single quote to double quote.

102
00:04:54,180 --> 00:04:56,850
So, these are both strings.

103
00:04:56,850 --> 00:04:58,440
Now, as I mentioned before,

104
00:04:58,440 --> 00:05:03,090
these IDs that you see on-screen are not gonna work for you,

105
00:05:03,090 --> 00:05:06,090
so make sure that you've actually logged on

106
00:05:06,090 --> 00:05:08,220
and confirmed all your details,

107
00:05:08,220 --> 00:05:11,733
so that you can get your own account SID and auth token.

108
00:05:13,110 --> 00:05:15,630
Now, let's head back to our Quickstart,

109
00:05:15,630 --> 00:05:18,000
and we've done all of this.

110
00:05:18,000 --> 00:05:21,630
What we need to do now is to set up a Twilio client,

111
00:05:21,630 --> 00:05:25,620
using our account SID and auth token.

112
00:05:25,620 --> 00:05:27,960
Let's go ahead and create our client

113
00:05:27,960 --> 00:05:32,190
at the point where we want to send ourselves a SMS message,

114
00:05:32,190 --> 00:05:34,920
so I'm gonna delete all of this,

115
00:05:34,920 --> 00:05:36,720
and instead of the print statement,

116
00:05:36,720 --> 00:05:39,390
we're gonna set up our Twilio client.

117
00:05:39,390 --> 00:05:41,820
Next, we're going to create our message.

118
00:05:41,820 --> 00:05:45,600
So, this is the message that we're going to send ourselves.

119
00:05:45,600 --> 00:05:48,060
The part that we need to change is the body,

120
00:05:48,060 --> 00:05:51,270
so this is the text that we're gonna send ourselves,

121
00:05:51,270 --> 00:05:53,970
and then we're gonna change the, "from," field

122
00:05:53,970 --> 00:05:57,240
to the telephone number that we got from Twilio,

123
00:05:57,240 --> 00:06:00,150
and the, "to," number to the telephone number

124
00:06:00,150 --> 00:06:02,370
that we signed up with Twilio with,

125
00:06:02,370 --> 00:06:04,350
so let's do one thing at a time.

126
00:06:04,350 --> 00:06:06,963
The message that I want to send myself is.

127
00:06:11,670 --> 00:06:13,657
As always, on a Mac, you can go to, "Edit,"

128
00:06:13,657 --> 00:06:16,890
"Emojis & Symbols," to pull up the emoji keyboard.

129
00:06:16,890 --> 00:06:18,150
If you're on Windows or Linux,

130
00:06:18,150 --> 00:06:20,220
you can simply just head over to Google,

131
00:06:20,220 --> 00:06:24,000
and search for an umbrella emoji, and paste it in here.

132
00:06:24,000 --> 00:06:25,830
That's our message, done.

133
00:06:25,830 --> 00:06:29,880
The next step is to figure out what is the telephone number

134
00:06:29,880 --> 00:06:33,720
that is going to be the sender's telephone number.

135
00:06:33,720 --> 00:06:36,630
Going back to our Twilio Dashboard,

136
00:06:36,630 --> 00:06:40,320
we're going to copy this trial number here,

137
00:06:40,320 --> 00:06:45,320
and we're gonna paste it in the, "from," field as a string.

138
00:06:45,660 --> 00:06:47,430
The next thing is the, "to,"

139
00:06:47,430 --> 00:06:51,150
so this is who the message is going to be sent to.

140
00:06:51,150 --> 00:06:54,330
This has to be the phone number that you used

141
00:06:54,330 --> 00:06:56,520
to sign up to Twilio.

142
00:06:56,520 --> 00:06:57,870
On the Twilio trial account,

143
00:06:57,870 --> 00:07:02,040
you can send messages and make calls to verified numbers,

144
00:07:02,040 --> 00:07:03,870
and you can add verified numbers

145
00:07:03,870 --> 00:07:05,940
by clicking on this plus button,

146
00:07:05,940 --> 00:07:08,370
and you can enter somebody's phone number,

147
00:07:08,370 --> 00:07:10,200
so your friends or family,

148
00:07:10,200 --> 00:07:12,720
but you have to be able to receive a message

149
00:07:12,720 --> 00:07:15,450
or receive a call, in order to verify

150
00:07:15,450 --> 00:07:17,310
that you actually own that number,

151
00:07:17,310 --> 00:07:19,503
or you have access to that number.

152
00:07:21,120 --> 00:07:22,770
Any of the numbers that you see here,

153
00:07:22,770 --> 00:07:27,630
under, "Verified Caller IDs," can be used in your code.

154
00:07:27,630 --> 00:07:31,290
Go ahead and replace this part, where it says, "to,"

155
00:07:31,290 --> 00:07:32,673
with your verified number.

156
00:07:33,780 --> 00:07:37,680
Now, finally, it asks us to print the message

157
00:07:37,680 --> 00:07:39,063
that we created here, .sid,

158
00:07:40,530 --> 00:07:43,650
and basically, what it means is that once this message

159
00:07:43,650 --> 00:07:47,400
has an ID created, it means it was sent successfully.

160
00:07:47,400 --> 00:07:49,800
Instead of printing the message SID,

161
00:07:49,800 --> 00:07:53,610
we're going to print the message status instead,

162
00:07:53,610 --> 00:07:57,210
just to make sure that it was actually sent successfully.

163
00:07:57,210 --> 00:08:01,170
All of this code is indented inside the, "will_rain," block,

164
00:08:01,170 --> 00:08:03,120
and this is what's gonna be triggered

165
00:08:03,120 --> 00:08:05,163
if, indeed, it will rain today.

166
00:08:06,450 --> 00:08:08,610
Now, let's go ahead and test this,

167
00:08:08,610 --> 00:08:10,290
but before we can test it,

168
00:08:10,290 --> 00:08:13,620
we have to find somewhere that's actually raining.

169
00:08:13,620 --> 00:08:15,630
It seems like people in burn

170
00:08:15,630 --> 00:08:17,220
are getting quite a bit of rainfall,

171
00:08:17,220 --> 00:08:20,730
so let's update our latitude and longitude

172
00:08:20,730 --> 00:08:23,883
to burn in Switzerland.

173
00:08:28,230 --> 00:08:30,060
Now, that we've got somewhere that's raining,

174
00:08:30,060 --> 00:08:32,940
let's go ahead and run our code,

175
00:08:32,940 --> 00:08:36,539
and let's take a look at what happens on the phone.

176
00:08:36,539 --> 00:08:37,650
There you have it.

177
00:08:37,650 --> 00:08:41,136
We've got our message sent from our Twilio trial account,

178
00:08:41,136 --> 00:08:42,246
"It's going to rain today.

179
00:08:42,246 --> 00:08:44,790
"Remember to bring an umbrella."

180
00:08:44,790 --> 00:08:47,130
Now, of course, you'll be able to get rid of this part,

181
00:08:47,130 --> 00:08:49,440
where it says, "Sent from your Twilio trial account,"

182
00:08:49,440 --> 00:08:52,200
if you sign up for an actual account,

183
00:08:52,200 --> 00:08:56,100
but for all intents and purposes, this is working perfectly,

184
00:08:56,100 --> 00:08:58,560
and we're now able to receive an SMS

185
00:08:58,560 --> 00:08:59,640
when the weather condition,

186
00:08:59,640 --> 00:09:03,450
in the next 12 hours, contains some rain.

187
00:09:03,450 --> 00:09:05,460
So, the very last thing we have to do

188
00:09:05,460 --> 00:09:10,460
is set up our Python script to run every morning at 7:00 AM.

