Skip to content

osscameroon/caparledev-bot

Repository files navigation

CaParleDev Bot

Twitter bot used to retweet all tweets having the hashtag #caparledev

Prerequisites

  • Node.js
  • MongoDB
  • Typescript

Installation

  • Clone the repository
git clone https://github.com/osscameroon/caparledev-bot.git [project_name]

cd [project_name]
  • Create configuration file and edit with your own value
cp .env.example .env
nano .env
  • Create a Twitter application
  1. Login to your Twitter account on developer.twitter.com.
  2. Navigate to the Twitter App dashboard
  3. If you have an existing app, go to the next step. If not, to create an app, you will be first asked to create a project. You can give the app the name you want. When asked the access level required for your app, select Read Only, it will be enough for the script to work.
  4. Open the app and navigate to the "keys and tokens" page.
  5. Copy the Consumer Key and Secret, Access Token and Secret
  • Set credentials in .env
TWITTER_APP_CONSUMER_KEY=consumer_key
TWITTER_APP_CONSUMER_SECRET=consumer_secret
TWITTER_APP_ACCESS_TOKEN_KEY=access_token_key
TWITTER_APP_ACCESS_TOKEN_SECRET=access_token_secret
  • Start application
yarn watch # On Terminal 1
yarn start # On Terminal 2
  • Launch ngrok tunnels to the app
yarn ngrok
  • Register the auth callback in twitter application

    Copy the url generated by Ngrok, go to to your Twitter app in developer portal, locate the section the section "CALLBACK URLS", paste the url and save the changes.

  • Call the endpoint to set generate auth URL

# The call
curl http://localhost:7432/auth/url

# The response
{"url":"https://api.twitter.com/oauth/authenticate?force_login=true&oauth_token=Qj8UOwBBAAABTOKhAAABdCZxfsI"}
  • Authenticate with the bot account

Navigate to the URL in the browser and login with the bot account. After successful login, you will receive a JSON reponse with OAuth access token key and OAuth access token secret

{
  "oauthToken": "OAuth access token key",
  "oauthTokenSecret": "OAuth access token secret"
}
  • Set in config file the OAuth token key and secret generated
TWITTER_BOT_ACCESS_TOKEN_KEY=oauth_access_token_key
TWITTER_BOT_ACCESS_TOKEN_SECRET=oauth_access_token_secret
  • Generate Bearer token
yarn bearer:token

Open the .env file and set the generated bearer token

TWITTER_BEARER_TOKEN=bearer_token
  • Enable Twitter stream and set the hashtag(s) you want to stream.

Separate hashtag with a comma if you have many. You can set up to 5000

ENABLE_STREAM=true
HASHTAG_TO_TRACK=#hastag1,#hastag2,#hastag3,....,#hastagn
  • Restart the app
yarn start