Create A Twitter Bot Using R

Create a twitter bot in the R programming language

Image for post
Image for post

Prerequisites:

0. You will need a twitter account

Steps:

  1. Install R & RStudio
  2. Install Twitter Package (twitteR)
  3. Create a Twitter Application
  4. Create the R-Program to connect to twitter
  5. Schedule the R-Program to run on Task Manager

Download R and RStudio for Windows

In order to create a twitter bot you must first have the R-Programming language and R-studio installed.

1) Download R -Programming Language:
https://cran.r-project.org/bin/windows/base/

2) Download RStudio:
https://www.rstudio.com/products/rstudio/download/

Install the twitteR Package

You will need the twitteR package to be able to connect to twitter and use some special functions for twitter. Just type the following command in your R-Studio. If you do not know how to install packages you can watch this video: https://www.youtube.com/watch?v=_-DFNLm3JeQ

install.packages("twitteR")

twitteR: Provides an interface to the Twitter web API.

Create a Twitter Application

Sign in using your Twitter Account

Image for post
Image for post

You should’ve been directed to the twitter app. Now click “Create New App

Image for post
Image for post

Create an application

Image for post
Image for post

NOTE: If you haven’t already, then you may need to add your phone number to your twitter profile. Go to settings and privacy→ Mobile

Image for post
Image for post

Go to Keys and Access Tokens

Image for post
Image for post

Write down the Consumer key (API Key) and the Consumer secret (API Secret)

Image for post
Image for post

Create your access token by clicking Create my access token

Image for post
Image for post

Once the access token is created, write down the Access Token and the Access Token Secret

Image for post
Image for post

Create the R-Program to connect to twitter

#Install and load the appropriate packages
install.packages("twitteR")
library("twitteR")

#REPLACE '####' with the appropriate values from your twitter app
consumerKey='####'
consumerSecret='####'
accessToken='####'
accessTokenSecret= '####'

#Connect to twitter
setup_twitter_oauth(consumerKey,consumerSecret,accessToken,accessTokenSecret)

#Extract Tweets !
searchTwitter("nba", n=3, lang="en")

#Post Tweet !
tweet("A tweet!")

Schedule the R-Program to run on Task Manager

Automating the tweets

Let’s automate our R program. I am using a Windows Operating System, so we can use Windows Task Scheduler, to schedule and automate our R-Program.

First we must open the Task Scheduler. You can go to your start button and in the search type “Task Scheduler” to open the program.

Right click on Task Scheduler Library and then right click on Task Scheduler Library.

Next click create task and give your task a name:

Image for post
Image for post

Then, under the Triggers tab choose your scheduling for the tweet. For example, you can make it daily, weekly, or monthly.

Image for post
Image for post

Finally, under the Actions tab you need to locate the file Rscript.exe for your installation of R. This executable will be used to run your R program.
Mine was under “C:\R-3.1.2\bin\x64\Rscript.exe”. For Add Arguments choose the name of the R script that you want to run. In the Start in , put the directory where the script is located.

Image for post
Image for post

That’s it your DONE !

Thanks for reading this article I hope its helpful to you all ! Keep up the learning, and if you would like more algorithm analysis videos please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 )

Check Out the following for content / videos on Computer Science, Algorithm Analysis, Programming and Logic:

YouTube Channel:
randerson112358: https://www.youtube.com/channel/UCaV_0qp2NZd319K4_K8Z5SQ

compsci112358:
https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1CA

Website:
http://everythingcomputerscience.com/

Video Tutorials on Recurrence Relation:
https://www.udemy.com/recurrence-relation-made-easy/

Video Tutorial on Algorithm Analysis:
https://www.udemy.com/algorithm-analysis/

Twitter:
https://twitter.com/CsEverything

RESOURCES:

1) Sentiment Analysis Evaluate Neg and Pos words:
https://www.r-bloggers.com/twitter-sentiment-analysis-with-r/

2) Twitter Analytics Using R:
https://www.credera.com/blog/business-intelligence/twitter-analytics-using-r-part-1-extract-tweets/

3) Sentiment Analysis Bernie Sanders:
https://www.r-bloggers.com/how-to-use-r-to-scrape-tweets-super-tuesday-2016/

4) Getting Started with Twitter Analysis:
https://www.r-bloggers.com/getting-started-with-twitter-analysis-in-r/

Extra Social Media API packages

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store