Sha256: eacc9470f4bcb908be18cb2ea1637b0201f6a88b9a1a7c3861fb62dad258cf7c

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module Chatterbot

  #
  # methods for preventing the bot from spamming people who don't want to hear from it
  module Blacklist
    attr_accessor :exclude, :blacklist

    # 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
    def exclude
      @exclude || []
    end
    
    # A list of Twitter users that don't want to hear from the bot.
    def blacklist
      @blacklist || []
    end
    def blacklist=(b)
      @blacklist = b
    end
    
    #
    # Based on the text of this tweet, should it be skipped?
    def skip_me?(s)
      search = s.is_a?(Hash) ? s[:text] : s
      exclude.detect { |e| search.downcase.include?(e) } != nil
    end

    #
    # Pull the username from a tweet hash -- this is different depending on
    # if we're doing a search, or parsing through replies/mentions.
    def tweet_user(s)
      s.has_key?(:from_user) ? s[:from_user] : s[:user][:screen_name]
    end
    
    #
    # Is this tweet from a user on our blacklist?
    def on_blacklist?(s)
      search = (s.is_a?(Hash) ? tweet_user(s) : s).downcase
      blacklist.any? { |b| search.include?(b.downcase) } ||
        on_global_blacklist?(search)
    end

    #
    # Is this user on our global blacklist?
    def on_global_blacklist?(user)
      return false if ! has_db?
      db[:blacklist].where(:user => user).count > 0
    end

    #
    # add the specified user to the global blacklist
    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
    
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
chatterbot-0.2.8 lib/chatterbot/blacklist.rb
chatterbot-0.2.7 lib/chatterbot/blacklist.rb
chatterbot-0.2.6 lib/chatterbot/blacklist.rb
chatterbot-0.2.5 lib/chatterbot/blacklist.rb
chatterbot-0.2.4 lib/chatterbot/blacklist.rb
chatterbot-0.2.3 lib/chatterbot/blacklist.rb
chatterbot-0.2.2 lib/chatterbot/blacklist.rb
chatterbot-0.2.1 lib/chatterbot/blacklist.rb
chatterbot-0.2.0 lib/chatterbot/blacklist.rb