Module: Chatterbot::DirectMessages

Included in:
Bot
Defined in:
lib/chatterbot/direct_messages.rb

Overview

handle Twitter DMs

Instance Method Summary collapse

Instance Method Details

#direct_message(txt, user = nil) ⇒ Object

send a direct message



8
9
10
11
12
13
14
15
# File 'lib/chatterbot/direct_messages.rb', line 8

def direct_message(txt, user=nil)
  return unless 

  if user.nil?
    user = current_user
  end
  client.create_direct_message(user, txt)
end

#direct_messages(opts = {}, &block) ⇒ Object

check direct messages for the bot



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chatterbot/direct_messages.rb', line 20

def direct_messages(opts = {}, &block)
  return unless 
  debug "check for DMs since #{since_id_dm}"
        
  #
  # search twitter
  #

  @current_tweet = nil
  client.direct_messages_received(since_id:since_id_dm, count:200).each { |s|
    update_since_id_dm(s)
    debug s.text
    if has_safelist? && !on_safelist?(s.sender)
      debug "skipping because user not on safelist"
    elsif block_given? && !on_blocklist?(s.sender) && !skip_me?(s)
      @current_tweet = s
      yield s
    end
  }
  @current_tweet = nil
rescue Twitter::Error::Forbidden => e
  puts "sorry, looks like we're not allowed to check DMs for this account"
end