## Description Adds webhook configuration management for WhatsApp Cloud API channels, allowing administrators to check webhook status and register webhooks directly from Chatwoot without accessing Meta Business Manager. ## Type of change - [ ] New feature (non-breaking change which adds functionality) ## Screenshots <img width="1130" height="676" alt="Screenshot 2026-03-05 at 7 04 18 PM" src="https://github.com/user-attachments/assets/f5dcd9dd-8827-42c5-a52b-1024012703c2" /> <img width="1101" height="651" alt="Screenshot 2026-03-05 at 7 04 29 PM" src="https://github.com/user-attachments/assets/e0bd59f9-2a90-4f24-87c0-b79f21e721ee" /> ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
96 lines
2.7 KiB
Ruby
96 lines
2.7 KiB
Ruby
class Whatsapp::HealthService
|
|
BASE_URI = 'https://graph.facebook.com'.freeze
|
|
|
|
def initialize(channel)
|
|
@channel = channel
|
|
@access_token = channel.provider_config['api_key']
|
|
@api_version = GlobalConfigService.load('WHATSAPP_API_VERSION', 'v22.0')
|
|
end
|
|
|
|
def fetch_health_status
|
|
validate_channel!
|
|
fetch_phone_health_data
|
|
end
|
|
|
|
private
|
|
|
|
def validate_channel!
|
|
raise ArgumentError, 'Channel is required' if @channel.blank?
|
|
raise ArgumentError, 'API key is missing' if @access_token.blank?
|
|
raise ArgumentError, 'Phone number ID is missing' if @channel.provider_config['phone_number_id'].blank?
|
|
end
|
|
|
|
def fetch_phone_health_data
|
|
phone_number_id = @channel.provider_config['phone_number_id']
|
|
|
|
response = HTTParty.get(
|
|
"#{BASE_URI}/#{@api_version}/#{phone_number_id}",
|
|
query: {
|
|
fields: health_fields,
|
|
access_token: @access_token
|
|
}
|
|
)
|
|
|
|
handle_response(response)
|
|
rescue StandardError => e
|
|
Rails.logger.error "[WHATSAPP HEALTH] Error fetching health data: #{e.message}"
|
|
raise e
|
|
end
|
|
|
|
def health_fields
|
|
%w[
|
|
id
|
|
quality_rating
|
|
messaging_limit_tier
|
|
code_verification_status
|
|
account_mode
|
|
display_phone_number
|
|
name_status
|
|
verified_name
|
|
webhook_configuration
|
|
throughput
|
|
last_onboarded_time
|
|
platform_type
|
|
certificate
|
|
].join(',')
|
|
end
|
|
|
|
def handle_response(response)
|
|
unless response.success?
|
|
error_message = "WhatsApp API request failed: #{response.code} - #{response.body}"
|
|
Rails.logger.error "[WHATSAPP HEALTH] #{error_message}"
|
|
raise error_message
|
|
end
|
|
|
|
data = response.parsed_response
|
|
format_health_response(data)
|
|
end
|
|
|
|
def format_health_response(response)
|
|
{
|
|
id: response['id'],
|
|
display_phone_number: response['display_phone_number'],
|
|
verified_name: response['verified_name'],
|
|
name_status: response['name_status'],
|
|
quality_rating: response['quality_rating'],
|
|
messaging_limit_tier: response['messaging_limit_tier'],
|
|
account_mode: response['account_mode'],
|
|
code_verification_status: response['code_verification_status'],
|
|
webhook_configuration: response['webhook_configuration'],
|
|
expected_webhook_url: build_expected_webhook_url,
|
|
throughput: response['throughput'],
|
|
last_onboarded_time: response['last_onboarded_time'],
|
|
platform_type: response['platform_type'],
|
|
certificate: response['certificate'],
|
|
business_id: @channel.provider_config['business_account_id']
|
|
}
|
|
end
|
|
|
|
def build_expected_webhook_url
|
|
frontend_url = ENV.fetch('FRONTEND_URL', nil)
|
|
return nil if frontend_url.blank?
|
|
|
|
"#{frontend_url}/webhooks/whatsapp/#{@channel.phone_number}"
|
|
end
|
|
end
|