Class: TweetBot::Bot

Inherits:
Object
  • Object
show all
Includes:
Talk
Defined in:
lib/tweetbot/bot.rb

Constant Summary collapse

DefaultFrequency =
20
Noop =
lambda {|status|}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Talk

#announce_wake_up, #configure_rest_twitter_client, #configure_streaming_twitter, #send_twitter_message, #talk

Constructor Details

#initializeBot

Returns a new instance of Bot.



10
11
12
13
14
# File 'lib/tweetbot/bot.rb', line 10

def initialize()
  self.response_frequency = DefaultFrequency
  @responses_for_phrases = Hash.new { |hash, key| hash[key] = [] }
  @blocks_for_phrases = {}
end

Instance Attribute Details

#response_frequencyObject

Returns the value of attribute response_frequency.



4
5
6
# File 'lib/tweetbot/bot.rb', line 4

def response_frequency
  @response_frequency
end

#twitter_authObject

Returns the value of attribute twitter_auth.



4
5
6
# File 'lib/tweetbot/bot.rb', line 4

def twitter_auth
  @twitter_auth
end

Instance Method Details

#add_responses_for_phrase(phrase, *responses) ⇒ Object



38
39
40
# File 'lib/tweetbot/bot.rb', line 38

def add_responses_for_phrase(phrase, *responses)
  @responses_for_phrases[phrase.downcase] += responses
end

#alert_status_captured(status) ⇒ Object



20
21
22
# File 'lib/tweetbot/bot.rb', line 20

def alert_status_captured(status)
  find_phrase_value(@blocks_for_phrases, status.text, Noop).call(status)
end

#find_phrase_value(hash, text, default) ⇒ Object



82
83
84
# File 'lib/tweetbot/bot.rb', line 82

def find_phrase_value(hash, text, default)
  hash.select { |phrase, _| text =~ /#{phrase.downcase}/i }.values[0] || default
end

#on_status_captured(key, &block) ⇒ Object



16
17
18
# File 'lib/tweetbot/bot.rb', line 16

def on_status_captured(key, &block)
  @blocks_for_phrases[key] = block
end

#phrases_to_searchObject



24
25
26
# File 'lib/tweetbot/bot.rb', line 24

def phrases_to_search
  @responses_for_phrases.keys + @blocks_for_phrases.keys
end

#rate_limited!Object



65
66
67
68
# File 'lib/tweetbot/bot.rb', line 65

def rate_limited!
  puts "Starting rate limit throttling!"
  @rate_limited_until = Time.now + 3600
end

#respond_to_phrase(phrase) {|responses| ... } ⇒ Object

Yields:

  • (responses)


32
33
34
35
36
# File 'lib/tweetbot/bot.rb', line 32

def respond_to_phrase(phrase)
  responses = []
  yield responses
  add_responses_for_phrase(phrase, *responses)
end

#response_for(tweet) ⇒ Object



42
43
44
45
# File 'lib/tweetbot/bot.rb', line 42

def response_for(tweet)
  responses = responses_for_tweet(tweet)
  "@#{tweet.user.screen_name} #{responses.sample}"
end

#responses_for(phrase) ⇒ Object



28
29
30
# File 'lib/tweetbot/bot.rb', line 28

def responses_for(phrase)
  @responses_for_phrases[phrase]
end

#responses_for_tweet(tweet) ⇒ Object



78
79
80
# File 'lib/tweetbot/bot.rb', line 78

def responses_for_tweet(tweet)
  find_phrase_value(@responses_for_phrases, tweet.text, [])
end

#should_i_respond_to?(tweet) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tweetbot/bot.rb', line 47

def should_i_respond_to?(tweet)
  if under_rate_limit_pause?
    puts "Under rate limit pause. Will let up at #{@rate_limited_until.to_s}"
    return false
  end
  matches = tweet_matches?(tweet)
  unless matches
    puts "Tweet does not match"
    return false
  end
  frequency_check = (rand(100) < self.response_frequency)
  unless frequency_check
    puts "Frequency check failed"
    return false
  end
  true
end

#tweet_matches?(tweet) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/tweetbot/bot.rb', line 74

def tweet_matches?(tweet)
  responses_for_tweet(tweet).any?
end

#under_rate_limit_pause?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/tweetbot/bot.rb', line 70

def under_rate_limit_pause?
  @rate_limited_until && (Time.now < @rate_limited_until)
end