Module: Chatterbot::DSL
- Defined in:
- lib/chatterbot/dsl.rb
Overview
very basic DSL to handle the common stuff you would want to do with a bot.
Instance Method Summary collapse
-
#bad_words ⇒ Object
a common list of bad words, which you might want to filter out.
-
#blacklist(*args) ⇒ Object
specify a bot-specific blacklist of users.
-
#bot ⇒ Object
generate a Bot object.
-
#client ⇒ Object
Initialized Twitter::REST::Client.
-
#consumer_key(k) ⇒ Object
set the consumer key.
-
#consumer_secret(s) ⇒ Object
set the consumer secret.
-
#db ⇒ Object
return the bot’s current database connection, if available.
-
#debug_mode(d = nil) ⇒ Object
should we send tweets?.
-
#exclude(*args) ⇒ Object
specify list of strings we will check when deciding to respond to a tweet or not.
-
#favorite(id) ⇒ Object
favorite a tweet.
-
#follow(u) ⇒ Object
follow a user.
-
#followers(opts = {}) ⇒ Object
return a list of users following the bot.
-
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?.
- #only_interact_with_followers ⇒ Object
- #profile_text(p = nil) ⇒ Object
- #profile_website(w = nil) ⇒ Object
-
#replies(&block) ⇒ Object
handle replies to the bot.
-
#reply(txt, source) ⇒ Object
reply to a tweet.
-
#retweet(id) ⇒ Object
retweet a tweet.
-
#search(query, opts = {}, &block) ⇒ Object
search twitter for the specified terms, then pass any matches to the block.
-
#secret(s) ⇒ Object
set the secret.
-
#since_id(s = nil) ⇒ Object
The ID of the most recent tweet processed by the bot.
-
#since_id_reply ⇒ Object
get the id of the last tweet the bot replied to.
- #streaming(opts = {}, &block) ⇒ Object
- #streaming_tweets(opts = {}, &block) ⇒ Object
-
#token(s) ⇒ Object
set the token.
-
#tweet(txt, params = {}, original = nil) ⇒ Object
send a tweet.
-
#update_config ⇒ Object
explicitly save the configuration/state of the bot.
-
#verbose(d = nil) ⇒ Object
turn on/off verbose output.
-
#whitelist(*args) ⇒ Object
specify a bot-specific whitelist of users.
Instance Method Details
#bad_words ⇒ Object
a common list of bad words, which you might want to filter out. lifted from github.com/dariusk/wordfilter/blob/master/lib/badwords.json
273 274 275 276 277 278 |
# File 'lib/chatterbot/dsl.rb', line 273 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 |
#blacklist(*args) ⇒ Object
specify a bot-specific blacklist of users. accepts an array, or a comma-delimited string. when called, any subsequent calls to search or replies will filter out these users.
218 219 220 221 222 223 224 225 226 |
# File 'lib/chatterbot/dsl.rb', line 218 def blacklist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? bot.blacklist = [] else bot.blacklist += list end end |
#bot ⇒ Object
generate a Bot object. if the DSL is being called from a Bot object, just return it otherwise create a bot and return that
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/chatterbot/dsl.rb', line 110 def bot return @bot unless @bot.nil? # # parse any command-line options and use them to initialize the bot # params = {} #:nocov: opts = OptionParser.new opts. = "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 } 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 } 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 |
#client ⇒ Object
Returns initialized Twitter::REST::Client.
10 11 12 |
# File 'lib/chatterbot/dsl.rb', line 10 def client bot.client end |
#consumer_key(k) ⇒ Object
set the consumer key
319 320 321 |
# File 'lib/chatterbot/dsl.rb', line 319 def consumer_key(k) bot.config[:consumer_key] = k end |
#consumer_secret(s) ⇒ Object
set the consumer secret
312 313 314 |
# File 'lib/chatterbot/dsl.rb', line 312 def consumer_secret(s) bot.config[:consumer_secret] = s end |
#db ⇒ Object
return the bot’s current database connection, if available. handy if you need to manage data with your bot
356 357 358 |
# File 'lib/chatterbot/dsl.rb', line 356 def db bot.db end |
#debug_mode(d = nil) ⇒ Object
should we send tweets?
186 187 188 189 |
# File 'lib/chatterbot/dsl.rb', line 186 def debug_mode(d=nil) d = true if d.nil? bot.debug_mode = d end |
#exclude(*args) ⇒ Object
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
289 290 291 292 293 294 295 296 |
# File 'lib/chatterbot/dsl.rb', line 289 def exclude(*args) e = flatten_list_of_strings(args) if e.nil? || e.empty? bot.exclude = [] else bot.exclude += e end end |
#favorite(id) ⇒ Object
favorite a tweet
77 78 79 |
# File 'lib/chatterbot/dsl.rb', line 77 def favorite(id) bot.favorite(id) end |
#follow(u) ⇒ Object
follow a user
264 265 266 |
# File 'lib/chatterbot/dsl.rb', line 264 def follow(u) bot.follow(u) end |
#followers(opts = {}) ⇒ Object
return a list of users following the bot. This passes directly to the underlying Twitter API call
256 257 258 |
# File 'lib/chatterbot/dsl.rb', line 256 def followers(opts={}) bot.followers(opts) end |
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?
195 196 197 198 |
# File 'lib/chatterbot/dsl.rb', line 195 def no_update(d=nil) d = true if d.nil? bot.no_update = d end |
#only_interact_with_followers ⇒ Object
247 248 249 |
# File 'lib/chatterbot/dsl.rb', line 247 def only_interact_with_followers whitelist followers end |
#profile_text(p = nil) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/chatterbot/dsl.rb', line 90 def profile_text(p=nil) if p.nil? bot.profile_text else bot.profile_text(p) end end |
#profile_website(w = nil) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/chatterbot/dsl.rb', line 98 def profile_website(w=nil) if w.nil? bot.profile_website else bot.profile_website(w) end end |
#replies(&block) ⇒ Object
handle replies to the bot. Each time this is called, chatterbot will pass any replies since the last call to the specified block
35 36 37 |
# File 'lib/chatterbot/dsl.rb', line 35 def replies(&block) bot.replies(&block) end |
#reply(txt, source) ⇒ Object
reply to a tweet
86 87 88 |
# File 'lib/chatterbot/dsl.rb', line 86 def reply(txt, source) bot.reply(txt, source) end |
#retweet(id) ⇒ Object
retweet a tweet
69 70 71 |
# File 'lib/chatterbot/dsl.rb', line 69 def retweet(id) bot.retweet(id) end |
#search(query, opts = {}, &block) ⇒ Object
search twitter for the specified terms, then pass any matches to the block.
22 23 24 |
# File 'lib/chatterbot/dsl.rb', line 22 def search(query, opts = {}, &block) bot.search(query, opts, &block) end |
#secret(s) ⇒ Object
set the secret
326 327 328 |
# File 'lib/chatterbot/dsl.rb', line 326 def secret(s) bot.config[:secret] = s end |
#since_id(s = nil) ⇒ Object
The ID of the most recent tweet processed by the bot
302 303 304 305 306 307 |
# File 'lib/chatterbot/dsl.rb', line 302 def since_id(s=nil) if s bot.config[:since_id] = s end bot.config[:since_id] end |
#since_id_reply ⇒ Object
get the id of the last tweet the bot replied to
340 341 342 |
# File 'lib/chatterbot/dsl.rb', line 340 def since_id_reply bot.config[:since_id_reply] end |
#streaming(opts = {}, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/chatterbot/dsl.rb', line 39 def streaming(opts = {}, &block) params = { :endpoint => :user }.merge(opts) h = StreamingHandler.new(bot, params) h.apply block bot.do_streaming(h) end |
#streaming_tweets(opts = {}, &block) ⇒ Object
50 51 52 |
# File 'lib/chatterbot/dsl.rb', line 50 def streaming_tweets(opts={}, &block) bot.streaming_tweets(opts, &block) end |
#token(s) ⇒ Object
set the token
333 334 335 |
# File 'lib/chatterbot/dsl.rb', line 333 def token(s) bot.config[:token] = s end |
#tweet(txt, params = {}, original = nil) ⇒ Object
send a tweet
62 63 64 |
# File 'lib/chatterbot/dsl.rb', line 62 def tweet(txt, params = {}, original = nil) bot.tweet(txt, params, original) end |
#update_config ⇒ Object
explicitly save the configuration/state of the bot.
348 349 350 |
# File 'lib/chatterbot/dsl.rb', line 348 def update_config bot.update_config end |
#verbose(d = nil) ⇒ Object
turn on/off verbose output
204 205 206 207 |
# File 'lib/chatterbot/dsl.rb', line 204 def verbose(d=nil) d = true if d.nil? bot.verbose = d end |
#whitelist(*args) ⇒ Object
specify a bot-specific whitelist 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.
237 238 239 240 241 242 243 244 245 |
# File 'lib/chatterbot/dsl.rb', line 237 def whitelist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? bot.whitelist = [] else bot.whitelist += list end end |