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



340
341
342
343
344
345
346
347
348
349
350
351
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
# File 'lib/chatterbot/dsl.rb', line 340

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



278
279
280
281
282
283
284
285
286
# File 'lib/chatterbot/dsl.rb', line 278

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



186
187
188
189
190
191
192
193
194
195
196
197
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
# File 'lib/chatterbot/dsl.rb', line 186

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



432
433
434
435
# File 'lib/chatterbot/dsl.rb', line 432

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



424
425
426
427
# File 'lib/chatterbot/dsl.rb', line 424

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



246
247
248
249
# File 'lib/chatterbot/dsl.rb', line 246

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



153
154
155
# File 'lib/chatterbot/dsl.rb', line 153

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



401
402
403
404
405
406
407
408
# File 'lib/chatterbot/dsl.rb', line 401

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



135
136
137
# File 'lib/chatterbot/dsl.rb', line 135

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



331
332
333
# File 'lib/chatterbot/dsl.rb', line 331

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



323
324
325
# File 'lib/chatterbot/dsl.rb', line 323

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



255
256
257
258
# File 'lib/chatterbot/dsl.rb', line 255

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



314
315
316
# File 'lib/chatterbot/dsl.rb', line 314

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



162
163
164
165
166
167
168
# File 'lib/chatterbot/dsl.rb', line 162

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



174
175
176
177
178
179
180
# File 'lib/chatterbot/dsl.rb', line 174

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) ⇒ Object

reply to a tweet

Parameters:

  • txt (String)

    the text you want to tweet

  • source (Tweet)

    the original tweet you are replying to



144
145
146
# File 'lib/chatterbot/dsl.rb', line 144

def reply(txt, source)
  bot.reply(txt, source)
end

#retweet(id) ⇒ Object

retweet a tweet

Parameters:

  • id (id)

    A tweet or the ID of a tweet



127
128
129
# File 'lib/chatterbot/dsl.rb', line 127

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



299
300
301
302
303
304
305
306
307
# File 'lib/chatterbot/dsl.rb', line 299

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



440
441
442
443
# File 'lib/chatterbot/dsl.rb', line 440

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



414
415
416
417
418
419
# File 'lib/chatterbot/dsl.rb', line 414

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



456
457
458
# File 'lib/chatterbot/dsl.rb', line 456

def since_id_reply
  bot.config[:since_id_reply]
end

#token(s) ⇒ Object

set the token

Parameters:

  • s (String)

    the token



448
449
450
451
# File 'lib/chatterbot/dsl.rb', line 448

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:



120
121
122
# File 'lib/chatterbot/dsl.rb', line 120

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

#update_configObject

explicitly save the configuration/state of the bot.



464
465
466
# File 'lib/chatterbot/dsl.rb', line 464

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



264
265
266
267
# File 'lib/chatterbot/dsl.rb', line 264

def verbose(d=nil)
  d = true if d.nil?
  bot.verbose = d
end