* feat: implement event dispatching for incoming WhatsApp messages * refactor: remove unused dispatcher instance variable in event dispatching context
36 lines
1.2 KiB
Ruby
36 lines
1.2 KiB
Ruby
# https://docs.360dialog.com/whatsapp-api/whatsapp-api/media
|
|
# https://developers.facebook.com/docs/whatsapp/api/media/
|
|
|
|
class Whatsapp::IncomingMessageWhatsappCloudService < Whatsapp::IncomingMessageBaseService
|
|
include Events::Types
|
|
|
|
def perform
|
|
return if processed_params.blank?
|
|
|
|
Rails.configuration.dispatcher.dispatch(PROVIDER_EVENT_RECEIVED, Time.zone.now, inbox: inbox, event: whatsapp_cloud_event_type,
|
|
payload: processed_params)
|
|
|
|
super
|
|
end
|
|
|
|
private
|
|
|
|
def whatsapp_cloud_event_type
|
|
params.dig(:entry, 0, :changes, 0, :field) || 'unknown'
|
|
end
|
|
|
|
def processed_params
|
|
@processed_params ||= params[:entry].try(:first).try(:[], 'changes').try(:first).try(:[], 'value')
|
|
end
|
|
|
|
def download_attachment_file(attachment_payload)
|
|
url_response = HTTParty.get(
|
|
inbox.channel.media_url(attachment_payload[:id]),
|
|
headers: inbox.channel.api_headers
|
|
)
|
|
# This url response will be failure if the access token has expired.
|
|
inbox.channel.authorization_error! if url_response.unauthorized?
|
|
Down.download(url_response.parsed_response['url'], headers: inbox.channel.api_headers) if url_response.success?
|
|
end
|
|
end
|