Automate a Twitter bot with the rtweet package and RStudio Connect

by Isabella Velásquez

When developing the promotion plan for the many exciting talks at rstudio::conf(2022), we thought of using the rtweet package by Michael W. Kearney to create a Twitter bot to automate Tweet announcements when a presentation was about to start. As it turned out, we did not end up using this process, but we thought it would be helpful to share how this can be done with rtweet v1.0.2 and RStudio Connect.

retweet allows users to use Twitter from R, and the function rtweet::post_tweet() allows users to share status updates on their Twitter feed without leaving RStudio (or the IDE of our choice). Once you have the Tweets you would like to post, the question is: how can you automate it so you don’t have to watch the clock anytime you want to make an announcement? One option is to use RStudio Connect, RStudio’s publishing platform which hosts data science content such as Shiny apps, Jupyter notebooks, APIs, etc. that is created in either R or Python. For this application, RStudio Connect’s ability to rerun R Markdown documents on a schedule is crucial.

The major steps in our workflow are:

  1. Create a spreadsheet with the talk schedule
  2. Clean table and automate Tweet creation
  3. Authenticate R and Twitter
  4. Schedule R Markdown documents on RStudio Connect

(Note the files are also available on GitHub).

Create a spreadsheet with the talk schedule

First, we create a spreadsheet with the relevant information on the talks. Here is an example using dummy data:

day start title speaker twitter_name image
July 27 10:00AM 30 tips to improve your datascience André Cardoso Azevedo @fake_twitter1 thumbnail.jpg
July 27 11:00AM 5 drinks to combine with data science Marie Wolf @fake_twitter2 thumbnail.jpg
July 27 11:30AM Data science techniques that changed my life forever Jasna Šahbaz @fake_twitter3 thumbnail.jpg
July 27 12:00PM 10 videos about data science that will make you laugh Dennis R. Everhart @fake_twitter4 thumbnail.jpg


Using this starting-off point:

  • We want to merge the day and start columns to create a date-time variable that specifies the time zone. This is important because we want to publish the Tweet in the time zone of the conference, which may differ from the time zone in which we’re creating this project!
  • We want to automate as much of the tweet-creation process as possible using R. This will help us avoid manual work if we want to add rows to our table later on.

Clean table and automate Tweet creation

Let’s begin with the following packages:

library(rtweet)
library(dplyr)
library(lubridate)

Now, we can load our data:

dat <-
  data.frame(
  stringsAsFactors = FALSE,
               day = c("July 27", "July 27", "July 27", "July 27"),
             start = c("10:00:00", "11:00:00", "11:30:00", "12:00:00"),
             title = c("30 tips to improve your datascience",
                       "5 drinks to combine with data science",
                       "Data science techniques that changed my life forever",
                       "10 videos about data science that will make you laugh"),
           speaker = c("André Cardoso Azevedo",
                       "Marie Wolf",
                       "Jasna Šahbaz",
                       "Dennis R. Everhart"),
      twitter_name = c("@fake_twitter1",
                       "@fake_twitter2",
                       "@fake_twitter3",
                       "@fake_twitter4"),
             image = c("thumbnail.jpg",
                       "thumbnail.jpg",
                       "thumbnail.jpg",
                       "thumbnail.jpg")
)

As mentioned above, we might be in a place with a different time zone than the conference location. We want to ensure that our Twitter bot posts status updates at the right time in the appropriate time zone.

Let’s specify the time zone in our date-time variable. To find out what it is called, we can see the complete list of time zone names in R using OlsonNames():

# Check timezone names
OlsonNames() %>%
  as_tibble() %>%
  dplyr::filter(stringr::str_detect(value, "US/")) # filtering for US-only
## # A tibble: 12 × 1
##    value            
##    <chr>            
##  1 US/Alaska        
##  2 US/Aleutian      
##  3 US/Arizona       
##  4 US/Central       
##  5 US/East-Indiana  
##  6 US/Eastern       
##  7 US/Hawaii        
##  8 US/Indiana-Starke
##  9 US/Michigan      
## 10 US/Mountain      
## 11 US/Pacific       
## 12 US/Samoa

