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.
-
#blocklist(*args) ⇒ Object
(also: #blacklist)
specify a bot-specific blocklist 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.
-
#debug_mode(d = nil) ⇒ Object
should we send tweets?.
-
#deleted(&block) ⇒ Object
handle notifications of tweets on the bot’s timeline that were deleted.
-
#direct_message(txt, user = nil) ⇒ Object
send a direct message to the specified user.
-
#direct_messages(&block) ⇒ Object
handle direct messages sent to the bot.
-
#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.
-
#favorited(&block) ⇒ Object
handle notifications of bot tweets favorited by other users.
-
#follow(u) ⇒ Object
follow a user.
-
#followed(&block) ⇒ Object
handle notifications that the bot has a new follower.
-
#followers(opts = {}) ⇒ Object
return a list of users following the bot.
-
#home_timeline(&block) ⇒ Object
handle tweets that are on the bot’s home timeline.
-
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?.
-
#only_interact_with_followers ⇒ Object
specify that the bot should only reply to tweets from users that are followers, basically making interactions opt-in.
-
#profile_text(p = nil) ⇒ Object
handle getting/setting the profile text.
-
#profile_website(w = nil) ⇒ Object
handle getting/setting the profile website.
-
#replies(&block) ⇒ Object
handle replies to the bot.
-
#reply(txt, source, params = {}) ⇒ Object
reply to a tweet.
-
#retweet(id) ⇒ Object
retweet a tweet.
-
#safelist(*args) ⇒ Object
(also: #whitelist)
specify a bot-specific safelist of users.
-
#search(*args, &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.
-
#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.
-
#use_streaming(s = nil) ⇒ Object
enable or disable usage of the Streaming API.
-
#verbose(d = nil) ⇒ Object
turn on/off verbose output.
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
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/chatterbot/dsl.rb', line 352 def bad_words [ "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 |
#blocklist(*args) ⇒ Object Also known as: blacklist
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.
290 291 292 293 294 295 296 297 298 |
# File 'lib/chatterbot/dsl.rb', line 290 def blocklist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? bot.blocklist = [] else bot.blocklist += 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
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/chatterbot/dsl.rb', line 198 def bot return @bot unless @bot.nil? @bot_command = 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('-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('-r', '--reset', "Reset your bot to ignore old tweets") { @bot_command = :reset_since_id_counters } opts.on('--profile [ARG]', "get/set your bot's profile text") { |p| @bot_command = :profile_text @bot_command_args = [ p ] } opts.on('--website [ARG]', "get/set your bot's profile URL") { |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 @bot_command != nil @bot.skip_run = true result = @bot.send(@bot_command, *@bot_command_args) puts result 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
444 445 446 447 |
# File 'lib/chatterbot/dsl.rb', line 444 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 |
#consumer_secret(s) ⇒ Object
set the consumer secret
436 437 438 439 |
# File 'lib/chatterbot/dsl.rb', line 436 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 |
#debug_mode(d = nil) ⇒ Object
should we send tweets?
258 259 260 261 |
# File 'lib/chatterbot/dsl.rb', line 258 def debug_mode(d=nil) d = true if d.nil? bot.debug_mode = d end |
#deleted(&block) ⇒ Object
handle notifications of tweets on the bot’s timeline that were deleted. Using this block will require usage of the Streaming API.
98 99 100 |
# File 'lib/chatterbot/dsl.rb', line 98 def deleted(&block) bot.register_handler(:deleted, block) end |
#direct_message(txt, user = nil) ⇒ Object
send a direct message to the specified user
165 166 167 |
# File 'lib/chatterbot/dsl.rb', line 165 def (txt, user=nil) bot.(txt, user) end |
#direct_messages(&block) ⇒ Object
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
65 66 67 |
# File 'lib/chatterbot/dsl.rb', line 65 def (&block) bot.register_handler(:direct_messages, block) 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
413 414 415 416 417 418 419 420 |
# File 'lib/chatterbot/dsl.rb', line 413 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
140 141 142 |
# File 'lib/chatterbot/dsl.rb', line 140 def favorite(id) bot.favorite(id) end |
#favorited(&block) ⇒ Object
handle notifications of bot tweets favorited by other users. Using this block will require usage of the Streaming API.
79 80 81 |
# File 'lib/chatterbot/dsl.rb', line 79 def favorited(&block) bot.register_handler(:favorited, block) end |
#follow(u) ⇒ Object
follow a user
343 344 345 |
# File 'lib/chatterbot/dsl.rb', line 343 def follow(u) bot.follow(u) end |
#followed(&block) ⇒ Object
handle notifications that the bot has a new follower. Using this block will require usage of the Streaming API.
91 92 93 |
# File 'lib/chatterbot/dsl.rb', line 91 def followed(&block) bot.register_handler(:followed, block) end |
#followers(opts = {}) ⇒ Object
return a list of users following the bot. This passes directly to the underlying Twitter API call
335 336 337 |
# File 'lib/chatterbot/dsl.rb', line 335 def followers(opts={}) bot.followers(opts) end |
#home_timeline(&block) ⇒ Object
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
39 40 41 |
# File 'lib/chatterbot/dsl.rb', line 39 def home_timeline(&block) bot.register_handler(:home_timeline, block) end |
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?
267 268 269 270 |
# File 'lib/chatterbot/dsl.rb', line 267 def no_update(d=nil) d = true if d.nil? bot.no_update = d end |
#only_interact_with_followers ⇒ Object
specify that the bot should only reply to tweets from users that are followers, basically making interactions opt-in
326 327 328 |
# File 'lib/chatterbot/dsl.rb', line 326 def only_interact_with_followers bot.config[:only_interact_with_followers] = true end |
#profile_text(p = nil) ⇒ Object
handle getting/setting the profile text.
174 175 176 177 178 179 180 |
# File 'lib/chatterbot/dsl.rb', line 174 def profile_text(p=nil) if p.nil? bot.profile_text else bot.profile_text(p) end end |
#profile_website(w = nil) ⇒ Object
handle getting/setting the profile website
186 187 188 189 190 191 192 |
# File 'lib/chatterbot/dsl.rb', line 186 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
52 53 54 |
# File 'lib/chatterbot/dsl.rb', line 52 def replies(&block) bot.register_handler(:replies, block) end |
#reply(txt, source, params = {}) ⇒ Object
reply to a tweet
156 157 158 |
# File 'lib/chatterbot/dsl.rb', line 156 def reply(txt, source, params={}) bot.reply(txt, source, params) end |
#retweet(id) ⇒ Object
retweet a tweet
132 133 134 |
# File 'lib/chatterbot/dsl.rb', line 132 def retweet(id) bot.retweet(id) end |
#safelist(*args) ⇒ Object Also known as: whitelist
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.
311 312 313 314 315 316 317 318 319 |
# File 'lib/chatterbot/dsl.rb', line 311 def safelist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? bot.safelist = [] else bot.safelist += list end end |
#search(*args, &block) ⇒ Object
search twitter for the specified terms, then pass any matches to the block. Twitter via the twitter gem. You can see the possible arguments at www.rubydoc.info/gems/twitter/Twitter/REST/Search#search-instance_method
26 27 28 |
# File 'lib/chatterbot/dsl.rb', line 26 def search(*args, &block) bot.register_handler(:search, args, &block) end |
#secret(s) ⇒ Object
set the secret
452 453 454 455 |
# File 'lib/chatterbot/dsl.rb', line 452 def 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 |
#since_id(s = nil) ⇒ Object
The ID of the most recent tweet processed by the bot
426 427 428 429 430 431 |
# File 'lib/chatterbot/dsl.rb', line 426 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
468 469 470 |
# File 'lib/chatterbot/dsl.rb', line 468 def since_id_reply bot.config[:since_id_reply] end |
#token(s) ⇒ Object
set the token
460 461 462 463 |
# File 'lib/chatterbot/dsl.rb', line 460 def token(s) bot.deprecated "Setting access_token outside of your config file is deprecated!", Kernel.caller.first bot.config[:access_token] = s end |
#tweet(txt, params = {}, original = nil) ⇒ Object
send a tweet
125 126 127 |
# File 'lib/chatterbot/dsl.rb', line 125 def tweet(txt, params = {}, original = nil) bot.tweet(txt, params, original) end |
#update_config ⇒ Object
explicitly save the configuration/state of the bot.
476 477 478 |
# File 'lib/chatterbot/dsl.rb', line 476 def update_config bot.update_config end |
#use_streaming(s = nil) ⇒ Object
enable or disable usage of the Streaming API
106 107 108 109 |
# File 'lib/chatterbot/dsl.rb', line 106 def use_streaming(s=nil) s = true if s.nil? bot.streaming = s end |
#verbose(d = nil) ⇒ Object
turn on/off verbose output
276 277 278 279 |
# File 'lib/chatterbot/dsl.rb', line 276 def verbose(d=nil) d = true if d.nil? bot.verbose = d end |