mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-04 21:02:35 +08:00
Some checks failed
Frontend Lint & Test / test (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
## Summary Frontend for WhatsApp Cloud Calling: header / contact-panel call buttons, ringing widget, accept/reject/hangup, mute, in-bubble audio player + transcript, recording-on-hangup upload, mid-call reload warning. WebRTC is browser-direct to Meta — no media server bridge. ## Closes - https://linear.app/chatwoot/issue/PLA-150 ## How to test Requires backend support — the controller, services, model changes, and routes ship in **#14334** (`feature/pla-150`). Merge / deploy that first (or simultaneously); the FE alone won't function without those endpoints. Then on staging, for a WhatsApp Cloud + embedded-signup inbox with the new \`Configuration → Enable voice calling\` toggle ON and webhook registered: 1. **Outbound** — open a conversation, click the phone icon in the conversation header (or contact panel), grant mic, your phone rings, answer, audio both ways, hang up. Recording + transcript land in the bubble within ~10s. 2. **Inbound** — call the business number from your phone. The FloatingCallWidget appears bottom-right with caller name. Click accept, audio both ways, hang up. Recording + transcript appear. 3. **Mute** — during an active WhatsApp call, click the mic icon next to hangup. Speech stops reaching Meta until you click again. 4. **Mid-call reload guard** — try `Cmd-R` during an active call; browser shows a confirm prompt. --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
128 lines
3.7 KiB
Ruby
128 lines
3.7 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
|
|
|
|
WEBHOOK_DEFAULT_FIELDS = %w[messages smb_message_echoes calls].freeze
|
|
|
|
def subscribe_waba_webhook(waba_id, callback_url, verify_token, subscribed_fields: WEBHOOK_DEFAULT_FIELDS)
|
|
# Step 1: Subscribe app to WABA first (required before override)
|
|
# Meta requires the app to be subscribed before using override_callback_uri
|
|
# See: https://github.com/chatwoot/chatwoot/issues/13097
|
|
subscribe_app_to_waba(waba_id)
|
|
|
|
# Step 2: Override callback URL for this specific WABA
|
|
override_waba_callback(waba_id, callback_url, verify_token, subscribed_fields: subscribed_fields)
|
|
end
|
|
|
|
def subscribe_app_to_waba(waba_id)
|
|
response = HTTParty.post(
|
|
"#{BASE_URI}/#{@api_version}/#{waba_id}/subscribed_apps",
|
|
headers: request_headers
|
|
)
|
|
|
|
handle_response(response, 'App subscription to WABA failed')
|
|
end
|
|
|
|
def override_waba_callback(waba_id, callback_url, verify_token, subscribed_fields: WEBHOOK_DEFAULT_FIELDS)
|
|
response = HTTParty.post(
|
|
"#{BASE_URI}/#{@api_version}/#{waba_id}/subscribed_apps",
|
|
headers: request_headers,
|
|
body: {
|
|
override_callback_uri: callback_url,
|
|
verify_token: verify_token,
|
|
subscribed_fields: subscribed_fields
|
|
}.to_json
|
|
)
|
|
|
|
handle_response(response, 'Webhook callback override 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
|