From cadc0b2e9a44a13c72c29f3443ed646c46bdcf20 Mon Sep 17 00:00:00 2001 From: brohand Date: Wed, 18 May 2022 00:11:45 +0800 Subject: [PATCH] feat: expose response and API usage headers --- lib/facebook/messenger/bot.rb | 4 +++- lib/facebook/messenger/bot/error_parser.rb | 3 +++ lib/facebook/messenger/error.rb | 4 ++++ spec/facebook/messenger/bot_spec.rb | 10 ++++++++++ spec/helpers/graph_api_helpers.rb | 4 +++- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/facebook/messenger/bot.rb b/lib/facebook/messenger/bot.rb index dc29e760..cec8b348 100644 --- a/lib/facebook/messenger/bot.rb +++ b/lib/facebook/messenger/bot.rb @@ -45,7 +45,7 @@ class << self # # Returns a String describing the message ID if the message was sent, # or raises an exception if it was not. - def deliver(message, page_id:) + def deliver(message, page_id:, return_response: false) access_token = config.provider.access_token_for(page_id) app_secret_proof = config.provider.app_secret_proof_for(page_id) @@ -59,6 +59,8 @@ def deliver(message, page_id:) Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response) + return response if return_response + response.body end diff --git a/lib/facebook/messenger/bot/error_parser.rb b/lib/facebook/messenger/bot/error_parser.rb index 66131a92..d74fcc51 100644 --- a/lib/facebook/messenger/bot/error_parser.rb +++ b/lib/facebook/messenger/bot/error_parser.rb @@ -45,6 +45,9 @@ def raise_errors_from(response) error_code = error['code'] error_subcode = error['error_subcode'] + error.merge!('app-usage' => response.headers['x-app-usage'], + 'buc-usage' => response.headers['x-business-use-case-usage']) + raise_code_only_error(error_code, error) if error_subcode.nil? raise_code_subcode_error(error_code, error_subcode, error) diff --git a/lib/facebook/messenger/error.rb b/lib/facebook/messenger/error.rb index d66b52b7..7430c922 100644 --- a/lib/facebook/messenger/error.rb +++ b/lib/facebook/messenger/error.rb @@ -12,6 +12,8 @@ class FacebookError < Error attr_reader :user_title attr_reader :user_msg attr_reader :fbtrace_id + attr_reader :buc_usage + attr_reader :app_usage # # Constructor function. @@ -26,6 +28,8 @@ def initialize(error) @user_title = error['error_user_title'] @user_msg = error['error_user_msg'] @fbtrace_id = error['fbtrace_id'] + @buc_usage = error['buc-usage'] + @app_usage = error['app-usage'] end # diff --git a/spec/facebook/messenger/bot_spec.rb b/spec/facebook/messenger/bot_spec.rb index eb3b06f8..61bcff5d 100644 --- a/spec/facebook/messenger/bot_spec.rb +++ b/spec/facebook/messenger/bot_spec.rb @@ -227,6 +227,16 @@ def stub_request_to_return(hash) expect(result).to eq({ recipient_id: recipient_id, message_id: message_id }.to_json) end + + context 'with option `return_response`' do + it 'sends a message and return response' do + response = subject.deliver(payload, page_id: page_id, return_response: true) + + expect(response.body).to eq({ recipient_id: recipient_id, + message_id: message_id }.to_json) + expect(response.headers).to eq(default_graph_api_response_headers) + end + end end context 'when Facebook had an internal server error' do diff --git a/spec/helpers/graph_api_helpers.rb b/spec/helpers/graph_api_helpers.rb index f6ccaad7..4b0be497 100644 --- a/spec/helpers/graph_api_helpers.rb +++ b/spec/helpers/graph_api_helpers.rb @@ -1,5 +1,7 @@ def default_graph_api_response_headers { - 'Content-Type' => 'text/javascript; charset=UTF-8' + 'Content-Type' => 'text/javascript; charset=UTF-8', + 'x-business-use-case-usage' => "{\"123456789012345\":[{\"type\":\"messenger\",\"call_count\":1,\"total_cputime\":1,\"total_time\":1,\"estimated_time_to_regain_access\":0}]}", + 'x-app-usage' => "{\"call_count\":0,\"total_cputime\":0,\"total_time\":0}" } end