We create an object s2 that prints the current time in our desired time zone:

s <- Sys.time()
s2 <- format(s, format = "%F %R %Z", tz = "US/Eastern")

s2
## [1] "2022-09-13 13:30 EDT"

Now, we create a new table cleaned_dat with the day and start columns merged into a single date-time object. We set the tz argument to use the appropriate time zone.

cleaned_dat <-
  dat %>%
  dplyr::mutate(date = lubridate::mdy_hms(paste0(day, ", 2022 ",
                                                 start, " EDT"),
                                          tz = "US/Eastern"))

Let’s add on to our mutate() pipeline to automate the Tweet message, which should read:

Happening now! [title]: [speaker] [twitter_name] 

Stream here: url goes here.

The \n creates a line break.

cleaned_dat <-
  cleaned_dat %>%
  dplyr::mutate(
    script =
      paste0(
        "Happening now! ",
        title,
        ": ",
        speaker,
        " ",
        twitter_name,
        "\n",
        "\n",
        "Stream here: ",
        "url goes here",
        "\n",
        "\n"
      ),
    .keep = "unused"
  )

This table contains all of the talks. Let’s create a table that filters to show only talks happening this minute.

filtered_dat <-
  cleaned_dat %>%
  filter(date(date) == date(s2) &
           hour(date) == hour(s2) & 
            minute(date) == minute(s2))

Authenticate R and Twitter

To use the API, all rtweet users must authenticate their Twitter accounts. The rtweet documentation has a comprehensive vignette on authentication. The high-level points are:

  • Apply for “Elevated” access to the API on the Twitter Developer Portal
  • Generate an “Access token and secret”
  • Save the API key, API secret key, access key, and access secret key in your R environment

Once we’ve finished these steps, we can authenticate a bot that takes action on our behalf. We reference the keys that we’ve saved in our R environment:

rbot_token <- rtweet::rtweet_bot(
  api_key = Sys.getenv("RBOT_TWITTER_API_KEY"),
  api_secret = Sys.getenv("RBOT_TWITTER_API_SECRET"),
  access_token = Sys.getenv("RBOT_TWITTER_ACCESS_KEY"),
  access_secret = Sys.getenv("RBOT_TWITTER_ACCESS_SECRET")
)

As our last step, we use rtweet::post_tweet() to post updates to our authenticated Twitter account. In addition to text, post_tweet() can also upload images to Twitter. We can specify them using the media argument.

We want this only to occur when there’s a talk starting at the current time. To accomplish this, we can create an if statement that checks to see if the table is empty. If it is not empty, then it posts the Tweet.

if (dim(filtered_dat)[1] > 0) {
  for (i in 1:dim(dat3)[1]) {
    rtweet::post_tweet(
      status = dat3$script[i],
      media = dat3$image[i],
      token = rbot_token
    )
  }
}

Schedule R Markdown documents on RStudio Connect

We can save the code above in a single R Markdown document and deploy it to RStudio Connect:

RStudio Connect deployment window allowing us to deploy an R Markdown document with source code On RStudio Connect, we can schedule an R Markdown document to rerun at specific intervals. For this example, we can set it to run every minute. The R Markdown document reruns, filters the table based on the time, and posts a status update if there is a talk happening in the current minute.

RStudio Connect deployed R Markdown document with scheduling options listed on the side. The document is set to rerun every 1 minute. With that, our Twitter bot is set up, authenticated, and has the information that it needs to post status updates based on our talk schedule.

Learn more

As mentioned above, there are various ways of automating a Twitter bot with R. RStudio Connect is a convenient choice as it deploys R Markdown documents and all associated files. It’s easy to set up and provides precise control over the schedule.

Another option is to use GitHub Actions:

Check out the GitHub repository for this blog post for an example of the R Markdown file. Happy Tweeting!

Share Comments · · · · ·

You may leave a comment below or discuss the post in the forum community.rstudio.com.