diff --git a/lib/stealth/services/facebook/client.rb b/lib/stealth/services/facebook/client.rb index 990ef67..11a2218 100644 --- a/lib/stealth/services/facebook/client.rb +++ b/lib/stealth/services/facebook/client.rb @@ -12,10 +12,13 @@ module Services module Facebook class Client < Stealth::Services::BaseClient + FB_URL = ENV['FACEBOOK_API_URL'] || "graph.facebook.com" + FB_HTTP_PROTOCOL = ENV['FACEBOOK_HTTP_PROTOCOL'] || "https" + FB_ENDPOINT = if ENV['FACEBOOK_API_VERSION'].present? - "https://graph.facebook.com/v#{ENV['FACEBOOK_API_VERSION']}/me" + "#{FB_HTTP_PROTOCOL}://#{FB_URL}/v#{ENV['FACEBOOK_API_VERSION']}/me" else - "https://graph.facebook.com/v3.2/me" + "#{FB_HTTP_PROTOCOL}://#{FB_URL}/v3.2/me" end attr_reader :api_endpoint, :reply @@ -67,11 +70,19 @@ def self.fetch_profile(recipient_id:, fields: nil) access_token: Stealth.config.facebook.page_access_token } - uri = URI::HTTPS.build( - host: "graph.facebook.com", - path: "/#{recipient_id}", - query: query_hash.to_query - ) + if FB_HTTP_PROTOCOL == 'https' + uri = URI::HTTPS.build( + host: FB_URL, + path: "/#{recipient_id}", + query: query_hash.to_query + ) + elsif FB_HTTP_PROTOCOL == 'http' + uri = URI::HTTP.build( + host: FB_URL, + path: "/#{recipient_id}", + query: query_hash.to_query + ) + end res = http_client.get(uri.to_s) Stealth::Logger.l(topic: @@ -107,10 +118,17 @@ def self.track(recipient_id:, metric:, value:, options: {}) page_id: Stealth.config.facebook.page_id } - uri = URI::HTTPS.build( - host: "graph.facebook.com", - path: "/#{Stealth.config.facebook.app_id}/activities" - ) + if FB_HTTP_PROTOCOL == 'https' + uri = URI::HTTPS.build( + host: FB_URL, + path: "/#{Stealth.config.facebook.app_id}/activities" + ) + elsif FB_HTTP_PROTOCOL == 'http' + uri = URI::HTTP.build( + host: FB_URL, + path: "/#{Stealth.config.facebook.app_id}/activities" + ) + end res = http_client.post(uri.to_s, body: MultiJson.dump(params)) Stealth::Logger.l( diff --git a/lib/stealth/services/facebook/events/message_event.rb b/lib/stealth/services/facebook/events/message_event.rb index c3fce80..6813b65 100644 --- a/lib/stealth/services/facebook/events/message_event.rb +++ b/lib/stealth/services/facebook/events/message_event.rb @@ -18,6 +18,7 @@ def process fetch_message fetch_location fetch_attachments + fetch_nlp end private @@ -68,6 +69,12 @@ def fetch_attachments end end + def fetch_nlp + if params.dig('message', 'nlp').present? + service_message.nlp_result = params.dig('message', 'nlp').as_json + end + end + end end