Class: ChatSDK::WhatsApp::Adapter

Inherits:
Adapter::Base
  • Object
show all
Includes:
Adapter::MetaVerification
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)


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

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.



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

def client
  @client
end

Instance Method Details

#ack_response(rack_request) ⇒ Object



34
35
36
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 34

def ack_response(rack_request)
  meta_ack_response(rack_request, verify_token: @verify_token)
end

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



69
70
71
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 69

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



81
82
83
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 81

def mention(user_id)
  user_id
end

#nameObject



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

def name
  :whatsapp
end

#open_dm(user_id) ⇒ Object



77
78
79
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 77

def open_dm(user_id)
  user_id
end

#parse_events(rack_request) ⇒ Object



38
39
40
41
42
43
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 38

def parse_events(rack_request)
  payload = read_json_body(rack_request)
  EventParser.parse(payload, @phone_number_id)
rescue JSON::ParserError
  []
end

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

Outbound



46
47
48
49
50
51
52
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 46

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



73
74
75
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 73

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



85
86
87
88
89
90
91
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 85

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 54

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



30
31
32
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 30

def verify_request!(rack_request)
  verify_meta_signature!(rack_request, secret: @app_secret, platform_name: "WhatsApp")
end