lib/chatterbot/dsl.rb in chatterbot-0.7.1 vs lib/chatterbot/dsl.rb in chatterbot-0.9.0
- old
+ new
@@ -3,20 +3,22 @@
module Chatterbot
#
# very basic DSL to handle the common stuff you would want to do with a bot.
module DSL
+ #
+ # @return initialized Twitter::REST::Client
def client
bot.client
end
#
# search twitter for the specified terms, then pass any matches to
# the block.
# @example
# search("chatterbot is cool!") do |tweet|
- # puts tweet[:text] # this is the actual tweeted text
+ # puts tweet.text # this is the actual tweeted text
# reply "I agree!", tweet
# end
def search(query, opts = {}, &block)
bot.search(query, opts, &block)
end
@@ -25,17 +27,32 @@
# handle replies to the bot. Each time this is called, chatterbot
# will pass any replies since the last call to the specified block
#
# @example
# replies do |tweet|
- # puts tweet[:text] # this is the actual tweeted text
+ # puts tweet.text # this is the actual tweeted text
# reply "Thanks for the mention!", tweet
# end
def replies(&block)
bot.replies(&block)
end
+ def streaming(opts = {}, &block)
+ params = {
+ :endpoint => :user
+ }.merge(opts)
+
+ h = StreamingHandler.new(bot, params)
+ h.apply block
+
+ bot.do_streaming(h)
+ end
+
+ def streaming_tweets(opts={}, &block)
+ bot.streaming_tweets(opts, &block)
+ end
+
#
# send a tweet
#
# @param [String] txt the text you want to tweet
# @param [Hash] params opts for the tweet
@@ -46,43 +63,71 @@
bot.tweet(txt, params, original)
end
#
# retweet a tweet
- # @param [id] id the ID of the tweet
+ # @param [id] id A tweet or the ID of a tweet
def retweet(id)
bot.retweet(id)
end
+
#
+ # favorite a tweet
+ # @param [id] id A tweet or the ID of a tweet
+ def favorite(id)
+ bot.favorite(id)
+ end
+
+ #
# reply to a tweet
#
# @param [String] txt the text you want to tweet
# @param [Tweet] source the original tweet you are replying to
def reply(txt, source)
bot.reply(txt, source)
end
+ def profile_text(p=nil)
+ if p.nil?
+ bot.profile_text
+ else
+ bot.profile_text(p)
+ end
+ end
+
+ def profile_website(w=nil)
+ if w.nil?
+ bot.profile_website
+ else
+ bot.profile_website(w)
+ end
+ end
+
#
# 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
+ puts @bot.inspect
return @bot unless @bot.nil?
+
#
# parse any command-line options and use them to initialize the bot
#
params = {}
+ #:nocov:
opts = OptionParser.new
opts.banner = "Usage: #{File.basename($0)} [options]"
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 }
@@ -90,19 +135,51 @@
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
}
+ opts.on('--profile [ARG]', "get/set your bot's profile text") { |p|
+ @handle_profile_text = true
+ @profile_text = p
+ }
+ opts.on('--website [ARG]', "get/set your bot's profile URL") { |u|
+ @handle_profile_website = true
+ @profile_website = 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
+ 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?
# @param [Boolean] d true/false if we should send tweets
@@ -155,12 +232,32 @@
# @see http://rdoc.info/gems/twitter/Twitter/API/FriendsAndFollowers#followers-instance_method
#
def followers(opts={})
bot.followers(opts)
end
+
+ #
+ # follow a user
+ #
+ # @param u a Twitter::User or user id
+ def follow(u)
+ bot.follow(u)
+ end
+
#
+ # 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" ]
+ 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.
# when called, any subsequent calls to search or replies will
# filter out tweets with these strings
#
@@ -185,28 +282,45 @@
bot.config[:since_id] = s
end
bot.config[:since_id]
end
+ #
+ # set the consumer secret
+ # @param s [String] the consumer secret
def consumer_secret(s)
bot.config[:consumer_secret] = s
end
+ #
+ # set the consumer key
+ # @param k [String] the consumer key
def consumer_key(k)
bot.config[:consumer_key] = k
end
+ #
+ # set the secret
+ # @param s [String] the secret
def secret(s)
bot.config[:secret] = s
end
+
+ #
+ # set the token
+ # @param s [String] the token
def token(s)
bot.config[:token] = s
end
+ #
+ # get the id of the last tweet the bot replied to
+ # @return tweet id
def since_id_reply
bot.config[:since_id_reply]
end
+
#
# explicitly save the configuration/state of the bot.
#
def update_config
bot.update_config