public
Created — forked from harvesthq/cobot_twitter.rb

  • Download Gist
cobot.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require 'net/http'
require 'base64'
require 'cgi'
require 'json'
 
class CoBot
HEADERS = {
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => 'Harvest Twitter Script' }
 
def initialize(group_id, group_key)
@group_id = group_id
@group_key = group_key
@connection = Net::HTTP.new("coopapp.com", 80)
@connection.open_timeout = 10
end
 
def post(message, message_id)
message_id_pattern = /#{Regexp.escape(message_id)}$/
unless statuses.any?{|m| message_id_pattern =~ m}
status = "#{message} - #{key}"
@connection.post("/groups/#{@group_id}/notes", {:status => status, :key => @group_key}.to_json, HEADERS)
statuses << status
end
end
 
private
 
def statuses
@statuses ||= retrieve_statuses
end
 
def retrieve_statuses
response = @connection.get("/groups/#{@group_id}/users/cobot?key=#{@group_key}", HEADERS)
JSON.parse(response.body).map{|s| s['text']}
end
end
cobot_twitter.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env ruby
$KCODE = 'U'
 
require 'rubygems'
require 'twitter'
require 'cobot'
 
MAX_TWEETS = 3
 
cobot = CoBot.new(<group_id>, <group_secret>)
 
%w{mobalean kanjidamage keitai-dev}.each do |keyword|
Twitter::Search.new(keyword).per_page(MAX_TWEETS).each do |r|
cobot.post("@#{r['from_user']} mentioned #{keyword}: \"#{r['text']}\"",
"from: http://twitter.com/#{r['from_user']}/status/#{r['id']}")
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.