I love making lists. I use them to organize my day, remember important things, and keep track of tools and libraries I want to explore. Lists like Awesome Java are a treat for people like me. Curated content on a technology I like and use everyday? Sign me in!
But the truth is: I am too lazy to actually read the list and select an entry I want to explore. Like many list makers, I end up with piles of lists (and even lists of lists!) that are just collecting dust. I believe these lists are missing one key ingredient to being really useful. I don’t want to look at them, I want their content to be pushed directly in front of me.
You probably see where this is going. Today we explore the creation of a Twitter bot that publishes daily content from the Awesome Java list. We’ll build the bot with our (also awesome) Xatkit chatbot development platform (fully open source!).
Xatkit for the bot behavior
Xatkit provides the two basic blocks we need to create such a bot:
– A Twitter platform to send tweets, post direct messages, and much more. This is a community platform developed and maintain by @nicoerlichman and @ffc91.
– A Cron event provider to produce events periodically.
With these elements we define a simple state machine to represent our bot behavior:
The bot has two states: a Wait state where it basically does nothing and waits for a CronTick event. A Tweet state where the bot retrieves the next entry of the list, formats it, and uses the Twitter platform to post it on twitter.
The code below shows how to define this state machine with the Xatkit DSL:
The tweetState body retrieves the index of the entry to tweet from the session, and uses the createTweetBody helper to get a String representation of the entry. The resulting tweet looks like the example below:
The configuration of the CronTick event period and Twitter credentials is done in the Xatkit property file:
Commonmark to parse the list
The bot creation part was quite straightforward in this case. In fact most of the work was about parsing the Awesome Java Markdown file and extract the information I wanted.
To do so I used commonmark-java. This library can render Markdown to HTML or plain text, and provides an easy-to-use parser to manipulate the Markdown AST.
Thanks to Andreas Kull‘s hard work the Awesome Java list is very well structured, and a simple visitor coupled with a few regular expressions were enough to extract the information I wanted to include in the tweets.
📚 I strongly recommend to take a look at iHateRegex if, like me, you have a complicated relationship with regular expressions.
Show me the bot!
The bot is live on Twitter, you can follow @daily_aswm_java if you want to receive daily news about cool java tools. The list has more than 500 entries, so this will be fresh for quite some time 😉 The complete code still needs some cleanup, but I will post it soon in our example repository.
Let us know in the comments if you are interested in a bot for a similar list. The Awesome list of lists is full of amazing lists that could have their own Twitter bot!