Sha256: c3d8f0ae5c705ca3a7b0dea596d2c68583c201ad2a1c84438e2e00e0168ecfc1

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# -*- coding: utf-8 -*-
require 'cinch'
require 'twitter'

module Cinch::Plugins
  class TwitterStatus
    include Cinch::Plugin

    self.help = 'Just link to a specific twitter status and I will post the content of that tweet.'

    listen_to :channel

    def initialize(*args)
      super
      if config
        Twitter.configure do |c|
          c.consumer_key =        config[:consumer_key]
          c.consumer_secret =     config[:consumer_secret]
          c.oauth_token =         config[:oauth_token]
          c.oauth_token_secret =  config[:oauth_secret]
        end
      end
    end

    def listen(m)
      urls = URI.extract(m.message, ["http", "https"])
      urls.each do |url|
        if url.match(/^https?:\/\/mobile|w{3}?\.?twitter\.com/)
          if tweet = url.match(/https?:\/\/mobile|w{3}?\.?twitter\.com\/?#?!?\/([^\/]+)\/statuse?s?\/(\d+)\/?/)
            status = Twitter.status(tweet[2]).text
            status.gsub!(/[\n]+/, " ") if status.match(/\n/)
            user = tweet[1]
            unless user.nil? || status.nil?
              m.reply "@#{user} tweeted \"#{status}\""
            end
          end
        end
      end
    rescue Twitter::Error::NotFound
      debug "User posted an invalid twitter status"
    rescue Twitter::Error::Forbidden
      debug "User attempted to post a Protected Tweet or you have not set valid Twitter credentials."
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-twitterstatus-1.0.0 lib/cinch/plugins/twitterstatus/twitterstatus.rb