Now, we'll connect our bot to the News API we demoed earlier this class.
The end goal of this is to have a bot that responds to keywords and phrases with news articles that match those keywords or phrases. We'll start by registering for the News API.
We'll start by heading the News API website, at https://newsapi.org/ and clicking the big blue Get API key button.
You'll have to fill out a few fields to register for this service — you may register as an individual.
The next screen after registering should be the API key that you receive:
Copy this API key! Not this one! The! one! on! your! screen!
Similarly to how we saved our Facebook access token, head into your Whales terminal at http://localhost:7331 and run:
EDITOR=nano rails credentials:edit
This will open the nano
editor. Use your arrow keys to navigate to the bottom of your file and add:
news_token: <paste in your NewsAPI.org token here>
Type Ctrl-O and Enter to save the file and Ctrl-X to exit the nano
editor.
Now, modify your receive
route in your MainController
to use the News API with the text message that the user sends you. You can send a link to an article and perhaps the headline, if you'd like.
Note: You should be constructing your own request here and send relevant information to the user with send_message
. There's some guidance below, but there is no step-by-step guide for this part. Try it yourself!
Check out the /v2/top-headlines
docs here (https://newsapi.org/docs/endpoints/top-headlines).
If you check out the table of parameters that this endpoint has, you'll see that there are ways to filter headlines by categories, sources, and country. Here are the important ones that we need:
apiKey
: This should be Rails.application.credentials.news_token
, which we stored above.