lib/chatterbot/dsl.rb in chatterbot-1.0.2 vs lib/chatterbot/dsl.rb in chatterbot-2.0.0.pre

- old
+ new

@@ -12,24 +12,21 @@ end # # search twitter for the specified terms, then pass any matches to # the block. - # @param opts [Hash] options. these will be passed directly to + # @param args [Hash] options. these will be passed directly to # Twitter via the twitter gem. You can see the possible arguments # at http://www.rubydoc.info/gems/twitter/Twitter/REST/Search#search-instance_method - # There is one extra argument: - # @option options [Integer] :limit limit the number of tweets to - # return per search - + # # @example # search("chatterbot is cool!") do |tweet| # puts tweet.text # this is the actual tweeted text # reply "I agree!", tweet # end - def search(query, opts = {}, &block) - bot.search(query, opts, &block) + def search(*args, &block) + bot.register_handler(:search, args, &block) end # # handle tweets that are on the bot's home timeline. this includes # tweets from accounts the bot is following, as well as its own tweets @@ -37,12 +34,12 @@ # @example # home_timeline do |tweet| # puts tweet.text # this is the actual tweeted text # favorite tweet # i like to fave tweets # end - def home_timeline(opts = {}, &block) - bot.home_timeline(opts, &block) + def home_timeline(&block) + bot.register_handler(:home_timeline, block) end # # handle replies to the bot. Each time this is called, chatterbot # will pass any replies since the last call to the specified block @@ -51,27 +48,68 @@ # replies do |tweet| # puts tweet.text # this is the actual tweeted text # reply "Thanks for the mention!", tweet # end def replies(&block) - bot.replies(&block) + bot.register_handler(:replies, block) end - def streaming(opts = {}, &block) - params = { - :endpoint => :user - }.merge(opts) + # + # handle direct messages sent to the bot. Each time this is called, chatterbot + # will pass any DMs since the last call to the specified block + # + # @example + # direct_messages do |dm| + # puts dm.text # this is the actual tweeted text + # direct_message "Thanks for the mention!", dm.sender + # end + def direct_messages(&block) + bot.register_handler(:direct_messages, block) + end + + + # + # handle notifications of bot tweets favorited by other users. + # Using this block will require usage of the Streaming API. + # + # @example + # favorited do |tweet| + # puts tweet.text # this is the actual tweeted text + # reply "@#{user.screen_name} thanks for the fave!", tweet + # end + def favorited(&block) + bot.register_handler(:favorited, block) + end + + # + # handle notifications that the bot has a new follower. + # Using this block will require usage of the Streaming API. + # + # @example + # followed do |user| + # follow user + # end + def followed(&block) + bot.register_handler(:followed, block) + end + + # + # handle notifications of tweets on the bot's timeline that were deleted. + # Using this block will require usage of the Streaming API. + def deleted(&block) + bot.register_handler(:deleted, block) + end - h = StreamingHandler.new(bot, params) - h.apply block - bot.do_streaming(h) + # + # enable or disable usage of the Streaming API + # + def use_streaming(s=nil) + s = true if s.nil? + bot.streaming = s end - def streaming_tweets(opts={}, &block) - bot.streaming_tweets(opts, &block) - end # # send a tweet # # @param [String] txt the text you want to tweet @@ -106,10 +144,20 @@ def reply(txt, source) bot.reply(txt, source) end # + # send a direct message to the specified user + # + # @param [String] txt the text you want to tweet + # @param [User] user to send the DM to + def direct_message(txt, user=nil) + bot.direct_message(txt, user) + end + + + # # handle getting/setting the profile text. # @param [p] p The new value for the profile. If this isn't passed in, the method will simply return the current value # @return profile text def profile_text(p=nil) if p.nil? @@ -119,11 +167,11 @@ end end # # handle getting/setting the profile website - # @param [p] p The new value for the website. If this isn't passed in, the method will simply return the current value + # @param [w] w The new value for the website. If this isn't passed in, the method will simply return the current value # @return profile website def profile_website(w=nil) if w.nil? bot.profile_website else @@ -136,10 +184,11 @@ # generate a Bot object. if the DSL is being called from a Bot object, just return it # otherwise create a bot and return that def bot return @bot unless @bot.nil? + @bot_command = nil # # parse any command-line options and use them to initialize the bot # params = {} @@ -151,61 +200,44 @@ opts.separator "" opts.separator "Specific options:" - opts.on('-d', '--db [ARG]', "Specify a DB connection URI") { |d| ENV["chatterbot_db"] = d } opts.on('-c', '--config [ARG]', "Specify a config file to use") { |c| ENV["chatterbot_config"] = c } opts.on('-t', '--test', "Run the bot without actually sending any tweets") { params[:debug_mode] = true } opts.on('-v', '--verbose', "verbose output to stdout") { params[:verbose] = true } opts.on('--dry-run', "Run the bot in test mode, and also don't update the database") { params[:debug_mode] = true ; params[:no_update] = true } - opts.on('-s', '--since_id [ARG]', "Check for tweets since tweet id #[ARG]") { |s| params[:since_id] = s.to_i } - opts.on('-m', '--since_id_reply [ARG]', "Check for mentions since tweet id #[ARG]") { |s| params[:since_id_reply] = s.to_i } + opts.on('-r', '--reset', "Reset your bot to ignore old tweets") { - params[:debug_mode] = true - params[:reset_since_id] = true + @bot_command = :reset_since_id_counters } + opts.on('--profile [ARG]', "get/set your bot's profile text") { |p| - @handle_profile_text = true - @profile_text = p + @bot_command = :profile_text + @bot_command_args = [ p ] } + opts.on('--website [ARG]', "get/set your bot's profile URL") { |u| - @handle_profile_website = true - @profile_website = u + @bot_command = :profile_website + @bot_command_args = [ u ] } - opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.parse!(ARGV) #:nocov: @bot = Chatterbot::Bot.new(params) - - if @handle_profile_text == true - if !@profile_text.nil? - @bot.profile_text @profile_text - else - puts @bot.profile_text - end + if @bot_command != nil + @bot.skip_run = true + result = @bot.send(@bot_command, *@bot_command_args) + puts result end - if @handle_profile_website == true - if !@profile_website.nil? - @bot.profile_website @profile_website - else - puts @bot.profile_website - end - end - - if @handle_profile_website == true || @handle_profile_text == true - exit - end - @bot end # # should we send tweets? @@ -233,49 +265,56 @@ d = true if d.nil? bot.verbose = d end # - # specify a bot-specific blacklist of users. accepts an array, or a + # specify a bot-specific blocklist of users. accepts an array, or a # comma-delimited string. when called, any subsequent calls to # search or replies will filter out these users. # # @param [Array, String] args list of usernames # @example - # blacklist "mean_user, private_user" + # blocklist "mean_user, private_user" # - def blacklist(*args) + def blocklist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? - bot.blacklist = [] + bot.blocklist = [] else - bot.blacklist += list + bot.blocklist += list end end + alias :blacklist :blocklist + # - # specify a bot-specific whitelist of users. accepts an array, or a + # specify a bot-specific safelist of users. accepts an array, or a # comma-delimited string. when called, any subsequent calls to # search or replies will only act upon these users. # # @param [Array, String] args list of usernames or Twitter::User objects # @example - # whitelist "mean_user, private_user" + # safelist "mean_user, private_user" # - def whitelist(*args) + def safelist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? - bot.whitelist = [] + bot.safelist = [] else - bot.whitelist += list + bot.safelist += list end end - + alias :whitelist :safelist + + # + # specify that the bot should only reply to tweets from users that + # are followers, basically making interactions opt-in + # def only_interact_with_followers - whitelist followers + bot.config[:only_interact_with_followers] = true end # # return a list of users following the bot. This passes directly # to the underlying Twitter API call @@ -297,14 +336,59 @@ # # a common list of bad words, which you might want to filter out. # lifted from https://github.com/dariusk/wordfilter/blob/master/lib/badwords.json # def bad_words - ["skank", "wetback", "bitch", "cunt", "dick", "douchebag", "dyke", "fag", "nigger", "tranny", "trannies", - "paki", "pussy", "retard", "slut", "titt", "tits", "wop", "whore", "chink", "fatass", "shemale", "daygo", - "dego", "dago", "gook", "kike", "kraut", "spic", "twat", "lesbo", "homo", "fatso", "lardass", "jap", - "biatch", "tard", "gimp", "gyp", "chinaman", "chinamen", "golliwog", "crip", "raghead" ] + [ + "biatch", + "bitch", + "chinaman", + "chinamen", + "chink", + "crip", + "cunt", + "dago", + "daygo", + "dego", + "dick", + "douchebag", + "dyke", + "fag", + "fatass", + "fatso", + "gash", + "gimp", + "golliwog", + "gook", + "gyp", + "homo", + "hooker", + "jap", + "kike", + "kraut", + "lardass", + "lesbo", + "negro", + "nigger", + "paki", + "pussy", + "raghead", + "retard", + "shemale", + "skank", + "slut", + "spic", + "tard", + "tits", + "titt", + "trannies", + "tranny", + "twat", + "wetback", + "whore", + "wop" + ] end # # specify list of strings we will check when deciding to respond # to a tweet or not. accepts an array or a comma-delimited string. @@ -336,32 +420,36 @@ # # set the consumer secret # @param s [String] the consumer secret def consumer_secret(s) + bot.deprecated "Setting consumer_secret outside of your config file is deprecated!", Kernel.caller.first bot.config[:consumer_secret] = s end - + # # set the consumer key # @param k [String] the consumer key def consumer_key(k) + bot.deprecated "Setting consumer_key outside of your config file is deprecated!", Kernel.caller.first bot.config[:consumer_key] = k end # # set the secret # @param s [String] the secret def secret(s) - bot.config[:secret] = s + bot.deprecated "Setting access_token_secret outside of your config file is deprecated!", Kernel.caller.first + bot.config[:access_token_secret] = s end # # set the token # @param s [String] the token def token(s) - bot.config[:token] = s + bot.deprecated "Setting access_token outside of your config file is deprecated!", Kernel.caller.first + bot.config[:access_token] = s end # # get the id of the last tweet the bot replied to # @return tweet id @@ -375,18 +463,9 @@ # def update_config bot.update_config end - # - # return the bot's current database connection, if available. - # handy if you need to manage data with your bot - # - def db - bot.db - end - - protected # # take a variable list of strings and possibly arrays and turn # them into a single flat array of strings