class Watson::Assistant::Manager

Public Class Methods

new(config) click to toggle source
# File lib/watson/assistant/manager.rb, line 8
def initialize(config)
  @config = config
  check = check_config()

  if check == true
    create_dialog()
    prepare_storage()
  end

end

Public Instance Methods

check_config() click to toggle source
# File lib/watson/assistant/manager.rb, line 36
def check_config()
  if @config[:apikey]
    if @config[:username] || @config[:password]
      puts "Error: 'Both API key' and 'Username/Password' are used"
      return false
    end
    @config[:auth] = "apikey:#{@config[:apikey]}"

  elsif @config[:username] && @config[:password]
    @config[:auth] = "#{@config[:username]}:#{@config[:password]}"

  else
    puts "Error: Not authorized"
    return false
  end

  return true
end
create_dialog() click to toggle source
# File lib/watson/assistant/manager.rb, line 19
def create_dialog
  @dlg = Dialog.new(
    @config
  )
end
delete(user) click to toggle source
# File lib/watson/assistant/manager.rb, line 66
def delete(user)
  @users.delete(user)
end
delete_context_section(user:, key:) click to toggle source
# File lib/watson/assistant/manager.rb, line 116
def delete_context_section(user:, key:)
  context = @users.fetch(user)
  context.delete(key)
  @users.store(user, context)
                          return context
end
has_dlg?() click to toggle source
# File lib/watson/assistant/manager.rb, line 56
def has_dlg?
  return @dlg
end
has_key?(user) click to toggle source
# File lib/watson/assistant/manager.rb, line 61
def has_key?(user) 
  @users.has_key?(user)
end
prepare_storage() click to toggle source
# File lib/watson/assistant/manager.rb, line 26
def prepare_storage
  storage = @config[:storage] || "hash"
  if storage == "hash"
    @users = Hash.new
  else
    @users = Redis.new(:url => storage)
  end
end
read_context(user:) click to toggle source
# File lib/watson/assistant/manager.rb, line 97
def read_context(user:)
  context = @users.fetch(user)
end
read_context_section(user:, key:) click to toggle source
# File lib/watson/assistant/manager.rb, line 102
def read_context_section(user:, key:)
  context = @users.fetch(user)
  return context[key]
end
talk(user, question) click to toggle source
# File lib/watson/assistant/manager.rb, line 71
def talk(user, question)
  future_data = nil

  if @users.has_key?(user) == false
    code, body = @dlg.talk("", "")
  else
    code, body = @dlg.talk(question, context = @users.fetch(user))
  end

  if code == 200
    context = body["context"]
    output = body["output"]["text"]
  else
    output = body["error"]
  end

  if code == 200
    @users.store(user, context)
  else
    @users.delete(user)
  end

  return {user: user, status_code: code, output: output}.to_json
end
update_context_section(user:, key:, value:) click to toggle source
# File lib/watson/assistant/manager.rb, line 108
def update_context_section(user:, key:, value:)
  context = @users.fetch(user)
  context[key] = value
  @users.store(user, context)
                          return context
end