Logo

The Data Daily

Automate a Twitter bot with the rtweet package and RStudio Connect | R-bloggers

Automate a Twitter bot with the rtweet package and RStudio Connect | R-bloggers

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 v1.0.2 and RStudio Connect.

allows users to use Twitter from R, and the function 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:

(Note the files are also available on GitHub).

Let’s begin with the following packages: Now, we can load our data: dat %
as_tibble() %>%
dplyr::filter(stringr::str_detect(value, "US/")) # filtering for US-only
## # A tibble: 12 × 1
## value
## We create an object that prints the current time in our desired time zone: Now, we create a new table with the and columns merged into a single object. We set the argument to use the appropriate time zone. Let’s add on to our pipeline to automate the Tweet message, which should read: Happening now! [title]: [speaker] [twitter_name]

Stream here: url goes here. 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.

To use the API, all users must authenticate their Twitter accounts. The documentation has a comprehensive vignette on authentication. The high-level points are: Apply for “Elevated” access to the API on the Twitter Developer Portal 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: As our last step, we use to post updates to our authenticated Twitter account. In addition to text, can also upload images to Twitter. We can specify them using the argument. We want this only to occur when there’s a talk starting at the current time. To accomplish this, we can create an 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
)
}
}

We can save the code above in a single R Markdown document and deploy it to RStudio Connect: 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. 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.

Images Powered by Shutterstock