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


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

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.

Examples:

blacklist "mean_user, private_user"

246
247
248
249
250
251
252
253
254
# File 'lib/chatterbot/dsl.rb', line 246

def blacklist(*args)
  list = flatten_list_of_strings(args)

  if list.nil? || list.empty?
    bot.blacklist = []
  else
    bot.blacklist += 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


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
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
# File 'lib/chatterbot/dsl.rb', line 138

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

#clientObject


10
11
12
# File 'lib/chatterbot/dsl.rb', line 10

def client
  bot.client
end

#consumer_key(k) ⇒ Object

set the consumer key


347
348
349
# File 'lib/chatterbot/dsl.rb', line 347

def consumer_key(k)
  bot.config[:consumer_key] = k
end

#consumer_secret(s) ⇒ Object

set the consumer secret


340
341
342
# File 'lib/chatterbot/dsl.rb', line 340

def consumer_secret(s)
  bot.config[:consumer_secret] = s
end

#dbObject

return the bot's current database connection, if available. handy if you need to manage data with your bot


384
385
386
# File 'lib/chatterbot/dsl.rb', line 384

def db
  bot.db
end

#debug_mode(d = nil) ⇒ Object

should we send tweets?


214
215
216
217
# File 'lib/chatterbot/dsl.rb', line 214

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

Examples:

exclude "spam, junk, something"

317
318
319
320
321
322
323
324
# File 'lib/chatterbot/dsl.rb', line 317

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


97
98
99
# File 'lib/chatterbot/dsl.rb', line 97

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

#follow(u) ⇒ Object

follow a user


292
293
294
# File 'lib/chatterbot/dsl.rb', line 292

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


284
285
286
# File 'lib/chatterbot/dsl.rb', line 284

def followers(opts={})
  bot.followers(opts)
end

#home_timeline(opts = {}, &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

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

def home_timeline(opts = {}, &block)
  bot.home_timeline(opts, &block)
end

#no_update(d = nil) ⇒ Object

should we update the db with a new since_id?


223
224
225
226
# File 'lib/chatterbot/dsl.rb', line 223

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

#only_interact_with_followersObject


275
276
277
# File 'lib/chatterbot/dsl.rb', line 275

def only_interact_with_followers
  whitelist followers
end

#profile_text(p = nil) ⇒ Object

handle getting/setting the profile text.


114
115
116
117
118
119
120
# File 'lib/chatterbot/dsl.rb', line 114

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


126
127
128
129
130
131
132
# File 'lib/chatterbot/dsl.rb', line 126

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

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

def replies(&block)
  bot.replies(&block)
end

#reply(txt, source) ⇒ Object

reply to a tweet


106
107
108
# File 'lib/chatterbot/dsl.rb', line 106

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

#retweet(id) ⇒ Object

retweet a tweet


89
90
91
# File 'lib/chatterbot/dsl.rb', line 89

def retweet(id)
  bot.retweet(id)
end

#search(query, opts = {}, &block) ⇒ Object

Examples:

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

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

def search(query, opts = {}, &block)
  bot.search(query, opts, &block)
end

#secret(s) ⇒ Object

set the secret


354
355
356
# File 'lib/chatterbot/dsl.rb', line 354

def secret(s)
  bot.config[:secret] = s
end

#since_id(s = nil) ⇒ Object

The ID of the most recent tweet processed by the bot


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

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


368
369
370
# File 'lib/chatterbot/dsl.rb', line 368

def since_id_reply
  bot.config[:since_id_reply]
end

#streaming(opts = {}, &block) ⇒ Object


59
60
61
62
63
64
65
66
67
68
# File 'lib/chatterbot/dsl.rb', line 59

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


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

def streaming_tweets(opts={}, &block)
  bot.streaming_tweets(opts, &block)
end

#token(s) ⇒ Object

set the token


361
362
363
# File 'lib/chatterbot/dsl.rb', line 361

def token(s)
  bot.config[:token] = s
end

#tweet(txt, params = {}, original = nil) ⇒ Object

send a tweet


82
83
84
# File 'lib/chatterbot/dsl.rb', line 82

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

#update_configObject

explicitly save the configuration/state of the bot.


376
377
378
# File 'lib/chatterbot/dsl.rb', line 376

def update_config
  bot.update_config
end

#verbose(d = nil) ⇒ Object

turn on/off verbose output


232
233
234
235
# File 'lib/chatterbot/dsl.rb', line 232

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.

Examples:

whitelist "mean_user, private_user"

265
266
267
268
269
270
271
272
273
# File 'lib/chatterbot/dsl.rb', line 265

def whitelist(*args)
  list = flatten_list_of_strings(args)

  if list.nil? || list.empty?
    bot.whitelist = []
  else
    bot.whitelist += list
  end
end