[Part 1] Automating boring work with Heroku

Tram Ho

I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it. – Bill Gates

Opening

Mr. Gates told me that I always wanted to choose lazy people because they would find a way to do it more simply. More than half, sometimes there is some boring work to be repeated many times, in a day. For a developer, why not find a more leisurely way to do it? Today I will introduce you how to automate some jobs with NodeJS and Heroku, completely free. After that, you can apply to your current work such as sending mail, automatically happy birthday on Facebook, and dozens more.

Apply.

To apply, I will guide you to write a fun script to apply automation to try some simple things. I will write a script that takes articles from Subreddit r / listentothis and posts to Facebook Page with NodeJS once every hour.

The series consists of two parts:

  • Write script
  • Automate with Heroku

Which code

First, I will write a script so that it automatically takes Subreddit posts and posts to Facebook Page. The specific steps will be as follows:

  1. Create a project
  2. Api Config of Red Ass
  3. Get a random post from Subreddit r / listentothis
  4. Config Facebook Page API
  5. Post status to Facebook Page

Create a project

Here I will use NodeJS with some packages such as snoowrap for Reddit API and node-fetch to post to Page. Also use dotenv to configure more envionement variables. Easy

Red Ass Config

To Config Red Data, I use the Snoowrap package. The setup consists of two steps: creating a Reddit App and creating a permanent Token.

Create the Reddit App

First, go to https://www.reddit.com/prefs/apps to create the Reddit App.

Screenshot-from-2019-11-16-23-46-34

Under Redirect URL remember to type url https://not-an-aardvark.github.io/reddit-oauth-helper/ . It will help you to create permanent tokens faster.

After creating, save the Client ID and Client Secret again.

Create Token permanently

Create a permanent token then continue to https://not-an-aardvark.github.io/reddit-oauth-helper/ . Then enter the Client Secret and Client ID. Remember to tick Permanent and then create Token. Scrolling down it will display the Token. Save the 2 Token that is again.

Reddit-OAuth-Helper - 1-

Get random posts from Subreddit

Here I started to index.js file to start the Code.

The result will return the Object a random post from Subreddit r / listentothis. Here I will take only 3 value demo: Title, Author and Url of that bowl.

The following results

Config Facebook Page API

Next, you have to create Page, then create App on Facebook Developer . And get the token permanently. You do not know how to get a permanent token, see here . This paragraph has many tutorials so I’m too lazy to rewrite.

Post Status on Page

Next, I post status to Page using Facebook Page API only.

Tested

Turn on To Mi Go and type node index.js to test. If the following result is correct

Conclude

Here is considered as a simple script, automatically get the post on Reddit to post via Facebook Page already. You can see the results in their Page http://fb.com/listenthisawesomesong . Source Code of this script I am here https://github.com/nguyen47/listen-to-this The following part I will introduce how to automate this with Heroku. Simple and no cost at all.

Currently I am learning to write Blog at https://htknguyen.com/ . If you are interested, then visit my blog to hear me slashing the wind up in the perspective of a leprous Developer. Shoot heart

Share the news now

Source : Viblo