How to program a Twitter bot

I have made many “Tweet bots” over the last year, but I’m just now getting around to sharing.

One of the simplest ones I have created is the “current conditions” auto-tweet. It  simply scrapes the weather observation data from the NWS website and then tweets it out.

I have the R script set up to run in batch mode a couple of times a day (times specified by me in my Task Scheduler).



You must first create an app in Twitter via the Twitter Developer Website.

You must then obtain a consumer key and secret. Here is a good how-to.

Play around with the R Twitter package (twitteR) and familiarize yourself with its functions.

I use the httr package for Twitter Authentication.

###This script will scrape the latest conditions from the NWS for KPBI and
###tweet out the current conditions including an image of sky cover.

###Have to first go through all the twitter stuff:

#detach("package:Rfacebook", unload=TRUE)

# returns string w/o leading or trailing whitespace
trim % html() %>% html_nodes(xpath = "/html/body/table") %>% html_table(fill = TRUE)

data %
  html_nodes("#current-conditions-body img") %>%

link = paste("", image_url, sep = '')
download.file(link, destfile = "C:/Users/Lisa/Desktop/currents/currents.png",
              mode = "wb")

#Set some conditions for the text.
if(current.temp >= 90 & current.dp > 69){a</pre>
Once you've got this done, you'll need to set up a batch file. I do this by creating a new R script (within Rstudio) and typing the following:
"C:\Program Files\R\R-3.1.2\bin\x64\R.exe" CMD BATCH "C:\Users\Lisa\Desktop\currents\currents.r"

Then, save this file as a '.bat' file instead of a '.r' file - this is very simple to do in Rstudio. (I don't know about the R console...)

It is this '.bat' file that you will want to trigger using your computer's task scheduler.

3 thoughts on “How to program a Twitter bot

Add yours

  1. How can I keep this bot alive 24 X 7. The problem with task scheduler is that it runs only when my system is on and not otherwise. How can I keep it online along with some time scheduler?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: