I Had A Dream Bot
Coming up with an idea for a bot is easy! Choosing one to make is hard. I pitched a dozen concepts to friends and family before setting on one that felt right. After much hemming and hawing hearing my husband mention his co-worker Adream triggered it. A dream! That was it! I have always had an interest in dreams and certainly Twitter would be full of tweets mentioning the relatable and bizarre dreams that everyone has.
So what does the bot do? It essentially acts as a dream aggregator. I’ll break it down into two parts: the code and running on a Raspberry Pi.
The Code: The basic behavior for the bot is once an hour to search for ten tweets that contain the words “I had a dream”. From there it filters through the tweets, removing any text before “I had a dream”, discarding any tweets with profanity, removing any @’s, discarding any tweets 110 characters or longer, and from whatever is left posting the first to meet all of the criteria.
Running in node.js I made use of two node packages, Twit and Bad-words. Twit made searching for tweets easy using the following code:
// search twitter for all tweets containing the phrase "I had a dream" T.get('search/tweets', { q: '"I had a dream"', count: 10, since_id: id}, function(err, data, response) { // do something with data }
From there I iterated through the returned JSON object with a for loop. My initial tests returned quite a few hits with profanity which is where the Bad-words package comes in. Using Bad-words I discarded any tweets containing profane language:
// filter out profane tweets if(!filter.isProfane(tweetText)) { // do something with tweets that pass filter }
With the code working the next question was where to run it?
Raspberry Pi: Fortunately a few years back I purchased a Raspberry Pi 3b that was still sitting in a drawer waiting for a purpose. Getting it up and running was pretty straightforward following the documentation from the Raspberry Pi organization. Mine was running an OS two versions old so I downloaded a fresh install using the Raspberry Pi Imager to update the OS. From there I was able to tunnel in with SSH via command line and screen share with VNC. Installing node and other packages used the same process as terminal in OS X.
Where I did run into a challenge was figuring out how to transfer files to the Pi. By default the Pi didn’t appear as an SMB server on my Mac so a little digging lead me to this resource for setting up a Samba Server on a Pi.
This whole process went surprisingly well and now I have my own little Twitter bot humming away doing a new post every hour.
You can follow I Had A Dream on Twitter at @HadzDream.