wesabe / wesabot

Wesabe's Campfire bot framework

wesabot / campfire / polling_bot / plugins / tweet_plugin.rb
100644 99 lines (91 sloc) 2.988 kb
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# plugin to send a tweet to a Twitter account
class TweetPlugin < Campfire::PollingBot::Plugin
  accepts :text_message, :addressed_to_me => true
 
  def process(message)
    case message.command
    when /^(?:tweet|twitter):?\s*("?)(.*?)\1$/i
      begin
        text = strip_links($2)
        post_tweet(text)
      rescue TwitterError => ex
        bot.say("Hmm...didn't work. Got this response:")
        bot.paste(ex.http_result.content)
      end
      return HALT
    when /^(?:save|queue)\s+(?:tweet|twitter):?\s*("?)(.*?)\1$/i
      tweet = Tweet.create(:person => message.person, :message => strip_links($2), :timestamp => message.timestamp.to_i)
      bot.say("Ok, saved for later: #{tweet}")
      return HALT
    when /^(?:show|list)\s+(?:all\s+)?(?:queued\s+)?(?:tweets|twitters)$/i
      act_on_list do |list|
        bot.say("Here are the tweets in the queue:")
        i = 0
        bot.paste(list.map{|tweet| "#{i+=1}. #{tweet}"}.join("\n"))
      end
      return HALT
    when /^show\s+next\s+(?:tweet|twitter)$/i
      act_on_list do |list|
        bot.say("Next tweet to post: #{list.first}")
      end
      return HALT
    when /^(?:post|send)\s+next\s+(?:tweet|twitter)$/i
      act_on_list do |list|
        tweet = list.first
        post_tweet(tweet)
        tweet.destroy
      end
      return HALT
    when /^(?:post|send)\s+tweet\s+(\d+)$/i
      act_on_tweet($1.to_i-1) do |tweet|
        post_tweet(tweet)
        tweet.destroy
      end
      return HALT
    when /^delete\s+tweet\s+(\d+)$/i
      act_on_tweet($1.to_i-1) do |tweet|
        tweet.destroy
        bot.say("Okay, deleted tweet #{$1}: #{tweet}")
      end
      return HALT
    end
  end
 
  # return array of available commands and descriptions
  def help
    [['tweet: <message>', "post <message> to #{config['username']}'s twitter account"],
     ['save tweet: <message>', "save <message> for later"],
     ['show tweets', "shows the queued tweets for #{config['username']}'s twitter account"],
     ['show next tweet', "shows the oldest queued twitter message"],
     ['post next tweet', "sends the oldest queued twitter message"],
     ['post tweet <n>', "sends the <n>th tweet from the list"],
     ['delete tweet <n>', "deletes the <n>th tweet from the list"]]
  end
 
  private
 
  def post_tweet(tweet)
    begin
      Tweet.tweet(tweet.to_s, config['username'], config['password'], config['proxy'])
      bot.say("Ok, tweeted: #{tweet}")
    rescue Tweet::TwitterError => ex
      bot.say("Hmm...didn't work. Got this response:")
      bot.paste(ex.http_result.content)
    end
  end
 
  def act_on_list
    list = Tweet.list
    if list.empty?
      bot.say("There are no queued tweets.")
    else
      yield list
    end
  end
 
  def act_on_tweet(n)
    if tweet = Tweet.list[n]
      yield tweet
    else
      bot.say("Could not find tweet #{n}")
    end
  end
 
  # strip links from CF messages
  def strip_links(msg)
    msg.gsub(/<a href="([^"]*)".*?>.*?<\/a>/, '\1')
  end
end