From 329d305e9272ae62113a3cfd6564500e5c5d1cca Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Wed, 11 May 2022 14:29:38 +0530 Subject: [PATCH] Fix: Creating contacts for already outgoing/echo messages (#4668) Check if the Instagram contact is already present in the system before throwing the exception. Fixes: #4666 --- app/services/instagram/message_text.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/services/instagram/message_text.rb b/app/services/instagram/message_text.rb index 75c77b540..619ef4c90 100644 --- a/app/services/instagram/message_text.rb +++ b/app/services/instagram/message_text.rb @@ -22,7 +22,7 @@ class Instagram::MessageText < Instagram::WebhooksBaseService return unsend_message if message_is_deleted? - ensure_contact(contact_id) + ensure_contact(contact_id) if contacts_first_message?(contact_id) create_message end @@ -36,7 +36,7 @@ class Instagram::MessageText < Instagram::WebhooksBaseService rescue Koala::Facebook::AuthenticationError @inbox.channel.authorization_error! raise - rescue StandardError => e + rescue StandardError, Koala::Facebook::ClientError => e result = {} ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception end @@ -52,6 +52,10 @@ class Instagram::MessageText < Instagram::WebhooksBaseService @messaging[:message][:is_deleted].present? end + def contacts_first_message?(ig_scope_id) + @inbox.contact_inboxes.where(source_id: ig_scope_id).empty? && @inbox.channel.instagram_id.present? + end + def unsend_message message_to_delete = @inbox.messages.find_by( source_id: @messaging[:message][:mid]