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)
This commit is contained in:
Tejaswini Chile 2022-09-29 01:20:23 +05:30 committed by GitHub
parent 83eee7df91
commit fcb9a9ab0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 6 deletions

View file

@ -42,7 +42,7 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception
end end
find_or_create_contact(result) find_or_create_contact(result) if result.present?
end end
def agent_message_via_echo? def agent_message_via_echo?
@ -71,6 +71,8 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
end end
def create_message def create_message
return unless @contact_inbox
Messages::Instagram::MessageBuilder.new(@messaging, @inbox, outgoing_echo: agent_message_via_echo?).perform Messages::Instagram::MessageBuilder.new(@messaging, @inbox, outgoing_echo: agent_message_via_echo?).perform
end end

View file

@ -147,4 +147,39 @@ FactoryBot.define do
end end
initialize_with { attributes } initialize_with { attributes }
end 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 end

View file

@ -11,7 +11,7 @@ describe Webhooks::InstagramEventsJob do
end end
let!(:account) { create(:account) } let!(:account) { create(:account) }
let(:return_onject) do let(:return_object) do
{ name: 'Jane', { name: 'Jane',
id: 'Sender-id-1', id: 'Sender-id-1',
account_id: instagram_inbox.account_id, 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!(:unsend_event) { build(:instagram_message_unsend_event).with_indifferent_access }
let!(:attachment_params) { build(:instagram_message_attachment_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_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 } let(:fb_object) { double }
describe '#perform' do describe '#perform' do
@ -31,7 +32,7 @@ describe Webhooks::InstagramEventsJob do
it 'creates incoming message in the instagram inbox' do it 'creates incoming message in the instagram inbox' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return( 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]) 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 it 'creates test text message in the instagram inbox' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return( 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]) 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 it 'creates incoming message with attachments in the instagram inbox' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return( 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]) 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 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(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return( allow(fb_object).to receive(:get_object).and_return(
return_onject.with_indifferent_access, return_object.with_indifferent_access,
{ story: { story:
{ {
mention: { mention: {
@ -113,6 +114,19 @@ describe Webhooks::InstagramEventsJob do
expect(instagram_inbox.messages.count).to be 1 expect(instagram_inbox.messages.count).to be 1
expect(instagram_inbox.messages.last.attachments.count).to be 1 expect(instagram_inbox.messages.last.attachments.count).to be 1
end 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 end
end end