Skip to content

Instantly share code, notes, and snippets.

@brampersandon
Last active January 13, 2016 11:03
Show Gist options
  • Save brampersandon/07dc1ce1f0ab54126da2 to your computer and use it in GitHub Desktop.
Save brampersandon/07dc1ce1f0ab54126da2 to your computer and use it in GitHub Desktop.
SOTU.rb: a quick method to tweet and text hot-takes simultaneously

SOTU.rb

My friend was stuck without an internet connection during this year's State of the Union. I wanted to make sure we could all share our hot takes and direct quotes with them, so I made this 'lil distribution channel in the ten minutes before the speech.

I wrote these docs, and this script, in a total of ten minutes. It may be a bit rough. I'll be making updates to simplify and clarify in a few minutes. If you have any questions, leave a comment on the Gist, or get at me on Twitter -- I'm glad to help!

APIs used

For SMS messaging, I used the awesome Twilio SMS APIs. To post to Twitter, I used the one, the only Twitter API.

To use

  1. Install the Twilio and Twitter gems. You may need to run these with sudo
      gem install twitter  
      gem install twilio-ruby  
    
  2. Set all API keys and config placeholders
  3. Run irb in your shell
  4. In irb, require_relative 'sotu'
  5. Pass strings to the Sotu::send function, and watch it tweet/text!

Why are you using a module?

Good question. The Sotu module groups together the two API clients, and the actions I want them to take. It's an example of scoping, which is particularly unique (in my estimation) in Ruby.

To read more about scoping and inheritance in Ruby, check out the excellent Learn Ruby the Hard Way book!

require 'twilio-ruby'
require 'twitter'
module Sotu
# Add your Twilio API keys here. Get them at http://twilio.com
@twilio = Twilio::REST::Client.new do |config|
config.account_sid = 'TWILIO_ACCOUNT_SID'
config.auth_token = 'TWILIO_AUTH_TOKEN'
# Add your Twitter API keys here. Get them at http://apps.twitter.com
@twitter = Twitter::REST::Client.new do |config|
config.consumer_key = "TWITTER_CONSUMER_KEY"
config.consumer_secret = "TWITTER_CONSUMER_SECRET"
config.access_token = "TWITTER_ACCESS_TOKEN"
config.access_token_secret = "TWITTER_ACCESS_TOKEN_SECRET"
end
def Sotu.send(msg)
# Send an SMS message from '+1TWILIONUMBER' to '+1DESTNUMBer' with the message as the body
@twilio.messages.create(
from: '+1TWILIONUMBER',
to: '+1DESTNUMBER',
body: msg.to_s
)
# Tweet the message, but with more hashtaggery.
@twitter.update(msg.to_s + " #SOTU")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment