Class: ChatSDK::WhatsApp::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::WhatsApp::Adapter
- Defined in:
- lib/chat_sdk/whatsapp/adapter.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #ack_response(rack_request) ⇒ Object
- #add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
-
#initialize(access_token: nil, app_secret: nil, phone_number_id: nil, verify_token: nil) ⇒ Adapter
constructor
A new instance of Adapter.
- #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.
-
#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 |
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: , 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 |
#name ⇒ Object
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 (channel_id:, message:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument payload = () result = @client.(to: channel_id, **payload) (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: , emoji: "") end |
#render(postable_message) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 113 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
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.(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 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 |