feat: send instagram message after 24 hours (#4698)
Added MESSAGE_TAG: HUMAN_AGENT for Instagram messages if the user wants to send it after the standard message window. Fixes #4689
This commit is contained in:
@@ -93,7 +93,7 @@ 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 can_reply_on_instagram? 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?
|
||||||
|
|
||||||
@@ -112,6 +112,18 @@ class Conversation < ApplicationRecord
|
|||||||
Time.current < last_incoming_message.created_at + 24.hours
|
Time.current < last_incoming_message.created_at + 24.hours
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def can_reply_on_instagram?
|
||||||
|
global_config = GlobalConfig.get('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT')
|
||||||
|
|
||||||
|
return false if last_incoming_message.nil?
|
||||||
|
|
||||||
|
if global_config['ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT']
|
||||||
|
Time.current < last_incoming_message.created_at + 7.days
|
||||||
|
else
|
||||||
|
last_message_less_than_24_hrs?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def update_assignee(agent = nil)
|
def update_assignee(agent = nil)
|
||||||
update!(assignee: agent)
|
update!(assignee: agent)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,17 +23,19 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def message_params
|
def message_params
|
||||||
{
|
params = {
|
||||||
recipient: { id: contact.get_source_id(inbox.id) },
|
recipient: { id: contact.get_source_id(inbox.id) },
|
||||||
message: {
|
message: {
|
||||||
text: message.content
|
text: message.content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
merge_human_agent_tag(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachament_message_params
|
def attachament_message_params
|
||||||
attachment = message.attachments.first
|
attachment = message.attachments.first
|
||||||
{
|
params = {
|
||||||
recipient: { id: contact.get_source_id(inbox.id) },
|
recipient: { id: contact.get_source_id(inbox.id) },
|
||||||
message: {
|
message: {
|
||||||
attachment: {
|
attachment: {
|
||||||
@@ -44,6 +46,8 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
merge_human_agent_tag(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Deliver a message with the given payload.
|
# Deliver a message with the given payload.
|
||||||
@@ -96,4 +100,14 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
|||||||
def config
|
def config
|
||||||
Facebook::Messenger.config
|
Facebook::Messenger.config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def merge_human_agent_tag(params)
|
||||||
|
global_config = GlobalConfig.get('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT')
|
||||||
|
|
||||||
|
return params unless global_config['ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT']
|
||||||
|
|
||||||
|
params[:messaging_type] = 'MESSAGE_TAG'
|
||||||
|
params[:tag] = 'HUMAN_AGENT'
|
||||||
|
params
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -59,3 +59,6 @@
|
|||||||
- name: DISABLE_USER_PROFILE_UPDATE
|
- name: DISABLE_USER_PROFILE_UPDATE
|
||||||
value: false
|
value: false
|
||||||
locked: false
|
locked: false
|
||||||
|
- name: ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT
|
||||||
|
value: false
|
||||||
|
locked: false
|
||||||
|
|||||||
@@ -512,6 +512,38 @@ RSpec.describe Conversation, type: :model do
|
|||||||
)
|
)
|
||||||
expect(conversation.can_reply?).to eq true
|
expect(conversation.can_reply?).to eq true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when instagram channel' do
|
||||||
|
it 'return true with HUMAN_AGENT if it is outside of 24 hour window' do
|
||||||
|
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: true)
|
||||||
|
|
||||||
|
conversation.update(additional_attributes: { type: 'instagram_direct_message' })
|
||||||
|
create(
|
||||||
|
:message,
|
||||||
|
account: conversation.account,
|
||||||
|
inbox: facebook_inbox,
|
||||||
|
conversation: conversation,
|
||||||
|
created_at: Time.now - 48.hours
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(conversation.can_reply?).to eq true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'return false without HUMAN_AGENT if it is outside of 24 hour window' do
|
||||||
|
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: false)
|
||||||
|
|
||||||
|
conversation.update(additional_attributes: { type: 'instagram_direct_message' })
|
||||||
|
create(
|
||||||
|
:message,
|
||||||
|
account: conversation.account,
|
||||||
|
inbox: facebook_inbox,
|
||||||
|
conversation: conversation,
|
||||||
|
created_at: Time.now - 48.hours
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(conversation.can_reply?).to eq false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -27,21 +27,68 @@ describe Instagram::SendOnInstagramService do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'if message is sent from chatwoot and is outgoing' do
|
context 'without message_tag HUMAN_AGENT' do
|
||||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
before do
|
||||||
response = ::Instagram::SendOnInstagramService.new(message: message).perform
|
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: false)
|
||||||
|
end
|
||||||
|
|
||||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
it 'if message is sent from chatwoot and is outgoing' do
|
||||||
|
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||||
|
|
||||||
|
allow(HTTParty).to receive(:post).with(
|
||||||
|
{
|
||||||
|
recipient: { id: contact.get_source_id(instagram_inbox.id) },
|
||||||
|
message: {
|
||||||
|
text: message.content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).and_return(
|
||||||
|
{
|
||||||
|
'message_id': 'anyrandommessageid1234567890'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
response = ::Instagram::SendOnInstagramService.new(message: message).perform
|
||||||
|
|
||||||
|
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'if message with attachment is sent from chatwoot and is outgoing' do
|
||||||
|
message = build(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||||
|
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||||
|
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
||||||
|
message.save!
|
||||||
|
response = ::Instagram::SendOnInstagramService.new(message: message).perform
|
||||||
|
|
||||||
|
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'if message with attachment is sent from chatwoot and is outgoing' do
|
context 'with message_tag HUMAN_AGENT' do
|
||||||
message = build(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
before do
|
||||||
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: true)
|
||||||
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
end
|
||||||
message.save!
|
|
||||||
response = ::Instagram::SendOnInstagramService.new(message: message).perform
|
|
||||||
|
|
||||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
it 'if message is sent from chatwoot and is outgoing' do
|
||||||
|
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||||
|
|
||||||
|
allow(HTTParty).to receive(:post).with(
|
||||||
|
{
|
||||||
|
recipient: { id: contact.get_source_id(instagram_inbox.id) },
|
||||||
|
message: {
|
||||||
|
text: message.content
|
||||||
|
},
|
||||||
|
messaging_type: 'MESSAGE_TAG',
|
||||||
|
tag: 'HUMAN_AGENT'
|
||||||
|
}
|
||||||
|
).and_return(
|
||||||
|
{
|
||||||
|
'message_id': 'anyrandommessageid1234567890'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
::Instagram::SendOnInstagramService.new(message: message).perform
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user