![]() Why? Because this is the way that Facebook interacts with your bot to validate that you’re a provider. A minimal Flask application looks something like this, but we will need something a little more elaborated. Hint: if you need a basic tutorial for the process of making a Fan Page and an App in Facebook, here is a good one. Please take note of this, because you will need it a couple of times And you need to make a new app because Facebook will give you an ACCESS TOKEN that you need to use to connect your bot. You need to have a Fan Page to have a bot that interacts in your name. The first and second steps are following the Facebook Tutorial to make a Fan Page and a new App. And ngrok is a tool that makes a tunnel between your local and their server, so you will have a public and working URL, even if you're working only in local.įrom this moment, we will have to do a couple of things simultaneously. Well, that is the reason we're using Flask and ngrok.įlask is a Python framework that allows you to have a server running locally. To be able to do that you have to have an app on production.īut, wait? We don't have an app in production yet. def random_url (lst): "This function return a random choice from the list of popular images of the wholesomememes subreddit"Ī Facebook Messenger chatbot is simply a web app that answers when somebody makes a connection with a FanPage. With those functions working I have now all the information that I want to use in my bot. My third function simply makes a random choice of a URL from the previous list. def get_imgs_bot (reddit): "This function returns a list with the first most populars images in the subreddit /wholesomememes"įor submission in reddit.subreddit( 'wholesomememes').hot(limit= 20): For simplicity, I'm saving those 20 URLs in a list. So, if I need the URL of those post, I just have to search for "submission.url". Praw calls this "submissions." One of the advantages of using praw, is that it returns the information as objects. My second function takes the Reddit session as a parameter, uses the session to go to the subreddit that I wanted ("wholesomememes), and grabs the first 20 most popular posts. Reddit = praw.Reddit(username=os.environ,Ĭlient_id=os.environ,Ĭlient_secret=os.environ, def bot_loggin (): "This function return your session in Reddit as an instance" It returns that session as an object, which I can use in other functions. My first function uses all my secret variables to connect my session to Reddit. It’s called “praw.” I use praw with “pip install praw”, to build a little scrappy script. There is a library in python that allows you to connect to Reddit and create a session with their API to handle all the request as objects. This will set the variable in your local environment, where we can later import them without committing this sensitive data to GitHub. In your terminal, type the following command:Įxport 'NAME_OF_YOUR_VARIABLE'=someverysecretvaluethatyouwanttokeep For that, you can import os on the top of your script, and simply keep all the variables as key, value pairs. The best way to save info without any risk of exposing it on Github is to make those keys available as environment variables. Reddit will give you four variables that will be used to connect your session to their API. For that, you have to create a new app in. The First step is going to Reddit to register as a developer. When starting a side project, the first question is often: where will you find your data? Fortunately, for this project, I knew exactly where to look: one of my favorite sub-reddits:r/wholesomememes. To do that, the first thing Groot does is connect with the Reddit API and grab the first 20 pictures from the subreddit. Groot will send you a random picture from the Subreddit r/wholesomememes to try to cheer up your day. Here are the requirements.txt for the project: flask You will need to know basic python, specially Flask, and a little of SQL to build this bot. Hopefully, this little article will help you get started on your own Facebook Messenger bot. So, I learned how to build a simple chat bot: “Groot the wholesome chat bot.” At the last F8 event I was super impressed by the use of the messenger bots I knew I had to jump in and try myself.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |