#!/usr/bin/env ruby require "rubygems" require "xmpp4r-simple" require "getoptlong" require 'json' require 'time' require 'net/http' require 'cgi' # A simple app that takes your last Twitters from the last # n minutes and posts them to Yammer optionally filtered based on a # keyword. It was built because I wanted to send my twitters to my # work Yammer stream automatically but wanted to be able to filter # them simply # # example: # ./twammer -t user:pass -j user:pass -f bbc # # Step though the above: # # 1. Twammer is run every 15 mins via cron # 2. I post a message to Twitter with #bbc at the end of the message # 3. Twammer checks for your last twitters in last 15 mins (default) # 4. Twammer filters the messages with #bbc at the end # 5. Twammer sends those messages onto Yammer with #bbc removed # # def options opts = GetoptLong.new( [ "--uptwitter", "-t", GetoptLong::REQUIRED_ARGUMENT ], [ "--upjabber", "-j", GetoptLong::REQUIRED_ARGUMENT ], [ "--delay", "-d", GetoptLong::OPTIONAL_ARGUMENT ], [ "--filter", "-f", GetoptLong::OPTIONAL_ARGUMENT ], [ "--help", "-h", GetoptLong::NO_ARGUMENT ], [ "--quiet", "-q", GetoptLong::NO_ARGUMENT ] ) def printusage(error_code) print "twammer -- Send your Twitters to Yammer\n" print "Usage: ./twammer -t STR:STR -j STR:STR [OPTIONS]\n" print "\n" print "Allowed options:\n" print " -t, --uptwitter STR Twitter user:pass\n" print " -j, --upjabber STR Jabber user:pass setup with Yammer IM\n" print " -d, --delay MINUTES How far to look back for tweets (default: 15mins)\n" print " -f, --filter STR Only send on messages with # in\n" print " -h, --help Shows this help\n" print " -q, --quiet Display, but don't actually send messages\n" print "\n" print "Examples:\n" print " ./twammer -t me@foo.com:mypass -j me@bar.com:mypass1 -f bbc\n" print " ./twammer -t me@foo.com:mypass -j me@bar.com:mypass1 -d 10 -q\n" print "\n" exit(error_code) end begin o = {} opts.each do |opt, arg| case opt when "--uptwitter" o[:tuser], o[:tpass] = arg.split(':') if o[:tuser].nil? or o[:tpass].nil? puts "Arg Missing --uptwitter required" exit 0 end when "--upjabber" o[:juser], o[:jpass] = arg.split(':') if o[:juser].nil? or o[:jpass].nil? puts "Arg Missing --upjabber required" exit 0 end when "--delay" o[:delay] = arg || 15 when "--filter" o[:filter] = arg || nil when "--help" printusage(0) when "--quiet" o[:quiet] = true end end return o rescue printusage(1) end end if __FILE__ == $0 opts = options response = nil yammer_bot = "yammer@chat.yammer.com" filter = "##{opts[:filter]}" if opts[:filter] delay_in_seconds = opts.has_key?(:delay) ? opts[:delay].to_i * 60 : (15*60) since = CGI.escape((Time.now - delay_in_seconds ).httpdate) # make the request to twitter for messages Net::HTTP.start('www.twitter.com') {|http| req = Net::HTTP::Get.new("/statuses/user_timeline.json?since=#{since}") req.basic_auth opts[:tuser], opts[:tpass] response = http.request(req) } case response when Net::HTTPSuccess # parse the returned data and stick in array messages = JSON.parse(response.body) # convert to array of messages messages.map! {|m| m["text"] } # filter if filter supplied if !opts[:filter].nil? or !messages.empty? messages = messages.map {|m| m.gsub!(/#{filter}$/, '') }.compact end # only carry on if there are messages unless messages.empty? # ignore messages that are replies i.e start with @ messages = messages.map {|m| m.strip unless m[0...1] == '@' } # now send them to Yammer im = Jabber::Simple.new(opts[:juser], opts[:jpass]) unless opts[:quiet] # loop through results and send for m in messages.compact # send message im.deliver(yammer_bot , m) unless opts[:quiet] puts "sent .. #{m}" # sleep for a few seconds, not really required, but means # that the tweets send get spread out a little sleep(5) end # disconnect im.disconnect unless opts[:quiet] end else response.error! end end