Class: ChatSDK::WhatsApp::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::WhatsApp::Adapter
- Includes:
- Adapter::MetaVerification
- 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.
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
#client ⇒ Object (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) (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: , 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 |
#name ⇒ Object
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 (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
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: , emoji: "") end |
#render(postable_message) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/chat_sdk/whatsapp/adapter.rb', line 85 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
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.(to: channel_id, type: media_type, **{media_type => media_payload.merge(id: media_id)}) (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) (rack_request, secret: @app_secret, platform_name: "WhatsApp") end |