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:
parent
83eee7df91
commit
fcb9a9ab0c
3 changed files with 57 additions and 6 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue