Class: ChatSDK::WhatsApp::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::WhatsApp::Adapter
- Includes:
- Adapter::MediaTypes, Adapter::MetaVerification
- Defined in:
- lib/chat_sdk/whatsapp/adapter.rb
Constant Summary collapse
- WHATSAPP_MESSAGE_LIMIT =
4096
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
Instance Method Summary collapse
- #ack_response(rack_request) ⇒ Object
- #add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
- #download_media(media_id:) ⇒ Object
-
#initialize(access_token: nil, app_secret: nil, phone_number_id: nil, verify_token: nil) ⇒ Adapter
constructor
A new instance of Adapter.
- #mark_as_read(message_id:) ⇒ Object
- #mention(user_id) ⇒ Object
- #name ⇒ Object
- #open_dm(user_id) ⇒ Object
- #parse_events(rack_request) ⇒ Object
-
#post_message(channel_id:, message:, thread_id: nil) ⇒ Object
Outbound.
- #post_template(channel_id:, template_name:, language_code: "en", components: nil) ⇒ Object
-
#remove_reaction(channel_id:, message_id:, emoji: "") ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #render(postable_message) ⇒ Object
-
#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#verify_request!(rack_request) ⇒ Object
Inbound.
Constructor Details
#initialize(access_token: nil, app_secret: nil, phone_number_id: nil, verify_token: nil) ⇒ Adapter
Returns a new instance of Adapter.
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
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 11 def client @client end |
Class Method Details
.template_components(header: nil, body_params: [], button_payloads: []) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 62 def self.template_components(header: nil, body_params: [], button_payloads: []) components = [] if header components << { "type" => "header", "parameters" => [{"type" => "text", "text" => header}] } end if body_params.any? components << { "type" => "body", "parameters" => body_params.map { |p| {"type" => "text", "text" => p.to_s} } } end .each_with_index do |payload, index| components << { "type" => "button", "sub_type" => "quick_reply", "index" => index.to_s, "parameters" => [{"type" => "payload", "payload" => payload}] } end components end |
Instance Method Details
#ack_response(rack_request) ⇒ Object
35 36 37 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 35 def ack_response(rack_request) (rack_request, verify_token: @verify_token) end |
#add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
123 124 125 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 123 def add_reaction(channel_id:, message_id:, emoji:) @client.send_reaction(to: channel_id, message_id: , emoji: emoji) end |
#download_media(media_id:) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 99 def download_media(media_id:) = @client.get_media_url(media_id: media_id) url = ["url"] return nil unless url response = @client.download_media(url: url) response.body end |
#mark_as_read(message_id:) ⇒ Object
95 96 97 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 95 def mark_as_read(message_id:) @client.mark_as_read(message_id: ) end |
#mention(user_id) ⇒ Object
135 136 137 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 135 def mention(user_id) user_id end |
#name ⇒ Object
26 27 28 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 26 def name :whatsapp end |
#open_dm(user_id) ⇒ Object
131 132 133 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 131 def open_dm(user_id) user_id end |
#parse_events(rack_request) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 39 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 47 def (channel_id:, message:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument payload = () result = if payload[:type] == "text" && payload.dig(:text, :body) chunks = (payload[:text][:body]) chunks.reduce(nil) do |_, chunk| @client.(to: channel_id, type: "text", text: {body: chunk}) end else @client.(to: channel_id, **payload) end (result, channel_id) end |
#post_template(channel_id:, template_name:, language_code: "en", components: nil) ⇒ Object
91 92 93 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 91 def post_template(channel_id:, template_name:, language_code: "en", components: nil) @client.send_template(to: channel_id, template_name: template_name, language_code: language_code, components: components) end |
#remove_reaction(channel_id:, message_id:, emoji: "") ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
127 128 129 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 127 def remove_reaction(channel_id:, message_id:, emoji: "") # rubocop:disable Lint/UnusedMethodArgument @client.send_reaction(to: channel_id, message_id: , emoji: "") end |
#render(postable_message) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 139 def render() if .card? @renderer.render(.card) else .text end end |
#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 108 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.(to: channel_id, type: media_type, **{media_type => media_payload.merge(id: media_id)}) (result, channel_id) end |
#verify_request!(rack_request) ⇒ Object
Inbound
31 32 33 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 31 def verify_request!(rack_request) (rack_request, secret: @app_secret, platform_name: "WhatsApp") end |