Files
chatwoot/app/services/whatsapp/facebook_api_client.rb
Petterson fd28ed8d83 feat: add support to embedded whatsapp coexistence method (#12108)
This update adds support to the coexistence method to Embedded Whatsapp,
allowing users to add their existing whatsapp business number in order
to use it in both places(chatwoot and whatsapp business) at the same
time.

This update require some changes in the permissions for the Meta App, as
described in the Meta Oficial Docs, I'll leave this listed below:

- **history** — describes past messages the business customer has
sent/received
- **smb_app_state_sync** — describes the business customer's current and
new contacts
- **smb_message_echoes** — describes any new messages the business
customer sends with the WhatsApp Business app after having been
onboarded

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
Co-authored-by: Tanmay Deep Sharma <32020192+tds-1@users.noreply.github.com>
2025-08-08 18:28:50 +05:30

106 lines
2.8 KiB
Ruby

class Whatsapp::FacebookApiClient
BASE_URI = 'https://graph.facebook.com'.freeze
def initialize(access_token = nil)
@access_token = access_token
@api_version = GlobalConfigService.load('WHATSAPP_API_VERSION', 'v22.0')
end
def exchange_code_for_token(code)
response = HTTParty.get(
"#{BASE_URI}/#{@api_version}/oauth/access_token",
query: {
client_id: GlobalConfigService.load('WHATSAPP_APP_ID', ''),
client_secret: GlobalConfigService.load('WHATSAPP_APP_SECRET', ''),
code: code
}
)
handle_response(response, 'Token exchange failed')
end
def fetch_phone_numbers(waba_id)
response = HTTParty.get(
"#{BASE_URI}/#{@api_version}/#{waba_id}/phone_numbers",
query: { access_token: @access_token }
)
handle_response(response, 'WABA phone numbers fetch failed')
end
def debug_token(input_token)
response = HTTParty.get(
"#{BASE_URI}/#{@api_version}/debug_token",
query: {
input_token: input_token,
access_token: build_app_access_token
}
)
handle_response(response, 'Token validation failed')
end
def register_phone_number(phone_number_id, pin)
response = HTTParty.post(
"#{BASE_URI}/#{@api_version}/#{phone_number_id}/register",
headers: request_headers,
body: { messaging_product: 'whatsapp', pin: pin.to_s }.to_json
)
handle_response(response, 'Phone registration failed')
end
def phone_number_verified?(phone_number_id)
response = HTTParty.get(
"#{BASE_URI}/#{@api_version}/#{phone_number_id}",
headers: request_headers
)
data = handle_response(response, 'Phone status check failed')
data['code_verification_status'] == 'VERIFIED'
end
def subscribe_waba_webhook(waba_id, callback_url, verify_token)
response = HTTParty.post(
"#{BASE_URI}/#{@api_version}/#{waba_id}/subscribed_apps",
headers: request_headers,
body: {
override_callback_uri: callback_url,
verify_token: verify_token
}.to_json
)
handle_response(response, 'Webhook subscription failed')
end
def unsubscribe_waba_webhook(waba_id)
response = HTTParty.delete(
"#{BASE_URI}/#{@api_version}/#{waba_id}/subscribed_apps",
headers: request_headers
)
handle_response(response, 'Webhook unsubscription failed')
end
private
def request_headers
{
'Authorization' => "Bearer #{@access_token}",
'Content-Type' => 'application/json'
}
end
def build_app_access_token
app_id = GlobalConfigService.load('WHATSAPP_APP_ID', '')
app_secret = GlobalConfigService.load('WHATSAPP_APP_SECRET', '')
"#{app_id}|#{app_secret}"
end
def handle_response(response, error_message)
raise "#{error_message}: #{response.body}" unless response.success?
response.parsed_response
end
end