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

Instance Method Details

#bad_wordsObject

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.

Examples:

blocklist "mean_user, private_user"

Parameters:

  • args (Array, String)

    list of usernames



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

#botObject

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.banner = "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

#clientObject

Returns initialized Twitter::REST::Client.

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

Parameters:

  • k (String)

    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

Parameters:

  • s (String)

    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?

Parameters:

  • d (Boolean) (defaults to: nil)

    true/false if we should 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

Parameters:

  • txt (String)

    the text you want to tweet

  • user (User) (defaults to: nil)

    to send the DM to



165
166
167
# File 'lib/chatterbot/dsl.rb', line 165

def direct_message(txt, user=nil)
  bot.direct_message(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

Examples:

direct_messages do |dm|
  puts dm.text # this is the actual tweeted text
  direct_message "Thanks for the mention!", dm.sender
end


65
66
67
# File 'lib/chatterbot/dsl.rb', line 65

def direct_messages(&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

Examples:

exclude "spam, junk, something"

Parameters:

  • args (Array, String)

    list of usernames



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

Parameters:

  • id (id)

    A tweet or the ID of 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.

Examples:

favorited do |tweet|
  puts tweet.text # this is the actual tweeted text
  reply "@#{user.screen_name} thanks for the fave!", tweet
end


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

Parameters:

  • u

    a Twitter::User or user id



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.

Examples:

followed do |user|
  follow user
end


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

Examples:

home_timeline do |tweet|
  puts tweet.text # this is the actual tweeted text
  favorite tweet # i like to fave tweets
end


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?

Parameters:

  • d (Boolean) (defaults to: nil)

    true/false if we should update the database



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_followersObject

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.

Parameters:

  • p (p) (defaults to: nil)

    The new value for the profile. If this isn’t passed in, the method will simply return the current value

Returns:

  • 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

Parameters:

  • w (w) (defaults to: nil)

    The new value for the website. If this isn’t passed in, the method will simply return the current value

Returns:

  • 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

Examples:

replies do |tweet|
  puts tweet.text # this is the actual tweeted text
  reply "Thanks for the mention!", tweet
end


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

Parameters:

Options Hash (params):

  • :media (String, File)

    Optional file object to send with the tweet. Must be an image or video that will be accepted by Twitter. You can pass a File object, or the path to a file

See Also:



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

Parameters:

  • id (id)

    A tweet or the ID of 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.

Examples:

safelist "mean_user, private_user"

Parameters:

  • args (Array, String)

    list of usernames or Twitter::User objects



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

Examples:

search("chatterbot is cool!") do |tweet|
  puts tweet.text # this is the actual tweeted text
  reply "I agree!", tweet
end

Parameters:

  • args (Hash)

    options. these will be passed directly to



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

Parameters:

  • s (String)

    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_replyObject

get the id of the last tweet the bot replied to

Returns:

  • tweet id



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

Parameters:

  • s (String)

    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

Parameters:

  • txt (String)

    the text you want to tweet

  • params (Hash) (defaults to: {})

    options for the tweet. You can get an idea of possible values you can send here from the underlying Twitter gem docs: rdoc.info/gems/twitter/Twitter/API#update-instance_method

  • original (Tweet) (defaults to: nil)

    if this is a reply, the original tweet. this will be used for variable substitution, and for logging

Options Hash (params):

  • :media (String, File)

    Optional file object to send with the tweet. Must be an image or video that will be accepted by Twitter. You can pass a File object, or the path to a file

See Also:



125
126
127
# File 'lib/chatterbot/dsl.rb', line 125

def tweet(txt, params = {}, original = nil)
  bot.tweet(txt, params, original)
end

#update_configObject

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

Parameters:

  • d (Boolean) (defaults to: nil)

    true/false use 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