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
-
#bad_words ⇒ Object
a common list of bad words, which you might want to filter out.
-
#blocklist(*args) ⇒ Object
(also: #blacklist)
specify a bot-specific blocklist of users.
-
#bot ⇒ Object
generate a Bot object.
-
#client ⇒ Object
Initialized Twitter::REST::Client.
-
#consumer_key(k) ⇒ Object
set the consumer key.
-
#consumer_secret(s) ⇒ Object
set the consumer secret.
-
#debug_mode(d = nil) ⇒ Object
should we send tweets?.
-
#direct_message(txt, user = nil) ⇒ Object
send a direct message to the specified user.
-
#direct_messages(&block) ⇒ Object
handle direct messages sent to the bot.
-
#exclude(*args) ⇒ Object
specify list of strings we will check when deciding to respond to a tweet or not.
-
#favorite(id) ⇒ Object
favorite a tweet.
-
#follow(u) ⇒ Object
follow a user.
-
#followers(opts = {}) ⇒ Object
return a list of users following the bot.
-
#home_timeline(&block) ⇒ Object
handle tweets that are on the bot’s home timeline.
-
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?.
-
#only_interact_with_followers ⇒ Object
specify that the bot should only reply to tweets from users that are followers, basically making interactions opt-in.
-
#profile_text(p = nil) ⇒ Object
handle getting/setting the profile text.
-
#profile_website(w = nil) ⇒ Object
handle getting/setting the profile website.
-
#replies(&block) ⇒ Object
handle replies to the bot.
-
#reply(txt, source, params = {}) ⇒ Object
reply to a tweet.
-
#retweet(id) ⇒ Object
retweet a tweet.
-
#safelist(*args) ⇒ Object
(also: #whitelist)
specify a bot-specific safelist of users.
-
#search(*args, &block) ⇒ Object
search twitter for the specified terms, then pass any matches to the block.
-
#secret(s) ⇒ Object
set the secret.
-
#since_id(s = nil) ⇒ Object
The ID of the most recent tweet processed by the bot.
-
#since_id_reply ⇒ Object
get the id of the last tweet the bot replied to.
-
#token(s) ⇒ Object
set the token.
-
#tweet(txt, params = {}, original = nil) ⇒ Object
send a tweet.
-
#update_config ⇒ Object
explicitly save the configuration/state of the bot.
-
#verbose(d = nil) ⇒ Object
turn on/off verbose output.
Instance Method Details
#bad_words ⇒ Object
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.
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 |
#bot ⇒ Object
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. = "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 |
#client ⇒ Object
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
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
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?
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
126 127 128 |
# File 'lib/chatterbot/dsl.rb', line 126 def (txt, user=nil) bot.(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
69 70 71 |
# File 'lib/chatterbot/dsl.rb', line 69 def (&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
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
101 102 103 |
# File 'lib/chatterbot/dsl.rb', line 101 def favorite(id) bot.favorite(id) end |
#follow(u) ⇒ Object
follow a user
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
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?
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_followers ⇒ Object
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.
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
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
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
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
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.
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
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
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_reply ⇒ Object
get the id of the last tweet the bot replied to
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
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
86 87 88 |
# File 'lib/chatterbot/dsl.rb', line 86 def tweet(txt, params = {}, original = nil) bot.tweet(txt, params, original) end |
#update_config ⇒ Object
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
237 238 239 240 |
# File 'lib/chatterbot/dsl.rb', line 237 def verbose(d=nil) d = true if d.nil? bot.verbose = d end |