Module: TweetBot::Talk

Included in:
Bot
Defined in:
lib/tweetbot/talk.rb

Instance Method Summary collapse

Instance Method Details

#announce_wake_upObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tweetbot/talk.rb', line 94

def announce_wake_up
  puts "Waking up to greet the world... #{Time.now}"
   send_twitter_message "Waking up to greet the world... #{Time.now}"
rescue Twitter::Error::Forbidden => ex
  puts "Twitter Forbidden Error while waking up"
  puts ex
  puts "Continuing"
rescue Twitter::Error => ex
  puts "Twitter Error while waking up"
  puts ex
  exit!(1)
rescue => ex
  puts "Unknown Error while waking up"
  puts ex
  exit!(1)
end

#configure_rest_twitter_clientObject



24
25
26
27
28
29
30
31
# File 'lib/tweetbot/talk.rb', line 24

def configure_rest_twitter_client
  Twitter::REST::Client.new do |config|
    config.consumer_key = twitter_auth[:consumer_key]
    config.consumer_secret = twitter_auth[:consumer_secret]
    config.access_token = twitter_auth[:access_token]
    config.access_token_secret = twitter_auth[:access_token_secret]
  end
end

#configure_streaming_twitterObject



15
16
17
18
19
20
21
22
# File 'lib/tweetbot/talk.rb', line 15

def configure_streaming_twitter
  Twitter::Streaming::Client.new do |config|
    config.consumer_key = twitter_auth[:consumer_key]
    config.consumer_secret = twitter_auth[:consumer_secret]
    config.access_token = twitter_auth[:access_token]
    config.access_token_secret = twitter_auth[:access_token_secret]
  end
end

#send_twitter_message(message, options = {}) ⇒ Object



89
90
91
92
# File 'lib/tweetbot/talk.rb', line 89

def send_twitter_message(message, options = {})
  client = configure_rest_twitter_client
  client.update message, options
end

#talkObject



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
# File 'lib/tweetbot/talk.rb', line 33

def talk
  client = configure_streaming_twitter

  bot = self

  announce_wake_up
  puts "Listening... #{Time.now}"

  ["INT", "TERM"].each do |signal|
    trap(signal) do
      puts "Got #{signal}"
      exit!(1)
    end
  end

  at_exit do
    puts "Shutting down... #{Time.now}"
    begin
      # send_twitter_message "Going to sleep... #{Time.now}"
    rescue
    end
  end



  client.filter(track: bot.phrases_to_search.join(",")) do |status|
    puts status.text if status.is_a?(Twitter::Tweet)
    if status.user.screen_name.downcase == TwitterAuth::MyName.downcase
      puts "#{Time.now} Caught myself saying it"
    else
      puts "#{Time.now} #{status.user.screen_name} said #{status.text}"
      if bot.should_i_respond_to?(status)
        response = bot.response_for(status)
        begin
          send_twitter_message(response, :in_reply_to_status_id => status.id)
          puts "Responding"
        rescue Twitter::Error::Forbidden => ex
          puts "Rate limited!"
          bot.rate_limited!
        rescue Exception => ex
          puts "Exception while sending the reply"
          puts ex
        end
      else
        puts "Bot told me not to respond"
      end
      begin
        bot.alert_status_captured(status)
      rescue => ex
        puts "Exception while alerting status"
        puts ex
      end
    end
  end
end