A Twitter Bot in Groovy Script - Part 1

For the last two years I’ve been working with the Twython package to build my (R2D2) Twitter Bot. It’s been successful and I’ve learned how to hack Python better than I ever did before.

Now I’m setting my sites on building a Twitter Bot using Groovy Script. Why Groovy Script? Groovy is a lot like Python in the sense that it’s a dynamic programming language. It can be used everywhere, easily, because of the JVM. Plus, I always wanted to teach myself Java so I thought this is a good way to get started.

Of course you can argue that I should just jump into Java but this feels more fun right now. I always learn better if I have a fun little project to work on, so I want to rebuild my R2D2 Bot in Groovy.

##Hello World!

Everyone starts with Hello World” and I did too. The simple program looks different and a bit more complex than python.

In Python it’s:

print "R2D2 says hi!"

In Groovy Script it’s way more complex. I have to create an R2D2 class, tell the class that it will be using strings, and then print my hello world-ish” message.

class R2D2 {
    static void main (String[] args){
        print ("R2D2 says hi!");

##Posting a Tweet

After I finished that introductory task, I then started to scour the Internet for a Twython like Java package. I discovered Twitter4j.

At first glance it reminded me of the Twython package but it seemed a lot harder - at first. It wasn’t until I started poking through the example code and questions on Stackflow that I found a script I could modify. Note: This is not my script 100%, I’ll give attribution to the author when I figure out where I got it from again. The closest post I got is this one.

The first thing I needed to do was download the Twitter4j package and I discovered a handy way to do just that. I just added

@Grab(group="org.twitter4j", module="twitter4j-core", version="4.0.4")

to the top of my script and everytime it runs it will download the twitter4j-core JAR. Probably not a good thing to do everytime but I’m just learned how to do this. In the future I’ll make sure that it’s already downloaded and properly referenced in the script.

Next I had to import the methods I was going to use. The methods are .Status,” .Twitter,”, .TwitterFactory,” etc. This is just the same as it is in Python when I want to import only select modules into the script. Note: a key one is the .ConfigurationBuilder” which is what you need to do the Twitter OAuth stuff.

import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

The orignal author of this script then creates a class called TweetMain. He tells the class that he’s going use some strings and assign them to the ConfigurationBuilder method. Those strings happen to be ConsumerKey, ConsumerSecret, OAuthAccessToken, and OAuthAccessSecret, also known as your Twitter keys. All those keys are needed to auto post to Twitter and you can get this info from apps.twitter.com.

What happens next is opening a connection to Twitter by using the TwitterFactory method and passing the keys to Twitter. Once the instance is established then you can use the .Status” method to post your tweet and then print out to your console that the status was successfully updated.

The Final Script

The final script looks like this:

@Grab(group="org.twitter4j", module="twitter4j-core", version="4.0.4")

import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TweetMain {
    public static void main(String[] args) {
        ConfigurationBuilder cb = new ConfigurationBuilder();

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();     
        Status status;
        try {
            status = twitter.updateStatus("R2D2 loves #GroovyScript");
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
        } catch (TwitterException e) {
            // TODO Auto-generated catch block

You’ll have to replace the XXXXs” with your own Twitter keys but this test script works.

The next task is to have Groovy open a text file, randomly parse a line and post that as a Tweet. That’ll be Part 2 when I figure out how to do it.

Up next Hacking Meta Data with Blot.Im - Part 1 August 2016 Blog Income Report
Latest posts The Ye Old Blog List Motorola: Then and Now EWM Redux Testing for mean reversion with Python & developing simple VIX system - Talaikis unsorted - Tadas Talaikis Blog Steps to calculate centroids in cluster using K-means clustering algorithm - Data Science Central Basics of Statistical Mean Reversion Testing - QuantStart Algorithmic trading in less than 100 lines of Python code - O’Reilly Media Interpreting Machine Learning Models Microsoft the AI Powerhouse Investing in the S&P500 still beats AI Trading Microsoft makes a push to simplify machine learning | TechCrunch 10 Great Articles On Python Development — Hacker Noon Introduction to Keras Democratising Machine learning with H2O — Towards Data Science Getting started with Python datatable | Kaggle Phone Addiction Version 12 Launches Today! Machine Learning Making Pesto Tastier 5 Dangerous Things You Should Let Your Kids Do The Pyschology of Writing Investing in 2019 and beyond TensorFlow and High Level APIs Driving Marketing Performance with H2O Driverless AI Machine Learning and Data Munging in H2O Driverless AI with datatable Making AI Happen Without Getting Fired Latest Musings from a Traveling Sales Engineer The Night before H2O World 2019 Why Forex Trading is Frustrating Functional Programming in Python Automatic Feature Engineering with Driverless AI Ray Dalio's Pure Alpha Fund