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.
-
#blacklist(*args) ⇒ Object
specify a bot-specific blacklist 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.
-
#db ⇒ Object
return the bot’s current database connection, if available.
-
#debug_mode(d = nil) ⇒ Object
should we send tweets?.
-
#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.
-
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?.
- #profile_text(p = nil) ⇒ Object
- #profile_website(w = nil) ⇒ Object
-
#replies(&block) ⇒ Object
handle replies to the bot.
-
#reply(txt, source) ⇒ Object
reply to a tweet.
-
#retweet(id) ⇒ Object
retweet a tweet.
-
#search(query, opts = {}, &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.
- #streaming(opts = {}, &block) ⇒ Object
- #streaming_tweets(opts = {}, &block) ⇒ Object
-
#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
251 252 253 254 255 256 |
# File 'lib/chatterbot/dsl.rb', line 251 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.
219 220 221 222 223 224 225 226 227 |
# File 'lib/chatterbot/dsl.rb', line 219 def blacklist(*args) list = flatten_list_of_strings(args) if list.nil? || list.empty? bot.blacklist = [] else bot.blacklist += 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
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 181 |
# File 'lib/chatterbot/dsl.rb', line 110 def bot puts @bot.inspect return @bot unless @bot.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('-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 |
#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
297 298 299 |
# File 'lib/chatterbot/dsl.rb', line 297 def consumer_key(k) bot.config[:consumer_key] = k end |
#consumer_secret(s) ⇒ Object
set the consumer secret
290 291 292 |
# File 'lib/chatterbot/dsl.rb', line 290 def consumer_secret(s) bot.config[:consumer_secret] = s end |
#db ⇒ Object
return the bot’s current database connection, if available. handy if you need to manage data with your bot
334 335 336 |
# File 'lib/chatterbot/dsl.rb', line 334 def db bot.db end |
#debug_mode(d = nil) ⇒ Object
should we send tweets?
187 188 189 190 |
# File 'lib/chatterbot/dsl.rb', line 187 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
267 268 269 270 271 272 273 274 |
# File 'lib/chatterbot/dsl.rb', line 267 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
77 78 79 |
# File 'lib/chatterbot/dsl.rb', line 77 def favorite(id) bot.favorite(id) end |
#follow(u) ⇒ Object
follow a user
242 243 244 |
# File 'lib/chatterbot/dsl.rb', line 242 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
234 235 236 |
# File 'lib/chatterbot/dsl.rb', line 234 def followers(opts={}) bot.followers(opts) end |
#no_update(d = nil) ⇒ Object
should we update the db with a new since_id?
196 197 198 199 |
# File 'lib/chatterbot/dsl.rb', line 196 def no_update(d=nil) d = true if d.nil? bot.no_update = d 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
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
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
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.
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
304 305 306 |
# File 'lib/chatterbot/dsl.rb', line 304 def secret(s) bot.config[:secret] = s end |
#since_id(s = nil) ⇒ Object
The ID of the most recent tweet processed by the bot
280 281 282 283 284 285 |
# File 'lib/chatterbot/dsl.rb', line 280 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
318 319 320 |
# File 'lib/chatterbot/dsl.rb', line 318 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
311 312 313 |
# File 'lib/chatterbot/dsl.rb', line 311 def token(s) bot.config[:token] = s end |
#tweet(txt, params = {}, original = nil) ⇒ Object
send a tweet
62 63 64 |
# File 'lib/chatterbot/dsl.rb', line 62 def tweet(txt, params = {}, original = nil) bot.tweet(txt, params, original) end |
#update_config ⇒ Object
explicitly save the configuration/state of the bot.
326 327 328 |
# File 'lib/chatterbot/dsl.rb', line 326 def update_config bot.update_config end |
#verbose(d = nil) ⇒ Object
turn on/off verbose output
205 206 207 208 |
# File 'lib/chatterbot/dsl.rb', line 205 def verbose(d=nil) d = true if d.nil? bot.verbose = d end |