Module: Sapcai::Converse

Included in:
Request
Defined in:
lib/sapcai/apis/request/converse.rb

Instance Method Summary collapse

Instance Method Details

#converse_file(file, token: nil, language: nil, conversation_token: nil, memory: nil) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sapcai/apis/request/converse.rb', line 30

def converse_file(file, token: nil, language: nil, conversation_token: nil, memory: nil)
  token ||= @token
  raise SapcaiError.new('Token is missing') if token.nil?

  language ||= @language

  body = { voice: File.new(file) }
  body[:language] = language unless language.nil?
  body[:conversation_token] = conversation_token unless conversation_token.nil?
  body[:memory] = memory unless memory.nil?
  response = HTTMultiParty.post(
    Utils::CONVERSE_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{token}" }
  )
  raise SapcaiError.new(JSON.parse(response.body)['message']) if response.code != 200

  Conversation.new(response.body, token)
end

#converse_text(text, token: nil, language: nil, conversation_token: nil, memory: nil) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sapcai/apis/request/converse.rb', line 9

def converse_text(text, token: nil, language: nil, conversation_token: nil, memory: nil)
  token ||= @token
  raise SapcaiError.new('Token is missing') if token.nil?

  language ||= @language

  body = { text: text }
  body[:language] = language unless language.nil?
  body[:conversation_token] = conversation_token unless conversation_token.nil?
  body[:memory] = memory unless memory.nil?

  response = HTTParty.post(
    Utils::CONVERSE_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{token}" }
  )
  raise SapcaiError.new(JSON.parse(response.body)['message']) if response.code != 200

  Conversation.new(response.body, token)
end