How to Build a Twitter-Bot

Reading time ~3 minutes

How to Build a Twitter-Bot

How to Build a Twitter-Bot

There is a command-line twitter client that can be used to send tweets to a certain account from terminal. Combining it with cron and other linux tools I have built a twitter-bot that sends proverbs periodically to twitter ( I will describe here how I did it.

1 Accessing twitter from the terminal

There is a single-letter tool called "t", which is a command-line Twitter client written in Ruby. I installed and configured it as described in this tutorial:

The installation is simple:

apt-get install ruby-dev
gem install t

Then create a new app on twitter: Follow the detailed instructions and the screenshots on the tutorial above.

The next step is to authorize your app to access the Twitter account:

useradd --system --create-home twitter
su - twitter
t authorize

The account access info will be stored in ~/.trc as a plain text.

Now we can use it like this

$ t account

$ t whois @dashohoxha
ID           50053789
Since        Jun 23  2009 (8 years ago)
Last update  20+ Free eBooks To Learn Linux For Free (a month ago)
Screen name  @dashohoxha
Name         Dashamir Hoxha
Tweets       1,403
Favorites    21
Listed       5
Following    50
Followers    141

$ t stream timeline

$ t update "Hello there"

$ t follow @dashohoxha @linuxfoundation

$ t mentions -n 10

$ t search timeline "linux"

For more complex usage examples, refer to the official documentation.

2 Getting random proverbs with 'fortune'

There is a nice little command, called fortune, which can get and print a random citation from a file of citations.

apt-get install fortune

The data files from which it gets the random citations are on /usr/share/games/fortunes/. The ones with the extension .dat are binary files (which are used by the command), and those without this extension are text files. The format of these text files is very simple: Quotations are separated from each-other by a line that contains a % sign. It is very easy to create a file that contains your own list of preferred proverbs or citations. Then convert it to the binary format with the command:

strfile -c % yourlist yourlist.dat

Now, to get a random item from your list you can run:

fortune yourlist

3 Twitting random items from your lists

I use a small script in order to tweet random sayings from my preferred lists:

tweet=$($fortune -s -n 140 25% english 75% shqip)
$t update "$tweet"

The main command here is fortune -s -n 140 25% english 75% shqip. The options -s -n 140 makes sure that only short proverbs of up to 140 chars are returned, which are suitable for being twitted. The arguments 25% english 75% shqip ask the command to return something from the files in the directory /usr/share/games/fortunes/english/ with a probability of 25%, and something from the files in the directory /usr/share/games/fortunes/shqip/ with a probability of 75% (in the first directory I have collected some English proverbs, and on the second one some Albanian proverbs). The last step of the script is to send this random proverb to twitter, with the command =t update "tweet"=.

4 Making it automatic

The last thing is to run the script above periodically and automatically. This is a job for the good old cron. I have created the file /etc/cron.d/fortune which has these lines:

### first create a user with `adduser twitter`
0 */5 * * *  twitter  /home/twitter/ > /dev/null 2>&1

### uncomment this line only for debugging
#*/5 * * * *  twitter  /home/twitter/

It runs the script each 5 hours, sending posts to twitter automatically.

5 Twitting translations from

Another script very similar to the previous one is this:

tweet=$(curl -k $base_url/btr/tweet)
mention=$( ( $t followings ; $t followers ) | uniq | sort -R | tail -1)
$t update "$tweet @$mention"

This one gets a random translation from, with the command:

curl -k

Then it appends to it the name of a random friend and sends it to twitter.

Date: 2017-03-24

Author: Dashamir Hoxha

Created: 2019-01-24 Thu 05:13

Emacs 25.1.1 (Org mode 8.2.10)


OpenPGP Web Key Directory

OpenPGP Web Key DirectoryOpenPGP Web Key DirectoryTable of Contents1. Introduction2. How WKD works3. Building a WKD3.1. Create the direct...… Continue reading

SMTP Server with LDAP Authentication

Published on April 17, 2021

Using WireGuard VPN

Published on November 09, 2020