iachat/app/controllers/webhooks/whatsapp_controller.rb
Cayo P. R. Oliveira 76deea996d
chore: refactor incoming_message_baileys_service using composition and fix some failure points (#53)
* feat: implement connection update handling for Baileys

* feat: add message update handling for Baileys integration

* feat: implement message processing and handling for Baileys integration

* fix: clear message source ID from Redis when contact is not found

* fix: raise error when attachment file is not found during media handling

* refactor: reorganize includes in incoming_message_baileys_service

* feat: add helper methods for message handling in Baileys integration

* feat: include IncomingMessageServiceHelpers in MessagesUpdate module

* refactor: replace IncomingMessageServiceHelpers with BaileysHandlers::Helpers in connection_update, messages_update, and messages_upsert modules

* fix: mark message as unsupported when attachment file is not found

* refactor: remove unnecessary namespace for includes in IncomingMessageBaileysService

* refactor: add private visibility to methods in connection_update, helpers, messages_update, and messages_upsert modules

* refactor: preserve original message in handle_edited_content method

* fix: attachment error handling

* feat: implement conversation creation logic in set_conversation method

* refactor: remove unused error handling for attachment not found in messages update and upsert

* feat: update last seen timestamps in conversation on message status update

* feat: log warning for unsupported message update status in Baileys service

* chore: merge

---------

Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
2025-05-29 10:56:39 -03:00

49 lines
1.5 KiB
Ruby

class Webhooks::WhatsappController < ActionController::API
include MetaTokenVerifyConcern
def process_payload
if inactive_whatsapp_number?
Rails.logger.warn("Rejected webhook for inactive WhatsApp number: #{params[:phone_number]}")
render json: { error: 'Inactive WhatsApp number' }, status: :unprocessable_entity
return
end
perform_whatsapp_events_job
end
private
def perform_whatsapp_events_job
perform_sync if params[:awaitResponse].present?
return if performed?
Webhooks::WhatsappEventsJob.perform_later(params.to_unsafe_hash)
head :ok
end
def perform_sync
Webhooks::WhatsappEventsJob.perform_now(params.to_unsafe_hash)
rescue Whatsapp::IncomingMessageBaileysService::InvalidWebhookVerifyToken
head :unauthorized
rescue Whatsapp::IncomingMessageBaileysService::MessageNotFoundError
head :not_found
end
def valid_token?(token)
channel = Channel::Whatsapp.find_by(phone_number: params[:phone_number])
whatsapp_webhook_verify_token = channel.provider_config['webhook_verify_token'] if channel.present?
token == whatsapp_webhook_verify_token if whatsapp_webhook_verify_token.present?
end
def inactive_whatsapp_number?
phone_number = params[:phone_number]
return false if phone_number.blank?
inactive_numbers = GlobalConfig.get_value('INACTIVE_WHATSAPP_NUMBERS').to_s
return false if inactive_numbers.blank?
inactive_numbers_array = inactive_numbers.split(',').map(&:strip)
inactive_numbers_array.include?(phone_number)
end
end