Instructor: So previously we've managed to check
the stock price difference between the day
before yesterday and yesterday.
And then based on when that different
is over a certain threshold,
then we trigger this bunch of code
which actually fetches some news
from the news API regarding that company.
So now that we've got the top three articles
from the news API, now we're on to step three
which is to use Twilio to send a message
with each article's title and description
to our phone number.
The first step is to create a list
of the first three articles headline and description
using list comprehension.
So what we're aiming for is a single string
that says something like this, Headline:,
and then it's the actual article title.
And then we've got our Brief
which is gonna go on a new line,
and then this is going to be the article description.
You can see those parts in the articles that we printed out.
For example, this first article here.
You can see the title says Spartan Energy surges
after something, something, something, something.
You can see this is the first title
and this is the first description.
So it's basically the title of the article
and also a brief description of what the article is about.
That's what we want to get message to us.
Now in order to use list comprehension,
we first use the keyword method.
So new_item for item in list.
Now our list in this case is of course our three articles,
and each of those items will be an article in themselves.
And for each of those articles, we want to have this format.
So I'm just going to cut that and then paste it in here.
And then I'm gonna use an f string
in order to insert these relevant parts.
So the article title is stored under the title key
and the description is stored under the description key.
So we can simply use that as the key
to tap into this article.
Notice how we've got a set of double quotes
outside to create our string.
If we have another set of double quotes
to specify the key,
this is going to be a bit confusing
for the code interpreter.
So let's change that to single quotes instead
to make it actually work, like this.
Now next is the article description.
So this is gonna go inside another set of quotes
and also it's gonna go inside
a set of square brackets, like this.
Each of the new items that's gonna go into this new list
is going to be a string
that's comprised of the article title
and also the article description.
Now we can save this new list
into a formatted_articles list, like this.
So that's TODO eight completed.
And now we're gonna try and send each article
as a separate message via Twilio.
Here's our Twilio SMS Python Quickstart,
and we're basically going to replicate all of this.
So first we're gonna import the Client class
from the Twilio library.
And make sure that we actually have this installed
if it's not already installed
and get rid of the red underlines.
Next we're going to get our account SID
and auth token from Twilio.
So I'm gonna copy my account SID and put that over here,
and also get my auth token from here as well.
So now I can set up my client
by creating it from the Client class.
Down here at the point where I want to send my message
I'm going to create a new client
from the Twilio Client class.
And this is going to be comprised
of my Twilio account SID and also my Twilio auth token.
Finally, we can create our message
and send it to our own number.
The body of the message is going to be each of the articles.
So in order to send three messages,
we can loop through our three formatted_articles like this
and then we can create a message
and send each of those one by one.
So the body is going to be each article
in the formatted_articles.
Now the from number is going to be our Twilio virtual number
and our to number is going to be our actual phone number.
Once we've done all of that
we can now test this and run it to see if it actually works.
Now I'm gonna go ahead and hit run,
and hopefully our messages will get sent.
As you can see right now.
So we've got our headline and our brief
and each of the three articles
are being sent as a separate message.
That's pretty much the end of this challenge.
Now you can of course, improve this even further.
And one of the things I thought about
is having a little emoji
to show whether if your stock is up or down
and also showing a rounded percentage.
To do that, we need to figure out
whether if the stock was up or down
and we can work that out by looking at the difference.
If instead of using the abs here,
which is gonna get rid of our negative and positive,
if we had instead use an if statement
to check if the difference is greater than zero,
so it's positive,
then we can create a variable called up_down,
which starts out as none;
but if it's positive,
then we can turn that into a up emoji.
And remember, emojis are simply
just treated as strings in our code.
Otherwise, if it's below zero,
then we'll change that to a down emoji,
like this.
And as always, you can always search
for these emojis in Google and copy and paste them in
if you don't have the emoji and symbols keyboard.
But having taken away that absolute value function here,
we're going to have to put it back somewhere else.
So we're gonna put it right here
at the point where we do the if checking.
And in addition, I want to change this diff_percent
so that we actually around it to the nearest whole number.
With the rounded diff_percent and also this up_down,
we can now format our message
so that it says the name of the stock
and then whether it fits up or down
and then the diff_percent,
and finally a percentage sign and also a new line.
So now if I run this again,
you can see that this time these messages come complete
with the stock name, the movement percentage,
the headlines, and the brief.
So there you have it.
I'm sure you can think
of even more improvements to this program.
And especially if you're somebody who actually trade stocks,
then I'm sure you'll think of even more ways
of improving this project.
If you come up with anything interesting and fun,
be sure to share it with the rest of us in the Q&A
so that we can all admire your hard work.