Class: ChatSDK::Messenger::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::Messenger::Adapter
- Defined in:
- lib/chat_sdk/messenger/adapter.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #ack_response(rack_request) ⇒ Object
-
#initialize(app_secret: nil, page_access_token: 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.
- #render(postable_message) ⇒ Object
-
#start_typing(channel_id:, thread_id: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#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(app_secret: nil, page_access_token: nil, verify_token: nil) ⇒ Adapter
Returns a new instance of Adapter.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 13 def initialize(app_secret: nil, page_access_token: nil, verify_token: nil) @app_secret = app_secret || ENV["FACEBOOK_APP_SECRET"] @page_access_token = page_access_token || ENV["FACEBOOK_PAGE_ACCESS_TOKEN"] @verify_token = verify_token || ENV["FACEBOOK_VERIFY_TOKEN"] raise ChatSDK::ConfigurationError, "Messenger app_secret required" unless @app_secret raise ChatSDK::ConfigurationError, "Messenger page_access_token required" unless @page_access_token @client = ApiClient.new(@page_access_token) @renderer = TemplateRenderer.new end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 11 def client @client end |
Instance Method Details
#ack_response(rack_request) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 49 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 |
#mention(user_id) ⇒ Object
100 101 102 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 100 def mention(user_id) user_id end |
#name ⇒ Object
25 26 27 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 25 def name :messenger end |
#open_dm(user_id) ⇒ Object
92 93 94 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 92 def open_dm(user_id) user_id end |
#parse_events(rack_request) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 62 def parse_events(rack_request) body = rack_request.body.read rack_request.body.rewind payload = JSON.parse(body) EventParser.parse(payload) rescue JSON::ParserError [] end |
#post_message(channel_id:, message:, thread_id: nil) ⇒ Object
Outbound
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 73 def (channel_id:, message:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument payload = () result = @client.( recipient_id: channel_id, message: payload ) (result, channel_id) end |
#render(postable_message) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 104 def render() if .card? @renderer.render(.card) else .text end end |
#start_typing(channel_id:, thread_id: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
96 97 98 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 96 def start_typing(channel_id:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument @client.send_action(recipient_id: channel_id, action: "typing_on") end |
#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
84 85 86 87 88 89 90 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 84 def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) # rubocop:disable Lint/UnusedMethodArgument raise ChatSDK::PlatformError.new( "Messenger file uploads require a publicly accessible URL. Binary upload is not supported. " \ "Host the file at a public URL and send it as an attachment.", adapter_name: :messenger ) end |
#verify_request!(rack_request) ⇒ Object
Inbound
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/chat_sdk/messenger/adapter.rb', line 30 def verify_request!(rack_request) signature = rack_request.get_header("HTTP_X_HUB_SIGNATURE_256") unless signature raise ChatSDK::SignatureVerificationError, "Missing Facebook 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 Facebook signature" end true end |