From fcb9a9ab0cece4f4d18041251c8344aec26c1ea5 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Thu, 29 Sep 2022 01:20:23 +0530 Subject: [PATCH] fix: contact is not available for inaccessible sender (#5509) Fixes: #5508 We can not read contact information because of this error, as the messages echo when the sender sends messages to contacts. We don't have the user's consent until and unless they send messages to us. So after this result, information about the contact is empty, and we are trying to create a contact inbox for the same, and the error appears. type: OAuthException, code: 230, message: (#230) User consent is required to access user profile, x-fb-trace-id: AaitxF/whwY [HTTP 403] (Koala::Facebook::ClientError) --- app/services/instagram/message_text.rb | 4 ++- .../instagram_message_create_event.rb | 35 +++++++++++++++++++ .../webhooks/instagram_events_job_spec.rb | 24 ++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/app/services/instagram/message_text.rb b/app/services/instagram/message_text.rb index 89cb12884..abcbe2d60 100644 --- a/app/services/instagram/message_text.rb +++ b/app/services/instagram/message_text.rb @@ -42,7 +42,7 @@ class Instagram::MessageText < Instagram::WebhooksBaseService ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception end - find_or_create_contact(result) + find_or_create_contact(result) if result.present? end def agent_message_via_echo? @@ -71,6 +71,8 @@ class Instagram::MessageText < Instagram::WebhooksBaseService end def create_message + return unless @contact_inbox + Messages::Instagram::MessageBuilder.new(@messaging, @inbox, outgoing_echo: agent_message_via_echo?).perform end diff --git a/spec/factories/instagram/instagram_message_create_event.rb b/spec/factories/instagram/instagram_message_create_event.rb index b821580b4..d024deccc 100644 --- a/spec/factories/instagram/instagram_message_create_event.rb +++ b/spec/factories/instagram/instagram_message_create_event.rb @@ -147,4 +147,39 @@ FactoryBot.define do end initialize_with { attributes } end + + factory :instagram_story_mention_event_with_echo, class: Hash do + entry do + [ + { + 'id': 'instagram-message-id-1234', + 'time': '2021-09-08T06:34:04+0000', + 'messaging': [ + { + 'sender': { + 'id': 'Sender-id-1' + }, + 'recipient': { + 'id': 'chatwoot-app-user-id-1' + }, + 'timestamp': '2021-09-08T06:34:04+0000', + 'message': { + 'mid': 'message-id-1', + 'attachments': [ + { + 'type': 'story_mention', + 'payload': { + 'url': 'https://www.example.com/test.jpeg' + } + } + ], + 'is_echo': true + } + } + ] + } + ] + end + initialize_with { attributes } + end end diff --git a/spec/jobs/webhooks/instagram_events_job_spec.rb b/spec/jobs/webhooks/instagram_events_job_spec.rb index 91600375f..89951e33a 100644 --- a/spec/jobs/webhooks/instagram_events_job_spec.rb +++ b/spec/jobs/webhooks/instagram_events_job_spec.rb @@ -11,7 +11,7 @@ describe Webhooks::InstagramEventsJob do end let!(:account) { create(:account) } - let(:return_onject) do + let(:return_object) do { name: 'Jane', id: 'Sender-id-1', account_id: instagram_inbox.account_id, @@ -24,6 +24,7 @@ describe Webhooks::InstagramEventsJob do let!(:unsend_event) { build(:instagram_message_unsend_event).with_indifferent_access } let!(:attachment_params) { build(:instagram_message_attachment_event).with_indifferent_access } let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access } + let!(:story_mention_echo_params) { build(:instagram_story_mention_event_with_echo).with_indifferent_access } let(:fb_object) { double } describe '#perform' do @@ -31,7 +32,7 @@ describe Webhooks::InstagramEventsJob do it 'creates incoming message in the instagram inbox' do allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_return( - return_onject.with_indifferent_access + return_object.with_indifferent_access ) instagram_webhook.perform_now(dm_params[:entry]) @@ -45,7 +46,7 @@ describe Webhooks::InstagramEventsJob do it 'creates test text message in the instagram inbox' do allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_return( - return_onject.with_indifferent_access + return_object.with_indifferent_access ) instagram_webhook.perform_now(test_params[:entry]) @@ -78,7 +79,7 @@ describe Webhooks::InstagramEventsJob do it 'creates incoming message with attachments in the instagram inbox' do allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_return( - return_onject.with_indifferent_access + return_object.with_indifferent_access ) instagram_webhook.perform_now(attachment_params[:entry]) @@ -92,7 +93,7 @@ describe Webhooks::InstagramEventsJob do it 'creates incoming message with attachments in the instagram inbox for story mention' do allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_return( - return_onject.with_indifferent_access, + return_object.with_indifferent_access, { story: { mention: { @@ -113,6 +114,19 @@ describe Webhooks::InstagramEventsJob do expect(instagram_inbox.messages.count).to be 1 expect(instagram_inbox.messages.last.attachments.count).to be 1 end + + it 'creates does not create contact or messages' do + allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) + allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError) + + instagram_webhook.perform_now(story_mention_echo_params[:entry]) + + instagram_inbox.reload + + expect(instagram_inbox.contacts.count).to be 0 + expect(instagram_inbox.contact_inboxes.count).to be 0 + expect(instagram_inbox.messages.count).to be 0 + end end end end