Class: TweetBot::Bot
- Inherits:
-
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
#initialize ⇒ Bot
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_frequency ⇒ Object
Returns the value of attribute response_frequency.
4
5
6
|
# File 'lib/tweetbot/bot.rb', line 4
def response_frequency
@response_frequency
end
|
Returns the value of attribute twitter_auth.
4
5
6
|
# File 'lib/tweetbot/bot.rb', line 4
def
@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_search ⇒ Object
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
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()
responses = ()
"@#{.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
|
78
79
80
|
# File 'lib/tweetbot/bot.rb', line 78
def ()
find_phrase_value(@responses_for_phrases, .text, [])
end
|
#should_i_respond_to?(tweet) ⇒ 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?()
if under_rate_limit_pause?
puts "Under rate limit pause. Will let up at #{@rate_limited_until.to_s}"
return false
end
matches = ()
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
|
74
75
76
|
# File 'lib/tweetbot/bot.rb', line 74
def ()
().any?
end
|
#under_rate_limit_pause? ⇒ 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
|