Class: Watson::Conversation

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

Direct Known Subclasses

ConversationEnhanced

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(options = {}) ⇒ Object



3
4
5
6
7
8
# File 'lib/watson/conversation.rb', line 3

def self.configure(options = {})
  @@username     = options[:username]
  @@password     = options[:password]
  @@url          = options[:url]
  @@workspace_id = options[:workspace_id]
end

Instance Method Details

#build_response(result) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/watson/conversation.rb', line 32

def build_response(result)
  {
    source: 'conversation', intent: result['intents'][0]['intent'],
    message: @message, tittle: @message, body: result['output']['text'],
    context_id: result['context']['conversation_id']
  }
end

#conversationObject



10
11
12
# File 'lib/watson/conversation.rb', line 10

def conversation
  @context['conversation_id']
end

#send_message(message = '') ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/watson/conversation.rb', line 14

def send_message(message = '')
  return if message.empty? || message.nil?
  @message = message
  params = { input: { text: @message }, alternate_intents: true }
  params.merge!(@context) unless @context.nil?
  response = Excon.post(url,
                        body:     params.to_json,
                        headers:  {
                          'Content-Type' => 'application/json',
                          'Accept' => 'application/json'
                        },
                        user:      @@username,
                        password:  @@password)
  result = JSON.parse(response.body)
  @context = { context: result['context'] }
  build_response(result)
end

#urlObject



40
41
42
# File 'lib/watson/conversation.rb', line 40

def url
  @@url + '/v1/workspaces/' + @@workspace_id + '/message?version=2016-09-20'
end