Set up the bot

Before touching the terminal or writing any code we'll need to create a Twitter app to get our API keys, we'll need them all:

Consumer Key (API Key)
Consumer Secret (API Secret)
Access Token
Access Token Secret

Keep the keys somewhere safe so you can use them again when you need them, we're going to be using them in the .env file we're going to create.

We're using dotenv so that if at some point in the future we want to add our bot to GitHub the Twitter API keys are not added to GitHub for all to see.

Starting from scratch, create a new folder via the terminal and initialise the package.json via npm or yarn we'll need twit and dotenv for all these examples.

I'll be using yarn for all these examples, you can use npm if you prefer.

Terminal commands:

mkdir tweebot-play
cd tweebot-play
npm init -y
npm i twit dotenv # i is shorthand for install
touch .env .gitignore index.js

If you take a look at the package.json that was created it should look something like this:

{
  "name": "tweebot-play",
  "version": "1.0.0",
  "main": "index.js",
  "author": "Scott Spence <[email protected]> (https://blog.scottspence.me/)",
  "license": "MIT",
  "dependencies": {
    "dotenv": "^4.0.0",
    "twit": "^2.2.9"
  }
}

Add an npm script to the package.json to kick off the bot when we're testing and looking for output:

  "scripts": {
    "start": "node index.js"
  },

It should look something like this now:

{
  "name": "tweebot-play",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "Scott Spence <[email protected]> (https://blog.scottspence.me/)",
  "license": "MIT",
  "dependencies": {
    "dotenv": "^4.0.0",
    "twit": "^2.2.9"
  }
}

Now we can add the following pointer to the bot in index.js, like so:

require('./src/bot')

So when we use npm run start to run the bot it calls the index.js file which runs the bot.js file from the src folder we're going to create.

Now we add our API keys to the .env file, it should look something like this:

CONSUMER_KEY=AmMSbxxxxxxxxxxNh4BcdMhxg
CONSUMER_SECRET=eQUfMrHbtlxxxxxxxxxxkFNNj1H107xxxxxxxxxx6CZH0fjymV
ACCESS_TOKEN=7xxxxx492-uEcacdl7HJxxxxxxxxxxecKpi90bFhdsGG2N7iII
ACCESS_TOKEN_SECRET=77vGPTt20xxxxxxxxxxxZAU8wxxxxxxxxxx0PhOo43cGO

In the .gitignore file we need to add .env and node_modules

# Dependency directories
node_modules

# env files
.env

Then init git:

git init

Ok, now we can start to configure the bot, we'll need a src folder a bot.js file and a config.js file.

Terminal:

mkdir src
cd src
touch config.js bot.js

Then we can set up the bot config, open the config.js file and add the following:

require('dotenv').config()

module.exports = {
  consumer_key: process.env.CONSUMER_KEY,
  consumer_secret: process.env.CONSUMER_SECRET,
  access_token: process.env.ACCESS_TOKEN,
  access_token_secret: process.env.ACCESS_TOKEN_SECRET,
}

Ok, that's the bot config done now we can set up the bot, each of the examples detailed here will have the same three lines of code:

const Twit = require('twit')
const config = require('./config')

const bot = new Twit(config)

Add these three lines to the bot.js file.

Ok, that's it our bot is ready to go, do a test with npm run start from the terminal, we should get a similar output to this:

npm start
$ node index.js
Done in 0.64s.

Bot is now configured and ready to go!🚀

Next: Post Statuses.

results matching ""

    No results matching ""