feat: Add webhook events for contact created, updated (#6415)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -14,7 +14,9 @@
|
||||
"CONVERSATION_UPDATED": "Conversation Updated",
|
||||
"MESSAGE_CREATED": "Message created",
|
||||
"MESSAGE_UPDATED": "Message updated",
|
||||
"WEBWIDGET_TRIGGERED": "Live chat widget opened by the user"
|
||||
"WEBWIDGET_TRIGGERED": "Live chat widget opened by the user",
|
||||
"CONTACT_CREATED": "Contact created",
|
||||
"CONTACT_UPDATED": "Contact update"
|
||||
}
|
||||
},
|
||||
"END_POINT": {
|
||||
|
||||
@@ -61,6 +61,8 @@ const SUPPORTED_WEBHOOK_EVENTS = [
|
||||
'message_created',
|
||||
'message_updated',
|
||||
'webwidget_triggered',
|
||||
'contact_created',
|
||||
'contact_updated',
|
||||
];
|
||||
|
||||
export default {
|
||||
|
||||
@@ -51,10 +51,25 @@ class WebhookListener < BaseListener
|
||||
deliver_webhook_payloads(payload, inbox)
|
||||
end
|
||||
|
||||
def contact_created(event)
|
||||
contact, account = extract_contact_and_account(event)
|
||||
payload = contact.webhook_data.merge(event: __method__.to_s)
|
||||
deliver_account_webhooks(payload, account)
|
||||
end
|
||||
|
||||
def contact_updated(event)
|
||||
contact, account = extract_contact_and_account(event)
|
||||
changed_attributes = extract_changed_attributes(event)
|
||||
return if changed_attributes.blank?
|
||||
|
||||
payload = contact.webhook_data.merge(event: __method__.to_s, changed_attributes: changed_attributes)
|
||||
deliver_account_webhooks(payload, account)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def deliver_account_webhooks(payload, inbox)
|
||||
inbox.account.webhooks.account_type.each do |webhook|
|
||||
def deliver_account_webhooks(payload, account)
|
||||
account.webhooks.account_type.each do |webhook|
|
||||
next unless webhook.subscriptions.include?(payload[:event])
|
||||
|
||||
WebhookJob.perform_later(webhook.url, payload)
|
||||
@@ -69,7 +84,7 @@ class WebhookListener < BaseListener
|
||||
end
|
||||
|
||||
def deliver_webhook_payloads(payload, inbox)
|
||||
deliver_account_webhooks(payload, inbox)
|
||||
deliver_account_webhooks(payload, inbox.account)
|
||||
deliver_api_inbox_webhooks(payload, inbox)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -121,11 +121,16 @@ class Contact < ApplicationRecord
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
name: name,
|
||||
account: account.webhook_data,
|
||||
additional_attributes: additional_attributes,
|
||||
avatar: avatar_url,
|
||||
type: 'contact',
|
||||
account: account.webhook_data
|
||||
custom_attributes: custom_attributes,
|
||||
email: email,
|
||||
id: id,
|
||||
identifier: identifier,
|
||||
name: name,
|
||||
phone_number: phone_number,
|
||||
thumbnail: avatar_url
|
||||
}
|
||||
end
|
||||
|
||||
@@ -180,7 +185,7 @@ class Contact < ApplicationRecord
|
||||
end
|
||||
|
||||
def dispatch_update_event
|
||||
Rails.configuration.dispatcher.dispatch(CONTACT_UPDATED, Time.zone.now, contact: self)
|
||||
Rails.configuration.dispatcher.dispatch(CONTACT_UPDATED, Time.zone.now, contact: self, changed_attributes: previous_changes)
|
||||
end
|
||||
|
||||
def dispatch_destroy_event
|
||||
|
||||
@@ -25,8 +25,8 @@ class Webhook < ApplicationRecord
|
||||
validate :validate_webhook_subscriptions
|
||||
enum webhook_type: { account_type: 0, inbox_type: 1 }
|
||||
|
||||
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created message_created message_updated
|
||||
webwidget_triggered].freeze
|
||||
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created contact_created contact_updated
|
||||
message_created message_updated webwidget_triggered].freeze
|
||||
|
||||
private
|
||||
|
||||
|
||||
Reference in New Issue
Block a user