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



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

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"

Parameters:

  • args (Array, String)

    list of usernames



218
219
220
221
222
223
224
225
226
# File 'lib/chatterbot/dsl.rb', line 218

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
# File 'lib/chatterbot/dsl.rb', line 110

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

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



319
320
321
# File 'lib/chatterbot/dsl.rb', line 319

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

#consumer_secret(s) ⇒ Object

set the consumer secret

Parameters:

  • s (String)

    the consumer secret



312
313
314
# File 'lib/chatterbot/dsl.rb', line 312

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



356
357
358
# File 'lib/chatterbot/dsl.rb', line 356

def db
  bot.db
end

#debug_mode(d = nil) ⇒ Object

should we send tweets?

Parameters:

  • d (Boolean) (defaults to: nil)

    true/false if we should send tweets



186
187
188
189
# File 'lib/chatterbot/dsl.rb', line 186

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"

Parameters:

  • args (Array, String)

    list of usernames



289
290
291
292
293
294
295
296
# File 'lib/chatterbot/dsl.rb', line 289

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



77
78
79
# File 'lib/chatterbot/dsl.rb', line 77

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

#follow(u) ⇒ Object

follow a user

Parameters:

  • u

    a Twitter::User or user id



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

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



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

def followers(opts={})
  bot.followers(opts)
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



195
196
197
198
# File 'lib/chatterbot/dsl.rb', line 195

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

#only_interact_with_followersObject



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

def only_interact_with_followers
  whitelist followers
end

#profile_text(p = nil) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/chatterbot/dsl.rb', line 90

def profile_text(p=nil)
  if p.nil?
    bot.profile_text
  else
    bot.profile_text(p)
  end
end

#profile_website(w = nil) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/chatterbot/dsl.rb', line 98

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


35
36
37
# File 'lib/chatterbot/dsl.rb', line 35

def replies(&block)
  bot.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



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

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



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

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

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

search twitter for the specified terms, then pass any matches to the block.

Examples:

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


22
23
24
# File 'lib/chatterbot/dsl.rb', line 22

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

#secret(s) ⇒ Object

set the secret

Parameters:

  • s (String)

    the secret



326
327
328
# File 'lib/chatterbot/dsl.rb', line 326

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

#since_id(s = nil) ⇒ Object

The ID of the most recent tweet processed by the bot



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

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



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

def since_id_reply
  bot.config[:since_id_reply]
end

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/chatterbot/dsl.rb', line 39

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



50
51
52
# File 'lib/chatterbot/dsl.rb', line 50

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

#token(s) ⇒ Object

set the token

Parameters:

  • s (String)

    the token



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

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

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

send a tweet

Parameters:



62
63
64
# File 'lib/chatterbot/dsl.rb', line 62

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

#update_configObject

explicitly save the configuration/state of the bot.



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

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



204
205
206
207
# File 'lib/chatterbot/dsl.rb', line 204

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"

Parameters:

  • args (Array, String)

    list of usernames or Twitter::User objects



237
238
239
240
241
242
243
244
245
# File 'lib/chatterbot/dsl.rb', line 237

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

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