#100DaysOfWeb in Python Transcripts
Chapter: Days 77-80: Twitter and Slack bots
Lecture: Create a Flask app and call our books API

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Back at my terminal let's make a virtual environment first. So I made directory book bot. I'm going to create a virtual environment like this.
0:14 And before activating this virtual environment I'm going to store the Slack verification token in the activation script.
0:28 We're going to go down and then type. And we need to retrieve that from Slack. So I'll go back to my app in basic information
0:41 and here is the verification token. Paste that in and we're done. Next I source it so now I'm in my virtual
0:59 environment and let's install the modules we're going to use. And as for best practice I create a requirements file may I upload this code
1:14 to GitHub at some point others know what's required.
1:32 Good. Next I prepared a little bit of template code to reduce the typing. So we're going to use os, Flask and request
1:44 and have some constants set up, book thumbnail which is a string we can later put the book ID into to get a thumbnail for our message
1:56 to Slack and a random book endpoint. And you saw this on the slides. And here I get a random book upon every request.
2:15 First we need to log in to Slack though can't be just stored in the environment. And a way to do that is with os.environ.
2:28 Next we need to instantiate a Flask application. And then all that's left really is the endpoint. So when we run our server, the server address
2:45 slash Slack is our endpoint and it needs to receive a published request. Then we set up a function and call that anything.
2:54 And it's going to receive the payload inside request.form. And first of all I'm going to see if the valid token is sent.
3:12 So we're going to only allow payload that has a token that's equal to our Slack token. If not we abort. So we cannot get any type of post request.
3:23 It needs to come from Slack. Next let's load in the random book from the random book endpoint and request makes that very easy.
3:40 And I'm going to grab that JSON response which the response object conveniently has in JSON. Then we're going to make an image link
3:52 and we're going to call format on that book thumb string constant. Then I'm going to pass in book ID. And that's coming from the book JSON.
4:06 And it will be book ID. And you can see that here. The JSON will have, it's like a dictionary so it'll have book ID title, URL, authors
4:20 et cetera, and we can just grab those attributes as if it was a dictionary. Next I'm going to set up a message using F string.
4:46 And notice the on this course, it's nothing to do with Python, well, the on this course Python, but in this case it's something else.
4:54 I'm using the Slack formatting. So using on this course the author's string will be in italics. That's it for this video and in the next video
5:09 we're going to format the message back to Slack and use end grok to test it out.


Talk Python's Mastodon Michael Kennedy's Mastodon