Auto-post to Twitter and Facebook from Rails in 10 minutes

My newest project, kind.fm, automatically creates awesome Spotify playlists from track data entered into Spinitron.

Recently, I wanted to add some social integration into kind.fm.  Whenever a new playlist was added, I wanted to auto-post the new playlist to Twitter and Facebook.

The Hard Way

I have done some social integrations with Rails before, so I started thinking about how to do this.  It went something like this:

– Register new Twitter developer app- Store Twitter developer app credentials in Rails
– Using omniauth, generate authorization URL with proper credentials to get authentication token for twitter
– Store authentication token credentials in Rails
– Integrate rails callback to use client API gem to tweet every time new playlist is added
– Rinse and repeat above steps for Facebook
– Ensure callback uses a background job so it doesn’t slow the rest of the app down, and can be retried.

If you don’t already have a background job system yet, the last step is significant, even with the introduction of ActiveJob. You still need to decide on an adapter (Sidekiq? DelayedJob? Resque?), and most adapters require adding additional technology to your stack (i.e. Redis).

The Fast Way (~10 minutes)

I wanted to get this done quickly, and found a creative way to get this going in about 10 minutes, as opposed to the few hours the ‘hard way’ would take.

You may be familiar with services like Zapier and IFTTT.  These services allow you to setup tasks based on actions, i.e. “Whenever I post an instagram photo, re-post on my blog”.  It turns out you can leverage these services to easily perform simple social integration, by setting up a RSS feed of the things you’d like posted to Twitter and Facebook.

Here’s what I did:

First, I created an “index” action for my playlists_controller that outputs a RSS feed of social messages for the last 30 playlists. Based on params[:social] (either ‘facebook’ or ‘twitter’), I decide what message to output for the RSS feed.

# app/controllers/playlists_controller.rb
class PlaylistsController < ApplicationController
  def index
    @playlists = Playlist.active.includes(:show).where(
      show_id: Show.active.pluck(:id)
    ).order('played_on DESC').limit(30)
  end
 
end
# app/views/playlists/index.rss.builder
xml.instruct! :xml, :version => '1.0'
xml.rss :version => '2.0' do
  xml.channel do
    xml.title 'kind.fm playlists'
    xml.description 'discover inspired music'
    xml.link 'http://kind.fm'
 
    @playlists.each do |playlist|
      xml.item do
        xml.title "#{playlist.show.name} | #{playlist.played_on_formatted}"
 
        # choose description based on social media platform
        # uses custom functions on models, they might be better
        # suited for a helper though
        description = if params[:social] == 'twitter'
          playlist.twitter_description
        else
          playlist.facebook_description
        end
        xml.description description
 
        xml.pubDate playlist.played_on.to_s(:rfc822)
        xml.link playlist_url(playlist)
        xml.guid "playlist-#{playlist.id}"
      end
    end
  end
end

Now, it’s time to integrate the RSS feed with Zapier. I chose Zapier over IFTTT because it was easier to test out my integration.  First, I linked kind.fm’s twitter account and facebook account with Zapier. Then I set up two tasks:

When a new item appears on RSS feed http://kind.fm/playlists.rss?social=twitter, post to kind.fm’s twitter account

When a new item appears on RSS feed http://kind.fm/playlists.rss?social=facebook, post to kind.fm’s facebook profile

That’s it.  When a new playlist is added, the RSS feed reflects this.  Zapier is polling the RSS feed every 15 minutes, and when a new item appears, it posts to Twitter and Facebook with a custom message.

Thoughts & Caveats

There are definitely some caveats to The Fast Way, using RSS feeds and Zapier:

– Zapier’s free edition only allows for 100 tasks per month (that’s task actions, so in my case, 100 new playlists).
– Zapier hits your Rails server every 15 minutes, adding additional load, but especially if you throw some simple caching on top of your RSS feed this should be very minimal.
– Zapier only checks the RSS feed every 15 minutes, so there will be a slight delay between a playlist being added and the social messages going through.
– Zapier branding. On Facebook, there will some text saying “Posted via Zapier” on each post, but it is pretty small.  Also, on twitter, your links will be shortened with the zapier shortener, so they will look like zpr.io/s7LZ

There are benefits to the “The Hard Way”, and you’re also setting yourself up for integration with users to sign up using Facebook or Twitter.  But, if you are looking for a quick and easy solution, and you don’t mind the caveats, posting to Facebook and Twitter using Zapier may be a great way to go.

Know of any other good ways to integrate social posting into Rails? Let me know in the comments.

Shameless plug: If use Spotify and are looking for some good music to code to, check out No Jay’s House Of Watts on kind.fm

Leave a Reply

Your email address will not be published. Required fields are marked *