Module: Chatterbot::Config

Included in:
Bot
Defined in:
lib/chatterbot/config.rb

Overview

routines for storing config information for the bot

Constant Summary collapse

MAX_TWEET_ID =
9223372036854775807

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

the entire config for the bot, loaded from YAML files and the DB if applicable



12
13
14
# File 'lib/chatterbot/config.rb', line 12

def config
  @config
end

Instance Method Details

#bot_configObject

bot-specific config settings



262
263
264
265
266
267
268
269
# File 'lib/chatterbot/config.rb', line 262

def bot_config
  {
    :consumer_key => ENV["chatterbot_consumer_key"],
    :consumer_secret => ENV["chatterbot_consumer_secret"],
    :token => ENV["chatterbot_token"],
    :secret => ENV["chatterbot_secret"]
  }.merge(slurp_file(config_file) || {})
end

#chatterbot_helper?Boolean

determine if we’re being called by one of our internal scripts

Returns:

  • (Boolean)


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

def chatterbot_helper?
  Chatterbot::from_helper == true
end

#client_paramsObject

return a hash of the params we need to connect to the Twitter API



171
172
173
174
175
176
177
178
# File 'lib/chatterbot/config.rb', line 171

def client_params
  { 
    :consumer_key => config[:consumer_key],
    :consumer_secret => config[:consumer_secret],
    :token => config[:token].nil? ? nil : config[:token],
    :secret => config[:secret].nil? ? nil : config[:secret]
  }
end

#config_fileObject

figure out what config file to load based on the name of the bot



215
216
217
218
# File 'lib/chatterbot/config.rb', line 215

def config_file
  dest = working_dir
  x = File.join(File.expand_path(dest), "#{botname}.yml")
end

#config_to_saveObject

figure out what we should save to the local config file. we don’t save anything that exists in the global config, unless it’s been modified for this particular bot.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/chatterbot/config.rb', line 282

def config_to_save
  # remove keys that are duped in the global config
  tmp = config.delete_if { |k, v| global_config.has_key?(k) && global_config[k] == config[k] }

  # let's not store these, they're just command-line options
  tmp.delete(:debug_mode)
  tmp.delete(:no_update)
  tmp.delete(:verbose)      

  
  # update the since_id now
  tmp[:since_id] = tmp.delete(:tmp_since_id) unless ! tmp.has_key?(:tmp_since_id)
  tmp[:since_id_reply] = tmp.delete(:tmp_since_id_reply) unless ! tmp.has_key?(:tmp_since_id_reply)

  tmp
end

#db_configObject

load the config settings from the db, if possible



273
274
275
276
# File 'lib/chatterbot/config.rb', line 273

def db_config
  return {} if db.nil?
  db[:config][:id => botname]
end

#debug_mode=(d) ⇒ Object



40
41
42
# File 'lib/chatterbot/config.rb', line 40

def debug_mode=(d)
  config[:debug_mode] = d
end

#debug_mode?Boolean

are we in debug mode?

Returns:

  • (Boolean)


57
58
59
# File 'lib/chatterbot/config.rb', line 57

def debug_mode?
  config[:debug_mode] || false
end

#global_configObject

get any config from our global config files



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

def global_config
  tmp = {}
  global_config_files.each { |f|
    tmp.merge!(slurp_file(f) || {})      
  }
  tmp
end

#global_config_filesObject

our list of “global config files”



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/chatterbot/config.rb', line 237

def global_config_files
  [
   # a system-wide global path
   "/etc/chatterbot.yml",
   
   # a file specified in ENV
   ENV["chatterbot_config"],
   
   # 'global' config file local to the path of the ruby script
   File.join(working_dir, "global.yml")
  ].compact
end

#has_config?Boolean

has the config been loaded yet?

Returns:

  • (Boolean)


18
19
20
# File 'lib/chatterbot/config.rb', line 18

def has_config?
  ! @config.nil?
end

#has_db?Boolean

do we have a DB connection string?

Returns:

  • (Boolean)


36
37
38
# File 'lib/chatterbot/config.rb', line 36

def has_db?
  has_sequel? && config.has_key?(:db_uri)
end

#has_sequel?Boolean

Check to see if Sequel was loaded successfully. If not, we won’t make any DB calls

Returns:

  • (Boolean)


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

def has_sequel?
  ! defined?(Sequel).nil?
end

#load_config(params = {}) ⇒ Object

load in the config from the assortment of places it can be specified.



301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/chatterbot/config.rb', line 301

def load_config(params={})
  # load the flat-files first
  @config  = global_config.merge(bot_config)
  @config[:db_uri] ||= ENV["chatterbot_db"] unless ENV["chatterbot_db"].nil?

  # if we have a key to load from the DB, do that now
  if @config.has_key?(:db_uri) && @config[:db_uri]
    tmp = db_config
    @config = @config.merge(tmp) unless tmp.nil?
  end
  @config.merge(params)
end

#log_destObject

destination for log entries



83
84
85
# File 'lib/chatterbot/config.rb', line 83

def log_dest
  config[:log_dest]
end

#log_tweets?Boolean

should we log tweets to the database?

Returns:

  • (Boolean)


24
25
26
# File 'lib/chatterbot/config.rb', line 24

def log_tweets?
  config.has_key?(:db_uri)
end

#logging?Boolean

should we write to a log file?

Returns:

  • (Boolean)


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

def logging?
  has_config? && config.has_key?(:log_dest)
end

#max_id_from(s) ⇒ Object



131
132
133
134
135
136
# File 'lib/chatterbot/config.rb', line 131

def max_id_from(s)
  # don't use max_id if it's this ridiculous number
  # @see https://dev.twitter.com/issues/1300
  sorted = s.reject { |t| !t || t.id == MAX_TWEET_ID }.max { |a, b| a.id <=> b.id }
  sorted && sorted.id
