Skip to content

Instantly share code, notes, and snippets.

@elib
Last active December 24, 2015 00:19
Show Gist options
  • Save elib/6716469 to your computer and use it in GitHub Desktop.
Save elib/6716469 to your computer and use it in GitHub Desktop.
A simple bot that manually retweets haikus directed at it. Uses the Twitter gem. You will need to provide the appropriate API tokens.
require 'rubygems'
require 'twitter'
require 'time'
@blacklist = []
def log text
@logfile.write( "\r\n" + Time.new.to_s + ": " + text.to_s())
end
@logfile = File.new('haiku_log.txt', "a")
log("starting!")
regex_url = /((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))[\w\d:#%\/;$()~_?\+\-=\\\.&]*)/i
def get_last_response_time
last_time = nil
begin
f = File.new('haiku_last_response_time.txt', "r")
last_time = Time.parse(f.read)
f.close
rescue
#just don't choke
end
last_time
end
def set_last_response_time last_time
#no error handling because we want it to succeed
f = File.new('haiku_last_response_time.txt', "w")
f.write(last_time.to_s)
f.close
end
Twitter.configure do |config|
config.consumer_key = 'CONSUMER_KEY'
config.consumer_secret = 'CONSUMER_SECRET'
config.oauth_token = 'OAUTH_TOKEN'
config.oauth_token_secret = 'OAUTH_TOKEN_SECRET'
end
last_reply_response = get_last_response_time()
#puts "last response time: #{last_reply_response}"
#while true
#puts Time.new.to_s + ": Getting replies..."
begin
replies = Twitter.mentions
rescue Exception => err
puts err
log(err)
exit
end
#get relevant replies
replies.reverse!
replies.reject!{|reply| reply.created_at <= last_reply_response } if last_reply_response
#puts "...got #{replies.length} replies."
last_time = nil
replies.each do |reply|
shouldtweet = true
# log(reply)
#check if user is blacklisted.
if (@blacklist.member? reply.user.screen_name.downcase) then
shouldtweet = false
end
# check if tweet is a mention - not for retweeting
# use \A instead of ^ - some tricky bots use line breaks inside tweets.
# http://www.regular-expressions.info/anchors.html#az
if (reply.text.strip =~ /\A@haikutime/i).nil? then
shouldtweet = false
end
#If shouldn't tweet, skip the retweeting, but still mark this as the last response (so we don't have to deal with it again).
if (shouldtweet) then
#remove any mentions (including mention of "@haikutime"), remove any hashtags
log("about to tweet: " + reply.text)
replytext = "from @" + reply.user.screen_name + ":" + (reply.text.gsub(/@\w+/i, "")).gsub(/#\w+/i,"").gsub(regex_url, "")
log("cleaned tweet: " + replytext)
begin
Twitter.update(replytext)
rescue Exception => err2
puts err2
log(err2)
sleep 60
next
end
end
#puts "posted response: #{replytext} "
last_time = reply.created_at
end
if last_time
set_last_response_time(last_time)
last_reply_response = last_time
end
@logfile.close
@elib
Copy link
Author

elib commented Sep 26, 2013

I had an SSL issue on my computer, solved using:
http://stackoverflow.com/a/16134586/359362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment