feat: enhancement to un-send instagram message (#4120)
This commit is contained in:
parent
6169d172d8
commit
7b4cac746a
3 changed files with 63 additions and 0 deletions
|
@ -20,6 +20,8 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
|
||||||
# person can connect the channel and then delete the inbox
|
# person can connect the channel and then delete the inbox
|
||||||
return if @inbox.blank?
|
return if @inbox.blank?
|
||||||
|
|
||||||
|
return unsend_message if message_is_deleted?
|
||||||
|
|
||||||
ensure_contact(contact_id)
|
ensure_contact(contact_id)
|
||||||
|
|
||||||
create_message
|
create_message
|
||||||
|
@ -46,6 +48,19 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
|
||||||
@messaging[:message][:is_echo].present?
|
@messaging[:message][:is_echo].present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def message_is_deleted?
|
||||||
|
@messaging[:message][:is_deleted].present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def unsend_message
|
||||||
|
message_to_delete = @inbox.messages.find_by(
|
||||||
|
source_id: @messaging[:message][:mid]
|
||||||
|
)
|
||||||
|
return if message_to_delete.blank?
|
||||||
|
|
||||||
|
message_to_delete.update!(content: I18n.t('conversations.messages.deleted'), deleted: true)
|
||||||
|
end
|
||||||
|
|
||||||
def create_message
|
def create_message
|
||||||
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
|
||||||
|
|
|
@ -56,6 +56,33 @@ FactoryBot.define do
|
||||||
initialize_with { attributes }
|
initialize_with { attributes }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :instagram_message_unsend_event, class: Hash do
|
||||||
|
entry do
|
||||||
|
[
|
||||||
|
{
|
||||||
|
'id': 'instagram-message-id-123',
|
||||||
|
'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-to-delete',
|
||||||
|
'is_deleted': true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
initialize_with { attributes }
|
||||||
|
end
|
||||||
|
|
||||||
factory :instagram_message_attachment_event, class: Hash do
|
factory :instagram_message_attachment_event, class: Hash do
|
||||||
entry do
|
entry do
|
||||||
[
|
[
|
||||||
|
|
|
@ -28,6 +28,7 @@ describe Webhooks::InstagramEventsJob do
|
||||||
let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
|
let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
|
||||||
let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access }
|
let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access }
|
||||||
let!(:test_params) { build(:instagram_test_text_event).with_indifferent_access }
|
let!(:test_params) { build(:instagram_test_text_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(:fb_object) { double }
|
let(:fb_object) { double }
|
||||||
|
@ -61,6 +62,26 @@ describe Webhooks::InstagramEventsJob do
|
||||||
expect(instagram_inbox.messages.last.content).to eq('This is a test message from facebook.')
|
expect(instagram_inbox.messages.last.content).to eq('This is a test message from facebook.')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'handle instagram unsend message event' do
|
||||||
|
create(:message,
|
||||||
|
source_id: 'message-id-to-delete',
|
||||||
|
inbox_id: instagram_inbox.id)
|
||||||
|
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||||
|
allow(fb_object).to receive(:get_object).and_return(
|
||||||
|
{
|
||||||
|
name: 'Jane',
|
||||||
|
id: 'Sender-id-1',
|
||||||
|
account_id: instagram_inbox.account_id,
|
||||||
|
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||||
|
}.with_indifferent_access
|
||||||
|
)
|
||||||
|
expect(instagram_inbox.messages.count).to be 1
|
||||||
|
instagram_webhook.perform_now(unsend_event[:entry])
|
||||||
|
|
||||||
|
expect(instagram_inbox.messages.last.content).to eq 'This message was deleted'
|
||||||
|
expect(instagram_inbox.messages.last.reload.deleted).to eq true
|
||||||
|
end
|
||||||
|
|
||||||
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(
|
||||||
|
|
Loading…
Reference in a new issue