Class: ChatSDK::WhatsApp::Adapter

Inherits:
Adapter::Base
  • Object
show all
Defined in:
lib/chat_sdk/whatsapp/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, app_secret: nil, phone_number_id: nil, verify_token: nil) ⇒ Adapter

Returns a new instance of Adapter.

Raises:

  • (ChatSDK::ConfigurationError)


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

def initialize(access_token: nil, app_secret: nil, phone_number_id: nil, verify_token: nil)
  @access_token = access_token || ENV["WHATSAPP_ACCESS_TOKEN"]
  @app_secret = app_secret || ENV["WHATSAPP_APP_SECRET"]
  @phone_number_id = phone_number_id || ENV["WHATSAPP_PHONE_NUMBER_ID"]
  @verify_token = verify_token || ENV["WHATSAPP_VERIFY_TOKEN"]

  raise ChatSDK::ConfigurationError, "WhatsApp access_token required" unless @access_token
  raise ChatSDK::ConfigurationError, "WhatsApp phone_number_id required" unless @phone_number_id

  @client = ApiClient.new(@access_token, @phone_number_id)
  @renderer = InteractiveRenderer.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 11

def client
  @client
end

Instance Method Details

#ack_response(rack_request) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 50

def ack_response(rack_request)
  return nil unless rack_request.get?

  params = rack_request.params
  mode = params["hub.mode"]
  token = params["hub.verify_token"]
  challenge = params["hub.challenge"]

  if mode == "subscribe" && token == @verify_token
    [200, {}, [challenge.to_s]]
  end
end

#add_reaction(channel_id:, message_id:, emoji:) ⇒ Object



97
98
99
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 97

def add_reaction(channel_id:, message_id:, emoji:)
  @client.send_reaction(to: channel_id, message_id: message_id, emoji: emoji)
end

#mention(user_id) ⇒ Object



109
110
111
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 109

def mention(user_id)
  user_id
end

#nameObject



26
27
28
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 26

def name
  :whatsapp
end

#open_dm(user_id) ⇒ Object



105
106
107
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 105

def open_dm(user_id)
  user_id
end

#parse_events(rack_request) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 63

def parse_events(rack_request)
  body = rack_request.body.read
  rack_request.body.rewind

  payload = JSON.parse(body)
  EventParser.parse(payload, @phone_number_id)
rescue JSON::ParserError
  []
end

#post_message(channel_id:, message:, thread_id: nil) ⇒ Object

Outbound



74
75
76
77
78
79
80
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 74

def post_message(channel_id:, message:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument
  payload = prepare_message_payload(message)

  result = @client.send_message(to: channel_id, **payload)

  parse_whatsapp_message(result, channel_id)
end

#remove_reaction(channel_id:, message_id:, emoji: "") ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



101
102
103
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 101

def remove_reaction(channel_id:, message_id:, emoji: "") # rubocop:disable Lint/UnusedMethodArgument
  @client.send_reaction(to: channel_id, message_id: message_id, emoji: "")
end

#render(postable_message) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 113

def render(postable_message)
  if postable_message.card?
    @renderer.render(postable_message.card)
  else
    postable_message.text
  end
end

#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 82

def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) # rubocop:disable Lint/UnusedMethodArgument
  content_type = detect_content_type(filename)
  media_type = media_type_for(content_type)

  # Upload media first
  media_result = @client.upload_media(io: io, filename: filename, content_type: content_type)
  media_id = media_result["id"]

  # Send media message
  media_payload = {caption: comment}.compact
  result = @client.send_message(to: channel_id, type: media_type, **{media_type => media_payload.merge(id: media_id)})

  parse_whatsapp_message(result, channel_id)
end

#verify_request!(rack_request) ⇒ Object

Inbound



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 31

def verify_request!(rack_request)
  signature = rack_request.get_header("HTTP_X_HUB_SIGNATURE_256")

  unless signature
    raise ChatSDK::SignatureVerificationError, "Missing WhatsApp signature header"
  end

  body = rack_request.body.read
  rack_request.body.rewind

  expected = "sha256=#{OpenSSL::HMAC.hexdigest("SHA256", @app_secret, body)}"

  unless Rack::Utils.secure_compare(signature, expected)
    raise ChatSDK::SignatureVerificationError, "Invalid WhatsApp signature"
  end

  true
end