Node Twitterbot Directions

This post consists of notes to myself, so that I can remember the commands to create a Node.js Twitterbot hosted on Heroku.

  1. Create a Github repo and clone it locally

  2. Create a .gitignore with .env and node_modules/
  3. Use npm to generate the initial project.
    npm init

    npm asks questions and builds a package.json file.

  4. Install some packages
    npm install dotenv
    npm install twit
    npm install express
  5. Create .env_sample with this
    CONSUMER_KEY = ...
    ACCESS_KEY = ...
  6. Create index.js with this starter code
    const express = require('express');
    const app = express();
    const Twit = require('twit');
    // const T = new Twit({
    //   consumer_key:         process.env.CONSUMER_KEY,
    //   consumer_secret:      process.env.CONSUMER_SECRET,
    //   access_token:         process.env.ACCESS_KEY,
    //   access_token_secret:  process.env.ACCESS_SECRET,
    //   timeout_ms:           60*1000,  // optional HTTP request timeout to apply to all requests.
    //   strictSSL:            true,     // optional - requires SSL certificates to be valid.
    // });
    // yr code here
      process.env.PORT || 3000,
      ()=>console.log("bot running")
  7. Write your code in index.js

  8. Use app.listen to log local, console-based tests. Run the tests with the command
    node index.js
  9. Create .env and add the API keys, then run live tests that post to Twitter.
    CONSUMER_KEY = ...
    ACCESS_KEY = ...
  10. Commit and push to Github. Create a public branch for Github and publish it
    git checkout -b Github_Public
    git push -u origin Github_Public
  11. Go to the website for the Github repo, click Settings > Branches, and change Github_Public to be the default branch

  12. Commit/push the Github_Public branch. Switch to the master/main branch.

  13. Add a file named Procfile that contains the following
    worker: node index.js
  14. Check that .gitignore is empty. Go to Heroku’s website. New > Create New App.
    heroku login
    heroku git:remote -a goodnitebot
    git add .
    git commit -am "make it better"
    git push heroku master
  15. Back to Heroku’s website. Click on your app, then Resources. Edit > disable Web > Confirm. Edit > enable Worker > Confirm. Check Twitter.