Build the future of communications.
Start building for free

Building the Good News SMS bot with Crocodile Dundee, Reddit and Ruby

robot-elliott-sq

Is it just me, or has 2016 been an extra rough year? It seemed like everyday on Facebook or Twitter there would be another piece of horrible news. Off the top of my head this year brought:

  • The Syrian Refugee crisis
  • Orlando shooting
  • RIP Prince
  • RIP Alan Rickman
  • RIP Gene Wilder
  • Harambe
  • RIP Parse

And personally I lost my Grandpa this year, who aside from reinventing himself in the last years of his life, was one of the best PHP developers I’ve ever met. Needless to say this year was full of bad news. It was this feeling and some help from my son, Reddit, and Crocodile Dundee that lead to me building the Good News bot.  It all started with a robot costume….

Elliott the Robot

My son Elliott is a two year-old and luckily he doesn’t read the news. He does however, have strong opinions about what he wants to be for Halloween, and this year he wanted to be a robot. So about a month ago I sat down to work on his costume (see above picture) and decided to put on a movie in the background. All of the suggestions were thrillers and horror flicks, but I felt like I could use a little pick-me-up… so I was thrilled to see Crocodile Dundee on Hulu.

For the first time in a while I was carefree and news free. Just me, some duct tape and Dundee. It was right about the time Dundee whips out a small machete that a fairly simple idea struck me. I should build a Twilio-powered SMS app that lets me get a dash of Dundee pick-me-up whenever I need it… just a simple app that delivers some good news whenever I ask it to. A couple hours later I had prototyped the server-side for the Good News bot.

Reddit to the rescue

Pretty immediately after deciding to pause robot construction, I had a fresh git repo with some basic Twilio stuff. But the main challenge of building this app was going to be finding a source for good news. I thought about using the Times’ or some other new source API and doing some filtering, but decided that would probably lead to some false positives (pun intended). It was then I remembered stumbling across HumansBeingBros on Reddit. I had never used the Reddit API and decided to give it a try. Luckily for me, it is a very simple interface and Sam Symon’s Ruby wrapper was well documented.

Bits of the bot

Here is the bit of code that grabs the good news from Reddit:

@reddit_client = RedditKit::Client.new ENV['REDDIT_USERNAME'], ENV['REDDIT_PW']

    good_news = @reddit_client.links 'UpliftingNews', :category => :new, :time => :all, :limit => 100
    parse_results(good_news)

The next bit stores the results in a local database:

  def parse_results(list)
    list.each do |post|
      exists = Post.get(post.id)
      img = post.image_link? ? post.url : post.thumbnail

      if post.over_18 == false && exists.nil?
        Post.create!(
          :id => post.id, 
          :thumbnail => img,
          :title => post.title,
          :permalink => post.permalink,
          :url => post.url)
      end
    end
  end

Now I have a way to poll the Reddit API and store a local copy so that I can more quickly respond with good news when anyone texts the number.

Finally when someone texts the number we grab the first post that they haven’t read yet and return a bit of TwiML that will send the message. This is the url we would configure our Twilio phone number to point to when texted:

  route :get, :post, '/pull-news' do 
    phone_number = params[:From]
    user = VerifiedUser.first(:phone_number => phone_number)

    # Get the first post that the user hasn't seen
    post = getTheNews(phone_number)

    Twilio::TwiML::Response.new do |r|
      r.Message do |m|
        m.Body "#{post.title} - #{post.url}"
        m.Media "#{post.thumbnail}"
      end
    end.text
  end

You can see the full code here: https://github.com/jarodreyes/good-news-bot

That’s pretty much all I’ve got on this little bot. But if you have any ideas on how to make it better please drop me an email or reach out on twitter.

As the holidays approach, I hope you have enough good news to get you through to 2017. But if not here are some other sources of good news:

Authors
Sign up and start building
Not ready yet? Talk to an expert.