Module: Chatterbot::Blacklist

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

Overview

methods for preventing the bot from spamming people who don't want to hear from it

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blacklistObject

A list of Twitter users that don't want to hear from the bot.


15
16
17
# File 'lib/chatterbot/blacklist.rb', line 15

def blacklist
  @blacklist
end

#excludeObject

return a list of text strings which we will check for in incoming tweets. If the text is listed, we won't use this tweet


10
11
12
# File 'lib/chatterbot/blacklist.rb', line 10

def exclude
  @exclude
end

Instance Method Details

#add_to_blacklist(user) ⇒ Object

add the specified user to the global blacklist


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chatterbot/blacklist.rb', line 46

def add_to_blacklist(user)    
  user = user.is_a?(Hash) ? user[:from_user] : user
  
  # don't try and add if we don't have a DB connection, or if the
  # user is already on the list
  return if ! has_db? || on_global_blacklist?(user)

  # make sure we don't have an @ at the beginning of the username
  user.gsub!(/^@/, "")
  debug "adding #{user} to blacklist"

  db[:blacklist].insert({ :user => user, :created_at => Time.now }) # 
end

#on_blacklist?(s) ⇒ Boolean

Is this tweet from a user on our blacklist?

Returns:

  • (Boolean)

31
32
33
34
35
# File 'lib/chatterbot/blacklist.rb', line 31

def on_blacklist?(s)
  search = (s.respond_to?(:user) ? from_user(s) : s).downcase
  blacklist.any? { |b| search.include?(b.downcase) } ||
    on_global_blacklist?(search)
end

#on_global_blacklist?(user) ⇒ Boolean

Is this user on our global blacklist?

Returns:

  • (Boolean)

39
40
41
42
# File 'lib/chatterbot/blacklist.rb', line 39

def on_global_blacklist?(user)
  return false if ! has_db?
  db[:blacklist].where(:user => user).count > 0
end

#skip_me?(s) ⇒ Boolean

Based on the text of this tweet, should it be skipped?

Returns:

  • (Boolean)

24
25
26
27
# File 'lib/chatterbot/blacklist.rb', line 24

def skip_me?(s)
  search = s.respond_to?(:text) ? s.text : s
  exclude.detect { |e| search.downcase.include?(e) } != nil
end