Sha256: f47906fbb2ae5f5d8fde709d987e5961f8b7585bd76b8fa5a1efe4be1a158b0f

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

module Chatterbot

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

    alias :blacklist :blocklist

    # 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 blocklist
      @blocklist || []
    end
    def blocklist=(b)
      @blocklist = b
    end

    #
    # Based on the text of this tweet, should it be skipped?
    def skip_me?(s)
      search = s.respond_to?(:text) ? s.text : s
      exclude.detect { |e| search.downcase.include?(e) } != nil
    end

    def interact_with_user?(t)
      return true unless only_interact_with_followers?
      u = t.respond_to?(:user) ? t.user : t
      client.friendship?(u, authenticated_user)
    end

    def valid_tweet?(object)
      if has_safelist? && ! on_safelist?(object)
        debug "skipping because user not on safelist"
        return false
      end

      !skippable_retweet?(object) && ! on_blocklist?(object) && ! skip_me?(object) && interact_with_user?(object)
    end

    #
    # Is this tweet from a user on our blocklist?
    def on_blocklist?(s)
      search = if s.is_a?(Twitter::User)
                 s.name
               elsif s.respond_to?(:user) && !s.is_a?(Twitter::NullObject)
                 from_user(s)
               else
                 s
               end.downcase

      blocklist.any? { |b| search.include?(b.downcase) }
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
chatterbot-2.2.0 lib/chatterbot/blocklist.rb
chatterbot-2.1.0 lib/chatterbot/blocklist.rb
chatterbot-2.0.5 lib/chatterbot/blocklist.rb
chatterbot-2.0.4 lib/chatterbot/blocklist.rb
chatterbot-2.0.3 lib/chatterbot/blocklist.rb
chatterbot-2.0.2 lib/chatterbot/blocklist.rb
chatterbot-2.0.0.pre lib/chatterbot/blocklist.rb