How To Make a Scary Russian Twitter Bot With Java

Using Twitter4J To Make Twitter API Requests

In Mother Russia, bot think for you.

Apparently, I’m a Russian bot. This morning, at least, I was told to submit a phone number to Twitter to verify my identity because they claimed to have “detected suspicious activity” coming from my account.

In Tweet-speak, “suspicious activity” means simply to demonstrate a conservative political opinion. So I did what any sane software developer would do. I used a VPN to fabricate a fake phone number over Skype that I could use for SMS verification purposes…

Russian bots are clearly responsible for everything from nuclear power plant meltdowns to having you spill your coffee on your way to work in the morning. Obviously, then, these superpowers ought not be reserved for just the lucky few. In the spirit of fairness, I’ll show you how you too can make a Scary Russian Twitter Bot... in five easy steps to boot!

You don’t have to be a programmer to do this. Just follow my instructions closely and you should be able to get this going in no time.

Step 1 — Make a Twitter Application

The first step is to make a Twitter Application with the user account that you want to perform the API request with.

Click “Create New App” and fill in the fields. You can put whatever down you like. It really doesn’t matter.

Now that you have an “App”, go to the Keys and Access Tokens tab. Your App will have a Consumer Key and Consumer Secret. Take note of these.

Scroll down a bit and generate a new Access Token.

Take note of the Access Token and Access Token Secret.

Step 2— Create a Maven Project in IntelliJ

I like to use IntelliJ Community Edition for Java projects. Eclipse is a little outdated in my opinion, so make sure to download IntelliJ if you would like to follow along without a hitch.

The first thing we’ll want to do is create a Maven Project, which will allow us to include Twitter4J, the one dependency we need to get this running.

After opening IntelliJ, click File > New > Project… > Maven > Next:

Set the GroupId to com.blockchainmarty and the ArtifactId to TwitterAutoBot for consistency. They can be whatever you want but you’ll have to make sure they’re consistent throughout the program so just copy me for now.

Go to your pom.xml file and replace its contents with this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>com.blockchainmarty</groupId>
<artifactId>TwitterAutoBot</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[3.0,)</version>
</dependency>
</dependencies>

</project>

Nazdravlje! We have now set up Twitter4J with our project. Click Import Changes when the prompt pops up and wait for Maven to import the new dependency:

Step 3 — Prepare Tweets and Configure Twitter4J

In your resources directory, create a new File. Name it tweets.txt:

The way we’re going to set up this bot is to have it tweet line by line from this file until there are no more lines. The more unique your content, the less likely tweets will fail as a result of duplicate content detection. I suggest writing at least 100 unique tweets to get going. Of course, each line should be less than Twitter’s limit of 280 characters each.

Read this article for inspiration if you’re looking to get banned as quickly as possible. Here are a few examples:

I love Donald Trump #MAGA
Guns don't kill people. People kill people. #<3NRA
Any other conservative opinion.

In the same directory, create a new file called twitter4j.properties. You will need to fill in the following information from Step 1 of this tutorial:

debug=true
oauth.consumerKey=
oauth.consumerSecret=
oauth.accessToken=
oauth.accessTokenSecret=

Now that we’re authenticated with our Twitter application, we can move ahead to the code.

Step 4 — The Source Code

Create a new Java Class in the src/main/java directory of your project. Make sure to name it TwitterAutoBot:

This file will contain all the code we need to blast out our deliciously offensive tweets. Paste the following, replacing whatever is already there:

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

import java.io.*;
import java.nio.charset.Charset;

public class TwitterAutoBot {

public static void main(String[] args) {
tweetLines();
}

private static void tweetLines() {
String line;
try {
try (
InputStream fis = new FileInputStream("C:\\Users\\marty\\Documents\\TwitterAutoBot\\src\\main\\resources\\tweets.txt");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("Cp1252"));
BufferedReader br = new BufferedReader(isr);
) {
while ((line = br.readLine()) != null) {
// Deal with the line
sendTweet
(line);
System.out.println("Tweeting: " + line + "...");

try {
System.out.println("Sleeping for 30 minutes...");
Thread.sleep(1800000); // every 30 minutes
// Thread.sleep(10000); // every 10 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
} catch (IOException e) {
e.printStackTrace();
}

}

private static void sendTweet(String line) {
Twitter twitter = TwitterFactory.getSingleton();
Status status;
try {
status = twitter.updateStatus(line);
System.out.println(status);
} catch (TwitterException e) {;
e.printStackTrace();
}
}

}

Make sure to change the file path within the FileInputStream function to match the actual location of the tweets.txt file on your computer. This will definitely be different than what is shown above. Unless you’re using my computer, you Russian hacker! In which case, trakhat’ tebya!

Thread.sleep(x), where x is the number of minutes between tweets in milliseconds is probably the only thing you’ll want to customize. By default, I’ve set it to tweet once every 30 minutes. You can probably get away with once every minute if you like.

You’ll also want to go ahead and read the Twitter API Limits to familiarize yourself with how often you can tweet without getting bulldozed by your new sentient AI overlords. As we know, Twitter’s Trust & Safety Council is dark and full of errors…

Step 5 — Running The Program

From the top-right corner of the screen, click Edit Configurations…

Next, press the green plus sign, and select Application from the Add New Configuration menu:

Click Main class under the Configuration tab, then select TwitterAutoBot.

The last step is to click the green play button in the top-right corner of your screen:

If it worked, you’ll see something like the following in your debug terminal at the bottom of the screen:

There are obviously more sophisticated ways to do this, such as running the program as a cloud function that doesn’t require IntelliJ and your laptop to be always on, but that’s about all the time I’ve got for now. Please comment if you have any issues, and I’ll try my best to help out. Three cheers for prrrropaganda!

Martin is Lead Project Manager at Selotec. You should follow him on Twitter.