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:
@@ -512,6 +512,38 @@ RSpec.describe Conversation, type: :model do
|
||||
)
|
||||
expect(conversation.can_reply?).to eq true
|
||||
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
|
||||
|
||||
|
||||
@@ -27,21 +27,68 @@ describe Instagram::SendOnInstagramService do
|
||||
)
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
response = ::Instagram::SendOnInstagramService.new(message: message).perform
|
||||
context 'without message_tag HUMAN_AGENT' do
|
||||
before do
|
||||
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
|
||||
|
||||
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
|
||||
context 'with message_tag HUMAN_AGENT' do
|
||||
before do
|
||||
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: true)
|
||||
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
|
||||
},
|
||||
messaging_type: 'MESSAGE_TAG',
|
||||
tag: 'HUMAN_AGENT'
|
||||
}
|
||||
).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
}
|
||||
)
|
||||
|
||||
::Instagram::SendOnInstagramService.new(message: message).perform
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user