Class: ChatSDK::WhatsApp::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/whatsapp/api_client.rb

Constant Summary collapse

BASE_URL =
"https://graph.facebook.com/v21.0"

Instance Method Summary collapse

Constructor Details

#initialize(access_token, phone_number_id) ⇒ ApiClient

Returns a new instance of ApiClient.



8
9
10
11
# File 'lib/chat_sdk/whatsapp/api_client.rb', line 8

def initialize(access_token, phone_number_id)
  @access_token = access_token
  @phone_number_id = phone_number_id
end

Instance Method Details

#send_message(to:, type:, **payload) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/chat_sdk/whatsapp/api_client.rb', line 13

def send_message(to:, type:, **payload)
  body = {
    "messaging_product" => "whatsapp",
    "recipient_type" => "individual",
    "to" => to,
    "type" => type
  }.merge(payload)

  request(:post, "#{@phone_number_id}/messages", body)
end

#send_reaction(to:, message_id:, emoji:) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/chat_sdk/whatsapp/api_client.rb', line 24

def send_reaction(to:, message_id:, emoji:)
  send_message(
    to: to,
    type: "reaction",
    reaction: {message_id: message_id, emoji: emoji}
  )
end

#upload_media(io:, filename:, content_type:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chat_sdk/whatsapp/api_client.rb', line 32

def upload_media(io:, filename:, content_type:)
  response = media_connection.post("#{@phone_number_id}/media") do |req|
    req.body = {
      "messaging_product" => "whatsapp",
      "file" => Faraday::Multipart::FilePart.new(io, content_type, filename),
      "type" => content_type
    }
  end

  handle_response(response)
end