Class: ChatbaseAPIClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/chatbase_api_client.rb

Overview

Client used to send metrics to Chatbase, when ENV var present

Constant Summary collapse

BASE_PATH =
'https://chatbase.com/api/facebook'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ChatbaseAPIClient



11
12
13
14
15
# File 'lib/chatbase_api_client.rb', line 11

def initialize(params = {})
  @intent = params.fetch(:intent, nil)
  @text = params.fetch(:text, nil)
  @not_handled = params.fetch(:not_handled, false)
end

Instance Attribute Details

#intentObject

Returns the value of attribute intent.



8
9
10
# File 'lib/chatbase_api_client.rb', line 8

def intent
  @intent
end

#not_handledObject

Returns the value of attribute not_handled.



8
9
10
# File 'lib/chatbase_api_client.rb', line 8

def not_handled
  @not_handled
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/chatbase_api_client.rb', line 8

def text
  @text
end

Instance Method Details

#send_bot_message(message, response)

This method returns an undefined value.

Sends message sent by bot to Chatbase

Parameters:

  • message (String, Hash)

    Message sent

  • response (Hash)

    Hash of response data from Facebook



40
41
42
43
44
45
46
47
# File 'lib/chatbase_api_client.rb', line 40

def send_bot_message(message, response)
  # Format for Chatbase Facebook API
  message_body = serialize_bot_message(message, response)
  # Post to Chatbase
  catch_errors{
    self.class.post("#{BASE_PATH}/send_message?api_key=#{ENV['CHATBASE_API_KEY']}", json_body(message_body))
  }
end

#send_user_message(message)

This method returns an undefined value.

Sends message sent by user to Chatbase

Parameters:

  • message (String)

    Text of user message



23
24
25
26
27
28
29
30
# File 'lib/chatbase_api_client.rb', line 23

def send_user_message(message)
  # Format for Chatbase Facebook API
  message_received = serialize_user_message(message)
  # Post to Chatbase
  catch_errors{
    self.class.post("#{BASE_PATH}/message_received?api_key=#{ENV['CHATBASE_API_KEY']}", json_body(message_received))
  }
end

#set_chatbase_fields(intent, text, not_handled)

This method returns an undefined value.

Method to set chatbase client fields

Parameters:

  • intent (String)

    Given intent for user message

  • text (String)

    Text of message or representation of postback

  • not_handled (Boolean)

    Whether or not user message was understood



59
60
61
62
63
# File 'lib/chatbase_api_client.rb', line 59

def set_chatbase_fields(intent, text, not_handled)
  @intent = intent
  @text = text
  @not_handled = not_handled
end