mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 03:27:52 +00:00
feat: notify on slack disconnect (#1323)
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
class AdministratorNotifications::ChannelNotificationsMailer < ApplicationMailer
|
||||||
|
def slack_disconnect(account)
|
||||||
|
return unless smtp_config_set_or_development?
|
||||||
|
|
||||||
|
emails = account.administrators.pluck(:email)
|
||||||
|
subject = 'Your Slack integration has expired'
|
||||||
|
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{account.id}/settings/integrations/slack"
|
||||||
|
send_mail_with_liquid(to: emails, subject: subject) and return
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -36,7 +36,7 @@ module Reauthorizable
|
|||||||
# could used to manually prompt reauthorization if auth scope changes
|
# could used to manually prompt reauthorization if auth scope changes
|
||||||
def prompt_reauthorization!
|
def prompt_reauthorization!
|
||||||
::Redis::Alfred.set(reauthorization_required_key, true)
|
::Redis::Alfred.set(reauthorization_required_key, true)
|
||||||
# TODO: send_reauthorize_prompt_email
|
AdministratorNotifications::ChannelNotificationsMailer.slack_disconnect(account)&.deliver_later if (is_a? Integrations::Hook) && slack?
|
||||||
end
|
end
|
||||||
|
|
||||||
# call this after you successfully Reauthorized the object in UI
|
# call this after you successfully Reauthorized the object in UI
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
# reference_id :string
|
# reference_id :string
|
||||||
#
|
#
|
||||||
class Integrations::Hook < ApplicationRecord
|
class Integrations::Hook < ApplicationRecord
|
||||||
|
include Reauthorizable
|
||||||
|
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
validates :app_id, presence: true
|
validates :app_id, presence: true
|
||||||
|
|
||||||
@@ -33,4 +35,8 @@ class Integrations::Hook < ApplicationRecord
|
|||||||
def slack?
|
def slack?
|
||||||
app_id == 'slack'
|
app_id == 'slack'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable
|
||||||
|
update(status: 'disabled')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<p>Hello,</p>
|
||||||
|
|
||||||
|
<p>Your Slack integration has expired. </p>
|
||||||
|
<p>Please reconnect Slack to continue receiving messages on Slack</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Click <a href="{{action_url}}">here</a> to re-connect.
|
||||||
|
</p>
|
||||||
@@ -41,17 +41,25 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def send_message
|
def send_message
|
||||||
sender = message.sender
|
|
||||||
sender_type = sender.class == Contact ? 'Contact' : 'Agent'
|
|
||||||
sender_name = sender.try(:name) ? "#{sender_type}: #{sender.try(:name)}" : sender_type
|
|
||||||
|
|
||||||
@slack_message = slack_client.chat_postMessage(
|
@slack_message = slack_client.chat_postMessage(
|
||||||
channel: hook.reference_id,
|
channel: hook.reference_id,
|
||||||
text: message_content,
|
text: message_content,
|
||||||
username: sender_name,
|
username: sender_name(message.sender),
|
||||||
thread_ts: conversation.identifier,
|
thread_ts: conversation.identifier,
|
||||||
icon_url: avatar_url(sender)
|
icon_url: avatar_url(message.sender)
|
||||||
)
|
)
|
||||||
|
rescue Slack::Web::Api::Errors::AccountInactive => e
|
||||||
|
Rails.logger.info e
|
||||||
|
hook.authorization_error!
|
||||||
|
hook.disable if hook.enabled?
|
||||||
|
end
|
||||||
|
|
||||||
|
def sender_name(sender)
|
||||||
|
sender.try(:name) ? "#{sender_type(sender)}: #{sender.try(:name)}" : sender_type(sender)
|
||||||
|
end
|
||||||
|
|
||||||
|
def sender_type(sender)
|
||||||
|
sender.class == Contact ? 'Contact' : 'Agent'
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_reference_id
|
def update_reference_id
|
||||||
|
|||||||
@@ -27,5 +27,24 @@ describe Integrations::Slack::SendOnSlackService do
|
|||||||
|
|
||||||
builder.perform
|
builder.perform
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'disables hook on Slack AccountInactive error' do
|
||||||
|
builder = described_class.new(message: message, hook: hook)
|
||||||
|
slack_client = double
|
||||||
|
expect(builder).to receive(:slack_client).and_return(slack_client)
|
||||||
|
expect(slack_client).to receive(:chat_postMessage).with(
|
||||||
|
channel: hook.reference_id,
|
||||||
|
text: message.content,
|
||||||
|
username: "Contact: #{message.sender.name}",
|
||||||
|
thread_ts: conversation.identifier,
|
||||||
|
icon_url: anything
|
||||||
|
).and_raise(Slack::Web::Api::Errors::AccountInactive.new('Account disconnected'))
|
||||||
|
|
||||||
|
allow(hook).to receive(:authorization_error!)
|
||||||
|
|
||||||
|
builder.perform
|
||||||
|
expect(hook).to be_disabled
|
||||||
|
expect(hook).to have_received(:authorization_error!)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user