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
COMMAND_LINE_VARIABLES =
[:debug_mode, :no_update, :verbose, :reset_since_id]

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


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

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"]
  }.delete_if { |k, v| v.nil? }.merge(slurp_file(config_file) || {})
end

#chatterbot_helper?Boolean

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


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

def chatterbot_helper?
  Chatterbot::from_helper == true
end

#client_paramsObject

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


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

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


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

def config_file
  dest = working_dir
  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
# 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
  COMMAND_LINE_VARIABLES.each { |k|
    tmp.delete(k)
  }
  
  # 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


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

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

#debug_mode?Boolean

are we in debug mode?


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

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?


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

def has_config?
  ! @config.nil?
end

#has_db?Boolean

do we have a DB connection string?


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

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


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

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

#load_config(params = {}) ⇒ Object

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


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

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


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

def log_dest
  config[:log_dest]
end

#log_tweets?Boolean

should we log tweets to the database?


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

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

#logging?Boolean

should we write to a log file?


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

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

#max_id_from(s) ⇒ Object


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

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?


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

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

#needs_auth_token?Boolean

has this script validated with Twitter OAuth?


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

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

#no_update=(d) ⇒ Object


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

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

#reset_bot?Boolean

should we reset the since_id for this bot?


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

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

#since_idObject

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


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

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


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

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


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

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


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

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

#slurp_file(f) ⇒ Object

load in a config file


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

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


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

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


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

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

#update_configObject

write out our config file


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

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?


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

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

#update_config_at_exitObject


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

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


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

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


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

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


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

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

#verbose?Boolean


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

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


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

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