Class: RecastAI::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/recastai/apis/build/build.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, language = nil) ⇒ Build

Returns a new instance of Build.



13
14
15
16
# File 'lib/recastai/apis/build/build.rb', line 13

def initialize(token = nil, language = nil)
  @token = token
  @language = language
end

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



11
12
13
# File 'lib/recastai/apis/build/build.rb', line 11

def language
  @language
end

#tokenObject (readonly)

Returns the value of attribute token.



11
12
13
# File 'lib/recastai/apis/build/build.rb', line 11

def token
  @token
end

Instance Method Details

#delete_conversation(user, bot, conversation_id) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/recastai/apis/build/build.rb', line 66

def delete_conversation(user, bot, conversation_id)
  raise RecastAI::RecastError.new('Token is missing') unless @token

  url = "#{RecastAI::Utils::BUILD_ENDPOINT}/users/#{user}/bots/#{bot}/builders/v1/conversation_states/#{conversation_id}"
  response = HTTParty.delete(url, headers: self.headers)
  raise RecastAI::RecastError.new(JSON.parse(response.body)['message']) if response.code != 204

  true
end

#dialog(msg, conversation_id, language = nil, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/recastai/apis/build/build.rb', line 22

def dialog(msg, conversation_id, language = nil, options = {})
  raise RecastAI::RecastError.new('Token is missing') unless @token

  log_level = options[:log_level] || "info"
  proxy = options[:proxy] || {}

  language = @language if language.nil?
  body = { message: msg, conversation_id: conversation_id, language: language, log_level: log_level}
  body[:memory] = options[:memory] unless options[:memory].nil?

  options = { body: body.to_json, headers: self.headers }
  if proxy != {}
    options[:http_proxyaddr] = proxy[:host]
    options[:http_proxyport] = proxy[:port]
  end
  response = HTTParty.post("#{RecastAI::Utils::BUILD_ENDPOINT}/dialog", options)
  raise RecastAI::RecastError.new(JSON.parse(response.body)['message']) if response.code != 200

  res = JSON.parse(response.body)['results']
  RecastAI::DialogResponse.new(res['messages'], res['conversation'], res['nlp'], res['logs'])
end

#get_conversation(user, bot, conversation_id) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/recastai/apis/build/build.rb', line 56

def get_conversation(user, bot, conversation_id)
  raise RecastAI::RecastError.new('Token is missing') unless @token

  url = "#{RecastAI::Utils::BUILD_ENDPOINT}/users/#{user}/bots/#{bot}/builders/v1/conversation_states/#{conversation_id}"
  response = HTTParty.get(url, headers: self.headers)
  raise RecastAI::RecastError.new(JSON.parse(response.body)['message']) if response.code != 200

  RecastAI::DialogConversation.new(JSON.parse(response.body)['results'])
end

#headersObject



18
19
20
# File 'lib/recastai/apis/build/build.rb', line 18

def headers
  { 'Authorization' => "Token #{@token}", 'Content-Type' => 'application/json' }
end

#update_conversation(user, bot, conversation_id, opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/recastai/apis/build/build.rb', line 44

def update_conversation(user, bot, conversation_id, opts)
  raise RecastAI::RecastError.new('Token is missing') unless @token

  body = opts

  url = "#{RecastAI::Utils::BUILD_ENDPOINT}/users/#{user}/bots/#{bot}/builders/v1/conversation_states/#{conversation_id}"
  response = HTTParty.put(url, body: body.to_json, headers: self.headers)
  raise RecastAI::RecastError.new(JSON.parse(response.body)['message']) if response.code != 200

  RecastAI::DialogConversation.new(JSON.parse(response.body)['results'])
end