chore: Add reauthorization prompt for Whatsapp Channel (#5929)
- Add reauthorization prompt for Whatsapp Channel fixes: #5782
This commit is contained in:
@@ -4,6 +4,7 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
|
||||
def perform(params = {})
|
||||
channel = find_channel_from_whatsapp_business_payload(params) || find_channel(params)
|
||||
return if channel.blank?
|
||||
return if channel.reauthorization_required?
|
||||
|
||||
case channel.provider
|
||||
when 'whatsapp_cloud'
|
||||
|
||||
@@ -15,6 +15,14 @@ class AdministratorNotifications::ChannelNotificationsMailer < ApplicationMailer
|
||||
send_mail_with_liquid(to: admin_emails, subject: subject) and return
|
||||
end
|
||||
|
||||
def whatsapp_disconnect(inbox)
|
||||
return unless smtp_config_set_or_development?
|
||||
|
||||
subject = 'Your Whatsapp connection has expired'
|
||||
@action_url = "#{ENV.fetch('FRONTEND_URL', nil)}/app/accounts/#{Current.account.id}/settings/inboxes/#{inbox.id}"
|
||||
send_mail_with_liquid(to: admin_emails, subject: subject) and return
|
||||
end
|
||||
|
||||
def email_disconnect(inbox)
|
||||
return unless smtp_config_set_or_development?
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
class Channel::Whatsapp < ApplicationRecord
|
||||
include Channelable
|
||||
include Reauthorizable
|
||||
|
||||
self.table_name = 'channel_whatsapp'
|
||||
EDITABLE_ATTRS = [:phone_number, :provider, { provider_config: {} }].freeze
|
||||
|
||||
@@ -39,11 +39,14 @@ module Reauthorizable
|
||||
def prompt_reauthorization!
|
||||
::Redis::Alfred.set(reauthorization_required_key, true)
|
||||
|
||||
if (is_a? Integrations::Hook) && slack?
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect.deliver_later
|
||||
elsif is_a? Channel::FacebookPage
|
||||
case self.class.name
|
||||
when 'Integrations::Hook'
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect.deliver_later if slack?
|
||||
when 'Channel::FacebookPage'
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).facebook_disconnect(inbox).deliver_later
|
||||
elsif is_a? Channel::Email
|
||||
when 'Channel::Whatsapp'
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).whatsapp_disconnect(inbox).deliver_later
|
||||
when 'Channel::Email'
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).email_disconnect(inbox).deliver_later
|
||||
end
|
||||
end
|
||||
|
||||
@@ -96,9 +96,11 @@ class Whatsapp::IncomingMessageBaseService
|
||||
return if %w[text button interactive location].include?(message_type)
|
||||
|
||||
attachment_payload = @processed_params[:messages].first[message_type.to_sym]
|
||||
attachment_file = download_attachment_file(attachment_payload)
|
||||
|
||||
@message.content ||= attachment_payload[:caption]
|
||||
|
||||
attachment_file = download_attachment_file(attachment_payload)
|
||||
return if attachment_file.blank?
|
||||
|
||||
@message.attachments.new(
|
||||
account_id: @message.account_id,
|
||||
file_type: file_content_type(message_type),
|
||||
|
||||
@@ -10,6 +10,8 @@ class Whatsapp::IncomingMessageWhatsappCloudService < Whatsapp::IncomingMessageB
|
||||
|
||||
def download_attachment_file(attachment_payload)
|
||||
url_response = HTTParty.get(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers)
|
||||
Down.download(url_response.parsed_response['url'], 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
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<p>Hello,</p>
|
||||
|
||||
<p>Your Whatsapp Access has expired. </p>
|
||||
<p>Please reconnect Whatsapp to continue receiving messages.</p>
|
||||
|
||||
<p>
|
||||
Click <a href="{{action_url}}">here</a> to re-connect.
|
||||
</p>
|
||||
Reference in New Issue
Block a user