class Hutzbot::Hutzbot
Public Class Methods
new(logger, host, user, key, application)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 76 def initialize logger, host, user, key, application @logger = logger @host = host @user = user @key = key @application = application @options = { headers: { 'X-User-Email' => @user, 'X-User-Token' => @key, 'Accept' => 'application/json' } } end
Public Instance Methods
add_response(conversation_id, answer)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 115 def add_response conversation_id, answer response = HTTParty.patch("#{@host}/conversations/#{conversation_id}/add_response", @options.merge({ query: { answer: answer } })) check_errors response Conversation.new(JSON.parse(response.body)) end
converse(metadata, body)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 139 def converse metadata, body if metadata.nil? || metadata.empty? # start a conversation via the API logger.info 'Starting a new conversation' conversation = start_conversation logger.info "Conversation ID: #{conversation.id}" else logger.info "Finding a conversation by metadata (#{metadata})" # find the conversation by metadata conversations = get_conversations metadata unless conversations.empty? # found a matching conversation conversation = conversations.first logger.info "Found conversation (#{conversation.id})" # now get the full conversation (more attributes than available in index call) conversation = get_conversation conversation.id # get the answer index matches = /(-?\d+)/.match(body) if matches.nil? # invalid answer logger.warn("No answer found in body (#{body})") else answer_index = matches[1].to_i logger.info("Found an answer in the message with index #{answer_index} in (#{conversation.valid_responses})") matching_answer = conversation.valid_responses[answer_index-1] logger.info("That answer is #{matching_answer.answer} (#{matching_answer.response})") conversation = add_response conversation.id, matching_answer.answer end end end if conversation logger.info "Preparing response" responses = conversation.responses.select{ |r| r.system? && (r.metadata.nil? || r.metadata.empty?) } contents = responses.map do |r| "<p>#{r.statement}</p>" end.join("<br>") logger.debug "Response is: #{contents}" logger.info "Preparing prompt" prompt = nil unless conversation.closed? prompt = conversation.valid_responses.map do |valid_response| "<li>#{valid_response.response}</li>" end.join prompt = "<p>Please make your selection: </p><ol>#{prompt}</ol>" end logger.debug "Prompt is: #{prompt}" return conversation, contents, prompt else return nil, "" end end
create_conversation(params)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 133 def create_conversation params response = HTTParty.post("#{@host}/conversations", @options.merge({ query: { conversation: params.merge({ application: @application }) } })) check_errors response Conversation.new(JSON.parse(response.body)) end
get_conversation(id)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 103 def get_conversation id response = HTTParty.get("#{@host}/conversations/#{id}", @options) check_errors response Conversation.new(JSON.parse(response.body)) end
get_conversations(metadata=nil)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 95 def get_conversations metadata=nil request = [@host, 'conversations'].join('/') request += "?metadata=#{metadata}" unless metadata.nil? response = HTTParty.get(request, @options) check_errors response JSON.parse(response.body).map{ |c| Conversation.new(c) } end
get_default_tagged_document()
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 127 def get_default_tagged_document response = HTTParty.get("#{@host}/tagged_documents/default", @options) check_errors response TaggedDocument.new(JSON.parse(response.body)) end
get_default_taxonomy()
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 121 def get_default_taxonomy response = HTTParty.get("#{@host}/taxonomies/default", @options) check_errors response Taxonomy.new(JSON.parse(response.body)) end
logger()
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 85 def logger @logger end
start_conversation()
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 89 def start_conversation taxonomy = get_default_taxonomy tagged_document = get_default_tagged_document conversation = create_conversation({ taxonomy_id: taxonomy.id, tagged_document_id: tagged_document.id, application: @application }) end
tag_responses(conversation_id, metadata)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 109 def tag_responses conversation_id, metadata response = HTTParty.patch("#{@host}/conversations/#{conversation_id}/tag_responses", @options.merge({ query: { metadata: metadata } })) check_errors response Conversation.new(JSON.parse(response.body)) end
Private Instance Methods
check_errors(response)
click to toggle source
# File lib/hutzbot/hutzbot.rb, line 199 def check_errors response case response.code when 200..299 "Success" when 400..499 raise APIError, "HutzbotAPI Error: #{response.code}" else raise APIError, "HutzbotAPI Error: #{response.code}" end end