whatsapp calls fixes
This commit is contained in:
@@ -38,19 +38,51 @@ class Api::V1::Accounts::WhatsappCallsController < Api::V1::Accounts::BaseContro
|
||||
contact_phone = conversation.contact&.phone_number
|
||||
return render json: { error: 'Contact phone number not available' }, status: :unprocessable_entity if contact_phone.blank?
|
||||
|
||||
result = conversation.inbox.channel.provider_service.initiate_call(contact_phone.delete('+'))
|
||||
return render json: { error: 'Failed to initiate call' }, status: :internal_server_error unless result
|
||||
sdp_offer = params[:sdp_offer]
|
||||
return render json: { error: 'sdp_offer is required' }, status: :unprocessable_entity if sdp_offer.blank?
|
||||
|
||||
render json: { status: 'calling', call_id: result['call_id'] }
|
||||
result = conversation.inbox.channel.provider_service.initiate_call(contact_phone.delete('+'), sdp_offer)
|
||||
call_id = result.dig('calls', 0, 'id') || result['call_id']
|
||||
wa_call = current_account.whatsapp_calls.create!(
|
||||
inbox: conversation.inbox,
|
||||
conversation: conversation,
|
||||
call_id: call_id,
|
||||
direction: 'outbound',
|
||||
status: 'ringing',
|
||||
meta: { sdp_offer: sdp_offer }
|
||||
)
|
||||
|
||||
render json: { status: 'calling', call_id: call_id, id: wa_call.id }
|
||||
rescue Whatsapp::CallErrors::NoCallPermission
|
||||
handle_no_call_permission(conversation)
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render json: { error: 'Conversation not found' }, status: :not_found
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "[WHATSAPP CALL] initiate failed: #{e.message}"
|
||||
render json: { error: 'Failed to initiate call' }, status: :internal_server_error
|
||||
render json: { error: e.message }, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle_no_call_permission(conversation)
|
||||
last_requested = conversation.additional_attributes&.dig('call_permission_requested_at')
|
||||
|
||||
# Don't re-send if permission was requested within the last 5 minutes
|
||||
if last_requested.present? && Time.zone.parse(last_requested) > 5.minutes.ago
|
||||
render json: { status: 'permission_pending' }
|
||||
return
|
||||
end
|
||||
|
||||
contact_phone = conversation.contact.phone_number.delete('+')
|
||||
result = conversation.inbox.channel.provider_service.send_call_permission_request(contact_phone)
|
||||
if result
|
||||
conversation.update!(additional_attributes: (conversation.additional_attributes || {}).merge('call_permission_requested_at' => Time.current.iso8601))
|
||||
render json: { status: 'permission_requested' }
|
||||
else
|
||||
render json: { error: 'Failed to send call permission request' }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def validate_whatsapp_calling(conversation)
|
||||
channel = conversation.inbox.channel
|
||||
return 'Calling is only supported on WhatsApp Cloud inboxes' unless channel.is_a?(Channel::Whatsapp) && channel.provider == 'whatsapp_cloud'
|
||||
|
||||
Reference in New Issue
Block a user