Class: Watson::Assistant::Dialog

Inherits:
Object
  • Object
show all
Defined in:
lib/watson/assistant.rb

Instance Method Summary collapse

Constructor Details

#initialize(username: "", password: "", workspace_id: "") ⇒ Dialog

Returns a new instance of Dialog.



13
14
15
16
17
# File 'lib/watson/assistant.rb', line 13

def initialize(username: "", password: "", workspace_id: "")
  url = "https://#{username}:#{password}@gateway.watsonplatform.net/assistant/api"
  version="2018-02-16"
  @endpoint = "#{url}/v1/workspaces/#{workspace_id}/message?version=#{version}"
end

Instance Method Details

#talk(question, context) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/watson/assistant.rb', line 20

def talk(question, context)
  if context == ""
    body = {}.to_json
  else
    body = {
      input: {
        text: question
      },
      alternate_intents: true,
      context: context,
    }.to_json
  end

  begin
    response = RestClient.post @endpoint, body, content_type: :json, accept: :json
    code = response.code
    body = JSON.parse(response.body)
  rescue RestClient::ExceptionWithResponse => e
    code = e.response.code
    body = JSON.parse(e.response.body)
  end
  
  return code, body
end