Feat: send fb message outside of standard messaging window (#4439)
This commit is contained in:
parent
d2d838afd1
commit
1ccd29140d
6 changed files with 26 additions and 9 deletions
|
@ -33,7 +33,7 @@ class Channel::FacebookPage < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_24_hour_messaging_window?
|
def has_24_hour_messaging_window?
|
||||||
true
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_contact_inbox(instagram_id, name)
|
def create_contact_inbox(instagram_id, name)
|
||||||
|
|
|
@ -90,10 +90,20 @@ class Conversation < ApplicationRecord
|
||||||
delegate :auto_resolve_duration, to: :account
|
delegate :auto_resolve_duration, to: :account
|
||||||
|
|
||||||
def can_reply?
|
def can_reply?
|
||||||
|
return last_message_less_than_24_hrs? if additional_attributes['type'] == 'instagram_direct_message'
|
||||||
|
|
||||||
return true unless inbox&.channel&.has_24_hour_messaging_window?
|
return true unless inbox&.channel&.has_24_hour_messaging_window?
|
||||||
|
|
||||||
last_incoming_message = messages.incoming.last
|
return false if last_incoming_message.nil?
|
||||||
|
|
||||||
|
last_message_less_than_24_hrs?
|
||||||
|
end
|
||||||
|
|
||||||
|
def last_incoming_message
|
||||||
|
messages&.incoming&.last
|
||||||
|
end
|
||||||
|
|
||||||
|
def last_message_less_than_24_hrs?
|
||||||
return false if last_incoming_message.nil?
|
return false if last_incoming_message.nil?
|
||||||
|
|
||||||
Time.current < last_incoming_message.created_at + 24.hours
|
Time.current < last_incoming_message.created_at + 24.hours
|
||||||
|
|
|
@ -22,7 +22,9 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
||||||
def fb_text_message_params
|
def fb_text_message_params
|
||||||
{
|
{
|
||||||
recipient: { id: contact.get_source_id(inbox.id) },
|
recipient: { id: contact.get_source_id(inbox.id) },
|
||||||
message: { text: message.content }
|
message: { text: message.content },
|
||||||
|
messaging_type: 'MESSAGE_TAG',
|
||||||
|
tag: 'ACCOUNT_UPDATE'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -37,7 +39,9 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
||||||
url: attachment.file_url
|
url: attachment.file_url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
messaging_type: 'MESSAGE_TAG',
|
||||||
|
tag: 'ACCOUNT_UPDATE'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,6 @@ describe Webhooks::InstagramEventsJob do
|
||||||
instagram_webhook.perform_now(test_params[:entry])
|
instagram_webhook.perform_now(test_params[:entry])
|
||||||
|
|
||||||
instagram_inbox.reload
|
instagram_inbox.reload
|
||||||
|
|
||||||
expect(instagram_inbox.messages.count).to be 1
|
expect(instagram_inbox.messages.count).to be 1
|
||||||
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
|
||||||
|
|
|
@ -489,7 +489,7 @@ RSpec.describe Conversation, type: :model do
|
||||||
let!(:conversation) { create(:conversation, inbox: facebook_inbox, account: facebook_channel.account) }
|
let!(:conversation) { create(:conversation, inbox: facebook_inbox, account: facebook_channel.account) }
|
||||||
|
|
||||||
it 'returns false if there are no incoming messages' do
|
it 'returns false if there are no incoming messages' do
|
||||||
expect(conversation.can_reply?).to eq false
|
expect(conversation.can_reply?).to eq true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'return false if last incoming message is outside of 24 hour window' do
|
it 'return false if last incoming message is outside of 24 hour window' do
|
||||||
|
@ -500,7 +500,7 @@ RSpec.describe Conversation, type: :model do
|
||||||
conversation: conversation,
|
conversation: conversation,
|
||||||
created_at: Time.now - 25.hours
|
created_at: Time.now - 25.hours
|
||||||
)
|
)
|
||||||
expect(conversation.can_reply?).to eq false
|
expect(conversation.can_reply?).to eq true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'return true if last incoming message is inside 24 hour window' do
|
it 'return true if last incoming message is inside 24 hour window' do
|
||||||
|
|
|
@ -60,7 +60,9 @@ describe Facebook::SendOnFacebookService do
|
||||||
::Facebook::SendOnFacebookService.new(message: message).perform
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
||||||
expect(bot).to have_received(:deliver).with({
|
expect(bot).to have_received(:deliver).with({
|
||||||
recipient: { id: contact_inbox.source_id },
|
recipient: { id: contact_inbox.source_id },
|
||||||
message: { text: message.content }
|
message: { text: message.content },
|
||||||
|
messaging_type: 'MESSAGE_TAG',
|
||||||
|
tag: 'ACCOUNT_UPDATE'
|
||||||
}, { page_id: facebook_channel.page_id })
|
}, { page_id: facebook_channel.page_id })
|
||||||
expect(bot).to have_received(:deliver).with({
|
expect(bot).to have_received(:deliver).with({
|
||||||
recipient: { id: contact_inbox.source_id },
|
recipient: { id: contact_inbox.source_id },
|
||||||
|
@ -71,7 +73,9 @@ describe Facebook::SendOnFacebookService do
|
||||||
url: attachment.file_url
|
url: attachment.file_url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
messaging_type: 'MESSAGE_TAG',
|
||||||
|
tag: 'ACCOUNT_UPDATE'
|
||||||
}, { page_id: facebook_channel.page_id })
|
}, { page_id: facebook_channel.page_id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue