Skip to content

Instantly share code, notes, and snippets.

@mseymour
Created December 28, 2011 17:59
Show Gist options
  • Save mseymour/1528899 to your computer and use it in GitHub Desktop.
Save mseymour/1528899 to your computer and use it in GitHub Desktop.
Twitter plugin v6 for Cinch (>=1.2.0)
Observations from below: nil gets sent, so that overrides any defaults.
Search should be switched over to named params.
<@Azure> !tw
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>nil, :nth_tweet=>nil}
<@Azure> !tw azuretan
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>nil}
<@Azure> !tw azuretan+10
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>"10"}
<@Azure> @
<@Azure> @azuretan
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>nil}
<@Azure> @azuretan+10
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>"10"}
<@Azure> !tw #100001
<@Aiko_> TWITTER: tweet_by_id - params: {:id=>"100001"}
<@Azure> @#100001
<@Aiko_> TWITTER: tweet_by_id - params: {:id=>"100001"}
<@Azure> ?tw azuretan
<@Aiko_> TWITTER: tweep_info - params: {:username=>"azuretan"}
<@Azure> ?ts #SUPERDRAGON
<@Aiko_> TWITTER: search_by_term - params: {:term=>{:term=>"#SUPERDRAGON"}}
<@Azure> http://twitter.com/
<@Azure> http://twitter.com/azuretan
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>:first}
<@Azure> something else http://twitter.com/azuretan kkk
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>:first}
<@Azure> something else http://twitter.com/azuretan/status/ kkk
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>:first}
<@Azure> something else http://twitter.com/azuretan/status/10001 kkk
<@Aiko_> TWITTER: tweet_by_id - params: {:id=>"10001"}
Observations from below: nil gets sent, so that overrides any defaults.
Search should be switched over to named params.
<@Azure> !tw
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>nil, :nth_tweet=>nil}
<@Azure> !tw azuretan
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>nil}
<@Azure> !tw azuretan+10
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>"10"}
<@Azure> @
<@Azure> @azuretan
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>nil}
<@Azure> @azuretan+10
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>"10"}
<@Azure> !tw #100001
<@Aiko_> TWITTER: tweet_by_id - params: {:id=>"100001"}
<@Azure> @#100001
<@Aiko_> TWITTER: tweet_by_id - params: {:id=>"100001"}
<@Azure> ?tw azuretan
<@Aiko_> TWITTER: tweep_info - params: {:username=>"azuretan"}
<@Azure> ?ts #SUPERDRAGON
<@Aiko_> TWITTER: search_by_term - params: {:term=>{:term=>"#SUPERDRAGON"}}
<@Azure> http://twitter.com/
<@Azure> http://twitter.com/azuretan
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>:first}
<@Azure> something else http://twitter.com/azuretan kkk
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>:first}
<@Azure> something else http://twitter.com/azuretan/status/ kkk
<@Aiko_> TWITTER: tweet_by_username - params: {:username=>"azuretan", :nth_tweet=>:first}
<@Azure> something else http://twitter.com/azuretan/status/10001 kkk
<@Aiko_> TWITTER: tweet_by_id - params: {:id=>"10001"}
module Plugins
module Twitter
module Error
class Notices < StandardError
class Protected < Notices; end
class No_Tweets < Notices; end
end
def HandleError ex, username, bot_nick
params = {
username: username,
bot_nick: bot_nick
}
exceptions = {
::Twitter::Error::BadRequest => "Bad request! (rate limit exceeded? Warn Azure when this occurs.)",
::Twitter::Error::Unauthorized => "%<username>s's account seems to be protected.",
::Twitter::Error::Forbidden => "Suspended!",
::Twitter::Error::NotFound => "The account \"%<username>s\" seems to be not found!",
::Twitter::Error::NotAcceptable => "An invalid format is specified in the search request.",
::Twitter::Error::EnhanceYourCalm => "Enhance your calm. %<bot_nick>s is being rate limited.",
::Twitter::Error::InternalServerError => "Something seems to be broken! Please try again in a minute.",
::Twitter::Error::BadGateway => "Twitter seems to be down, or is being upgraded. Please try again in a minute.",
::Twitter::Error::ServiceUnavailable => "Twitter is currently under heavy load. Please try again in a few minutes, and hopefully it'll clear up.",
Notices::Protected => "%<username>s's tweets are protected!",
Notices::No_Tweets => "%<username>s is lame for creating an account and not tweeting yet!" }
puts "Exception: #{ex.inspect}"
exceptions[ex.class] % params;
end
end
end
end
gem 'twitter', '~>2.0.0'
require 'twitter'
require_relative 'twitter/error'
module Plugins
module Twitter
class Client
include Cinch::Plugin
include Error
EXCEPTIONS = [::Twitter::Error, Notices]
# Handler methods
def tweet_by_username params={}
params = { username: "Twitter", nth_tweet: :first }.merge(params)
begin
"TWITTER: tweet_by_username - params: #{params.to_s}"
rescue *EXCEPTIONS => ex
HandleError ex, params[:username], @bot.nick
end
end
def tweet_by_id params={}
params = {id: 0 }.merge(params)
begin
"TWITTER: tweet_by_id - params: #{params.to_s}"
rescue *EXCEPTIONS => ex
HandleError ex, params[:username], @bot.nick
end
end
def tweep_info params={}
params = {username: "Twitter"}.merge(params)
begin
"TWITTER: tweep_info - params: #{params.to_s}"
rescue *EXCEPTIONS => ex
HandleError ex, params[:username], @bot.nick
end
end
def search_by_term params={}
params = {term: "cat"}.merge(params)
begin
"TWITTER: search_by_term - params: #{params.to_s}"
rescue *EXCEPTIONS => ex
HandleError ex, params[:username], @bot.nick
end
end
# Matchers
match /tw$/, method: :execute_tweet
match /tw (\w+)(?:\+(\d+))?$/, method: :execute_tweet
match /^@(\w+)(?:\+(\d+))?$/, method: :execute_tweet, use_prefix: false
def execute_tweet m, username = nil, nth_tweet = nil
options = {}
options[:username] = username unless username.nil?
options[:nth_tweet] = nth_tweet unless nth_tweet.nil?
m.reply tweet_by_username(options)
end
match /tw #(\d+)$/, method: :execute_id
match /^@#(\d+)$/, method: :execute_id, use_prefix: false
def execute_id m, id
m.reply tweet_by_id(id: id)
end
match /\?tw (\w+)$/, method: :execute_info, use_prefix: false
def execute_info m, username
m.reply tweep_info(username: username)
end
match /\?ts (.+)$/, method: :execute_search, use_prefix: false
def execute_search m, term
m.reply search_by_term(term: term)
end
listen_to :channel, method: :listen_channel
def listen_channel m
urlregexp = /https?:\/\/twitter.com\/(?:#!\/)?(\w+)(?:\/status\/(\d+))?/i
return unless m.message =~ urlregexp
username, id = m.message.match(urlregexp)[1..2]
if id.nil?
m.reply tweet_by_username(username: username)
else
m.reply tweet_by_id(id: id)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment