add webhook registration page as well

This commit is contained in:
Tanmay Deep Sharma
2025-06-09 02:02:21 +05:30
parent 806ddaa9ab
commit e0b52c3fc2
3 changed files with 29 additions and 6 deletions
+1 -3
View File
@@ -5,6 +5,7 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
channel = find_channel_from_whatsapp_business_payload(params)
if channel_is_inactive?(channel)
Rails.logger.info("Channel is inactive: #{channel.inspect}")
Rails.logger.warn("Inactive WhatsApp channel: #{channel&.phone_number || "unknown - #{params[:phone_number]}"}")
return
end
@@ -48,8 +49,5 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
channel = Channel::Whatsapp.find_by(phone_number: phone_number)
# validate to ensure the phone number id matches the whatsapp channel
return channel if channel && channel.provider_config['phone_number_id'] == phone_number_id
phone_number = wb_params[:phone_number]
Channel::Whatsapp.find_by(phone_number: phone_number)
end
end
-1
View File
@@ -8,7 +8,6 @@
# allow_messages_after_resolved :boolean default(TRUE)
# auto_assignment_config :jsonb
# business_name :string
# channel_attributes :jsonb
# channel_type :string
# csat_config :jsonb not null
# csat_survey_enabled :boolean default(FALSE)
@@ -73,7 +73,7 @@ class Whatsapp::EmbeddedSignupService
{
phone_number_id: phone_data['id'],
phone_number: phone_data['display_phone_number'],
phone_number: "+#{phone_data['display_phone_number']}",
verified: phone_data['code_verification_status'] == 'VERIFIED',
business_name: phone_data['verified_name'] || phone_data['display_phone_number']
}
@@ -86,8 +86,10 @@ class Whatsapp::EmbeddedSignupService
if existing_channel
update_existing_channel(existing_channel, channel_attributes, waba_info, phone_info)
else
create_new_channel(channel_attributes, waba_info, phone_info)
channel = create_new_channel(channel_attributes, waba_info, phone_info)
register_phone_number(phone_info[:phone_number_id], access_token)
override_waba_webhook(waba_info[:waba_id], channel, access_token)
channel
end
end
@@ -214,4 +216,28 @@ class Whatsapp::EmbeddedSignupService
app_secret = GlobalConfigService.load('WHATSAPP_APP_SECRET', '')
"#{app_id}|#{app_secret}"
end
def override_waba_webhook(waba_id, channel, access_token)
callback_url = "https://tanmay-local.chatwoot.dev/webhooks/whatsapp/#{channel.phone_number}"
verify_token = channel.provider_config['webhook_verify_token']
response = HTTParty.post(
"https://graph.facebook.com/v21.0/#{waba_id}/subscribed_apps",
{
headers: {
'Authorization' => "Bearer #{access_token}",
'Content-Type' => 'application/json'
},
body: {
override_callback_uri: callback_url,
verify_token: verify_token
}.to_json
}
)
return if response.success?
Rails.logger.error("[WHATSAPP] Webhook override failed: #{response.body}")
raise "Webhook override failed: #{response.body}"
end
end