end

#needs_api_key?Boolean

do we have an API key specified?

Returns:

  • (Boolean)


182
183
184
# File 'lib/chatterbot/config.rb', line 182

def needs_api_key?
  config[:consumer_key].nil? || config[:consumer_secret].nil?
end

#needs_auth_token?Boolean

has this script validated with Twitter OAuth?

Returns:

  • (Boolean)


188
189
190
# File 'lib/chatterbot/config.rb', line 188

def needs_auth_token?
  config[:token].nil?
end

#no_update=(d) ⇒ Object



44
45
46
# File 'lib/chatterbot/config.rb', line 44

def no_update=(d)
  config[:no_update] = d
end

#reset_bot?Boolean

should we reset the since_id for this bot?

Returns:

  • (Boolean)


51
52
53
# File 'lib/chatterbot/config.rb', line 51

def reset_bot?
  config[:reset_since_id] || false
end

#since_idObject

return the ID of the most recent tweet pulled up in searches



96
97
98
# File 'lib/chatterbot/config.rb', line 96

def since_id
  config[:since_id] || 0
end

#since_id=(x) ⇒ Object

store since_id to a different key so that it doesn’t actually get updated until the bot is done running



90
91
92
# File 'lib/chatterbot/config.rb', line 90

def since_id=(x)
  config[:tmp_since_id] = x
end

#since_id_replyObject

return the ID of the most recent tweet pulled up in mentions or since_id if since_id_reply is nil



109
110
111
# File 'lib/chatterbot/config.rb', line 109

def since_id_reply
  config[:since_id_reply] || since_id
end

#since_id_reply=(x) ⇒ Object

store since_id_reply to a different key so that it doesn’t actually get updated until the bot is done running



103
104
105
# File 'lib/chatterbot/config.rb', line 103

def since_id_reply=(x)
  config[:tmp_since_id_reply] = x
end

#slurp_file(f) ⇒ Object

load in a config file



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/chatterbot/config.rb', line 222

def slurp_file(f)
  f = File.expand_path(f)

  tmp = {}

  if File.exist?(f)
    File.open( f ) { |yf| 
      tmp = YAML::load( yf ) 
    }
  end
  tmp.symbolize_keys! unless tmp == false
end

#store_database_configObject

store config settings in the database, if possible



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

def store_database_config
  return false if db.nil?

  configs = db[:config]
  data = {
    :since_id => config.has_key?(:tmp_since_id) ? config[:tmp_since_id] : config[:since_id],
    :since_id_reply => config.has_key?(:tmp_since_id_reply) ? config[:tmp_since_id_reply] : config[:since_id_reply],
    :token => config[:token],
    :secret => config[:secret],
    :consumer_secret => config[:consumer_secret],
    :consumer_key => config[:consumer_key],
    :updated_at => Time.now #:NOW.sql_function
  }

  row = configs.filter('id = ?', botname)

  if row.count > 0
    row.update(data)
  else
    data[:id] = botname
    data[:created_at] = Time.now #:NOW.sql_function
    configs.insert data
  end
  
  true
end

#store_local_configObject

write out the config file for this bot



316
317
318
# File 'lib/chatterbot/config.rb', line 316

def store_local_config
  File.open(config_file, 'w') { |f| YAML.dump(config_to_save, f) }
end

#update_configObject

write out our config file



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chatterbot/config.rb', line 115

def update_config
  return if ! update_config?

  # don't update flat file if we can store to the DB instead
  if has_db?
    debug "storing config to database -- you don't need local file anymore"
    store_database_config
  else
    store_local_config
  end
end

#update_config?Boolean

Should we run any config updates?

Returns:

  • (Boolean)


63
64
65
# File 'lib/chatterbot/config.rb', line 63

def update_config?
  config.has_key?(:no_update) ? ! config[:no_update] : true
end

#update_config_at_exitObject



127
128
129
# File 'lib/chatterbot/config.rb', line 127

def update_config_at_exit
  update_config
end

#update_since_id(search) ⇒ Object

update the since_id with either the highest ID of the specified tweets, unless it is lower than what we have already



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chatterbot/config.rb', line 153

def update_since_id(search)
  return if search.nil?
 
  tmp_id = if search.is_a?(Twitter::SearchResults)
             search.attrs[:search_metadata][:max_id]
           elsif search.respond_to?(:max)
             max_id_from(search)
           elsif search.is_a?(Twitter::Tweet)
             search.id
           else
             search
           end.to_i
  
  config[:tmp_since_id] = [config[:tmp_since_id].to_i, tmp_id].max
end

#update_since_id_reply(tweet) ⇒ Object

update the since_id_reply with the id of the given tweet, unless it is lower thant what we have already



142
143
144
145
146
147
148
# File 'lib/chatterbot/config.rb', line 142

def update_since_id_reply(tweet)
  return if tweet.nil? or tweet.class != Twitter::Tweet || tweet.id == MAX_TWEET_ID

  tmp_id = tweet.id

  config[:tmp_since_id_reply] = [config[:tmp_since_id_reply].to_i, tmp_id].max
end

#verbose=(v) ⇒ Object



73
74
75
# File 'lib/chatterbot/config.rb', line 73

def verbose=(v)
  config[:verbose] = v
end

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  config[:verbose] || false
end

#working_dirObject

if we are called by a bot, we want to use the directory of that script. If we are called by chatterbot-register or another helper script, we want to use the current working directory



204
205
206
207
208
209
210
211
# File 'lib/chatterbot/config.rb', line 204

def working_dir
  if chatterbot_helper?
    Dir.getwd
  else
    File.dirname($0)
    #Dir.pwd
  end
end