Building your own Buffer App

Sat 01 February 2014

I tend to consume my daily reading list - consisting of new posts on blogs I follow, interesting articles forwarded by my friends and colleagues as well as those suggested by Zite - in bursts. And hence when it comes to sharing the stuff I've liked on Twitter, my posting habits follow a similar bursty pattern.

Of course, I'm not the only one with this problem as evidenced by how successful Buffer has been. In fact, I'm a regular user of the service myself, though I routinely reach the 10 post limit that is imposed on free accounts. While I'll likely convert to a paid customer in the near future, I thought it'd be a cool exercise to hack together a poor man's Buffer using tools and services already out there, patched together with a little bit of code. Here's the recipe -

Ingredients:

Recipe:

  1. Create a tweets folder inside the Public folder in your Dropbox
  2. Create an archived_tweets folder inside the main Dropbox
  3. Activate this IFTTT recipe - Post a Tweet via Email
  4. Activate this IFTTT recipe - Create Dropbox files via Email
  5. Using the Dropbox Linux agent to sync your Dropbox files to your VPS or shell account (you can selectively only sync the Public and the archived_tweets folders
  6. Copy the following Python code to a file called sendtweet.py on your VPS or shell account
import requests
import glob
import os
import sys

# Change the API endpoint, the API key and the email address below
def send_simple_message(subject, content):
    return requests.post(
            "https://api.mailgun.net/v2/your-api-endpoint/messages",
            auth=("api", "key-your-own-key-here"),
            data={"from": "your@emailaddress.com",
                  "to": "trigger@ifttt.com",
                  "subject": subject,
                  "text": content})

def find_tweet(dir):
    os.chdir(dir)
    for file in glob.glob("*.txt"):
        return file

def main(args):
    # Change the paths below to point to the Dropbox folder
    queue_dir = '/path/to/Dropbox/Public/tweets/'
    archive_dir = '/path/to/Dropbox/archived_tweets/'

    filename = find_tweet(queue_dir)

    if filename is None:
        print "Out of tweets to send!"
        sys.exit(0)

    fullpath = queue_dir + filename

    fo = open(fullpath, 'r')
    subject = fo.readline()
    content = '\n'.join(fo.readlines())
    fo.close()

    if subject != '' or content != '':
        print "Sending Tweet: " + subject + " - " + content
        subject = subject + " #send"
        send_simple_message(subject, content)

    os.rename(fullpath, archive_dir + filename)

if __name__ == "__main__":
    main(sys.argv)
  1. Set up cron job to execute the script above at specified times a few times a day. Here's my setup -
12 9,20 * * * /usr/bin/python /home/farhan/bin/sendtweet.py                                                                                                                                                                   
29 22 11,17 * * * /usr/bin/python /home/farhan/bin/sendtweet.py
30 40 13,15 * * * /usr/bin/python /home/farhan/bin/sendtweet.py

Congratulations! You've just built yourself a poor man's Buffer! Here's how you use it -

To post a tweet (say, an interesting article you just read), send an email to trigger@ifttt.com with the Subject and the Body containing the contents of the tweet (they are concatenated with the - character). The Subject line must contain the #queue token. For example -

Subject: This is the best Super Bown commercial I have ever seen! #queue
Body: http://www.youtube.com/watch?v=uQB7QRyF4p4

How does it work?

IFTTT, upon receiving this email, executes this recipe and creates a text file with the tweet's contents in the tweets folder.

Example of tweets waiting to be sent

On the next run of the sendtweet.py script, the code finds a text file in the folder, reads it and sends an email to trigger@ifttt.com but this time with the #send token in the Subject line. It uses Mailgun to send the email but that part can easily be replaced with any service that allows you to send emails.

This triggers the other recipe and IFTTT posts the tweet on your behalf.

Once the email is sent, the code moves the file to the archived_tweets folder.

To control the frequency with which the tweets go out as well as the times at which they are posted, just change the frequency at which the sendtweet.py script is executed.

Of course, the Buffer service offers a lot more than a way to queue your tweets and spread them out but I thought this was a cool little weekend hack to overcome the limits of the free account!