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



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/chatterbot/dsl.rb', line 313

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



251
252
253
254
255
256
257
258
259
# File 'lib/chatterbot/dsl.rb', line 251

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
# File 'lib/chatterbot/dsl.rb', line 159

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



405
406
407
408
# File 'lib/chatterbot/dsl.rb', line 405

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



397
398
399
400
# File 'lib/chatterbot/dsl.rb', line 397

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



219
220
221
222
# File 'lib/chatterbot/dsl.rb', line 219

def debug_mode(d=nil)
  d = true if d.nil?
  bot.debug_mode = d
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



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

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


69
70
71
# File 'lib/chatterbot/dsl.rb', line 69

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



374
375
376
377
378
379
380
381
# File 'lib/chatterbot/dsl.rb', line 374

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



101
102
103
# File 'lib/chatterbot/dsl.rb', line 101

def favorite(id)
  bot.favorite(id)
end

#follow(u) ⇒ Object

follow a user

Parameters:

  • u

    a Twitter::User or user id



304
305
306
# File 'lib/chatterbot/dsl.rb', line 304

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



296
297
298
# File 'lib/chatterbot/dsl.rb', line 296

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


43
44
45
# File 'lib/chatterbot/dsl.rb', line 43

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



228
229
230
231
# File 'lib/chatterbot/dsl.rb', line 228

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



287
288
289
# File 'lib/chatterbot/dsl.rb', line 287

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



135
136
137
138
139
140
141
# File 'lib/chatterbot/dsl.rb', line 135

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



147
148
149
150
151
152
153
# File 'lib/chatterbot/dsl.rb', line 147

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


56
57
58
# File 'lib/chatterbot/dsl.rb', line 56

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:



117
118
119
# File 'lib/chatterbot/dsl.rb', line 117

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



93
94
95
# File 'lib/chatterbot/dsl.rb', line 93

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



272
273
274
275
276
277
278
279
280
# File 'lib/chatterbot/dsl.rb', line 272

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. NOTE: by default, search terms are wrapped in quotes so the Twitter API will return tweets that include your exact query. You can disable this by passing exact:false as an option

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



30
31
32
# File 'lib/chatterbot/dsl.rb', line 30

def search(*args, &block)
  bot.register_handler(:search, args, &block)
end

#secret(s) ⇒ Object

set the secret

Parameters:

  • s (String)

    the secret



413
414
415
416
# File 'lib/chatterbot/dsl.rb', line 413

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



387
388
389
390
391
392
# File 'lib/chatterbot/dsl.rb', line 387

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



429
430
431
# File 'lib/chatterbot/dsl.rb', line 429

def since_id_reply
  bot.config[:since_id_reply]
end

#token(s) ⇒ Object

set the token

Parameters:

  • s (String)

    the token



421
422
423
424
# File 'lib/chatterbot/dsl.rb', line 421

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:



86
87
88
# File 'lib/chatterbot/dsl.rb', line 86

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

#update_configObject

explicitly save the configuration/state of the bot.



437
438
439
# File 'lib/chatterbot/dsl.rb', line 437

def update_config
  bot.update_config
end

#verbose(d = nil) ⇒ Object

turn on/off verbose output

Parameters:

  • d (Boolean) (defaults to: nil)

    true/false use verbose output



237
238
239
240
# File 'lib/chatterbot/dsl.rb', line 237

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