Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building A Smart Twitter Bot with Apache NiFi

DZone's Guide to

Building A Smart Twitter Bot with Apache NiFi

This article gives a simple tutorial on how to build your first Twitter bot with ease using Apache NiFi. Read on to find out more.

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

Let's start with a few images.

This is the new processor, PutTwitterProcessor, it will use Twitter4J to send tweets from a parameter you set.

This is an example tweet and my reply to it. It's a regular tweet.

This is the result of a run. I put the tweet id and date-time stamp in the result attribute.

Another example:

You need to create a Twitter application and obtain the Consumer Key, Consumer Secret, Access Token, and Access secret. If you have a lat and long in your data, you can send that. It's optional. The message takes expression language and lets you build a message.

JUnit Test of My Processor

JUnit Run Results

Most of the code is using the very easy to use Twitter4J library.

So, what can I do with this? In my flow, I am ingesting Tweets, and with a streaming SQL query, I can retweet ones where there is no existing retweet, and the followers of the tweet are more than 1,000. I can also feed all of this information to a Hive table and then run some machine learning on it to figure out other parameters to filter on for my bot.

This is a start. I put a scheduler on my PutTweet to only tweet every 10 minutes (600 seconds). Let's see how this goes.

SQL for QueryRecord

SELECT * FROM FLOWFILE WHERE CAST(retweet_count AS DOUBLE) <= 0 AND CAST(followers_count AS DOUBLE) > 1000



Example Message Building

${'user_name':append( ' '):append( ${'location'} ):append(' '):append(${'hashtags'} ):append(' ' ):append(${'msg'})}


Download NAR to Install in Apache NiFi lib Directory and Then Restart:

https://github.com/tspannhw/nifi-puttwitter-processor/releases/tag/1.0

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
apache nifi ,twitter ,social media ,bots ,sql ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}