feat: Instagram Inbox using Instagram Business Login (#11054)
This PR introduces basic minimum version of **Instagram Business Login**, making Instagram inbox setup more straightforward by removing the Facebook Page dependency. This update enhances user experience and aligns with Meta’s recommended best practices. Fixes https://linear.app/chatwoot/issue/CW-3728/instagram-login-how-to-implement-the-changes ## Why Introduce Instagram as a Separate Inbox? Currently, our Instagram integration requires linking an Instagram account to a Facebook Page, making setup complex. To simplify this process, Instagram now offers **Instagram Business Login**, which allows users to authenticate directly with their Instagram credentials. The **Instagram API with Instagram Login** enables businesses and creators to send and receive messages without needing a Facebook Page connection. While an Instagram Business or Creator account is still required, this approach provides a more straightforward integration process. | **Existing Approach (Facebook Login for Business)** | **New Approach (Instagram Business Login)** | | --- | --- | | Requires linking Instagram to a Facebook Page | No Facebook Page required | | Users log in via Facebook credentials | Users log in via Instagram credentials | | Configuration is more complex | Simpler setup | Meta recommends using **Instagram Business Login** as the preferred authentication method due to its easier configuration and improved developer experience. --- ## Implementation Plan The core messaging functionality is already in place, but the transition to **Instagram Business Login** requires adjustments. ### Changes & Considerations - **API Adjustments**: The Instagram API uses `graph.instagram`, whereas Koala (our existing library) interacts with `graph.facebook`. We may need to modify API calls accordingly. - **Three Main Modules**: 1. **Instagram Business Login** – Handle authentication flow. 2. **Permissions & Features** – Ensure necessary API scopes are granted. 3. **Webhooks** – Enable real-time message retrieval.  --- ## Instagram Login Flow 1. User clicks **"Create Inbox"** for Instagram. 2. App redirects to the [Instagram Authorization URL](https://developers.facebook.com/docs/instagram-platform/instagram-api-with-instagram-login/business-login#embed-the-business-login-url). 3. After authentication, Instagram returns an authorization code. 5. The app exchanges the code for a **long-lived token** (valid for 60 days). 6. Tokens are refreshed periodically to maintain access. 7. Once completed, the app creates an inbox and redirects to the Chatwoot dashboard. --- ## How to Test the Instagram Inbox 1. Create a new app on [Meta's Developer Portal](https://developers.facebook.com/apps/). 2. Select **Business** as the app type and configure it. 3. Add the Instagram product and connect a business account. 4. Copy Instagram app ID and Instagram app secret 5. Add the Instagram app ID and Instagram app secret to your app config via `{Chatwoot installation url}/super_admin/app_config?config=instagram` 6. Configure Webhooks: - Callback URL: `{your_chatwoot_url}/webhooks/instagram` - Verify Token: `INSTAGRAM_VERIFY_TOKEN` - Subscribe to `messages`, `messaging_seen`, and `message_reactions` events. 7. Set up **Instagram Business Login**: - Redirect URL: `{your_chatwoot_url}/instagram/callback` 8. Test inbox creation via the Chatwoot dashboard. ## Troubleshooting & Common Errors ### Insufficient Developer Role Error - Ensure the Instagram user is added as a developer: - **Meta Dashboard → App Roles → Roles → Add People → Enter Instagram ID** ### API Access Deactivated - Ensure the **Privacy Policy URL** is valid and correctly set. ### Invalid request: Request parameters are invalid: Invalid redirect_uri - Please configure the Frontend URL. The Frontend URL does not match the authorization URL. --- ## To-Do List - [x] Basic integration setup completed. - [x] Enable sending messages via [Messaging API](https://developers.facebook.com/docs/instagram-platform/instagram-api-with-instagram-login/messaging-api). - [x] Implement automatic webhook subscriptions on inbox creation. - [x] Handle **canceled authorization errors**. - [x] Handle all the errors https://developers.facebook.com/docs/instagram-platform/instagram-graph-api/reference/error-codes - [x] Dynamically fetch **account IDs** instead of hardcoding them. - [x] Prevent duplicate Instagram channel creation for the same account. - [x] Use **Global Config** instead of environment variables. - [x] Explore **Human Agent feature** for message handling. - [x] Write and refine **test cases** for all scenarios. - [x] Implement **token refresh mechanism** (tokens expire after 60 days). Fixes https://github.com/chatwoot/chatwoot/issues/10440 --------- Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
@@ -95,5 +95,33 @@ describe ContactInboxWithContactBuilder do
|
||||
|
||||
expect(contact_inbox.contact.id).to be(contact.id)
|
||||
end
|
||||
|
||||
it 'reuses contact if it exists with the same source_id in a Facebook inbox when creating for Instagram inbox' do
|
||||
instagram_source_id = '123456789'
|
||||
|
||||
# Create a Facebook page inbox with a contact using the same source_id
|
||||
facebook_inbox = create(:inbox, channel_type: 'Channel::FacebookPage', account: account)
|
||||
facebook_contact = create(:contact, account: account)
|
||||
facebook_contact_inbox = create(:contact_inbox, contact: facebook_contact, inbox: facebook_inbox, source_id: instagram_source_id)
|
||||
|
||||
# Create an Instagram inbox
|
||||
instagram_inbox = create(:inbox, channel_type: 'Channel::Instagram', account: account)
|
||||
|
||||
# Try to create a contact inbox with same source_id for Instagram
|
||||
contact_inbox = described_class.new(
|
||||
source_id: instagram_source_id,
|
||||
inbox: instagram_inbox,
|
||||
contact_attributes: {
|
||||
name: 'Instagram User',
|
||||
email: 'instagram_user@example.com'
|
||||
}
|
||||
).perform
|
||||
|
||||
# Should reuse the existing contact from Facebook
|
||||
expect(contact_inbox.contact.id).to eq(facebook_contact.id)
|
||||
# Make sure the contact inbox is not the same as the Facebook contact inbox
|
||||
expect(contact_inbox.id).not_to eq(facebook_contact_inbox.id)
|
||||
expect(contact_inbox.inbox_id).to eq(instagram_inbox.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Messages::Instagram::MessageBuilder do
|
||||
subject(:instagram_message_builder) { described_class }
|
||||
subject(:instagram_direct_message_builder) { described_class }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph.facebook.com/)
|
||||
stub_request(:post, /graph\.instagram\.com/)
|
||||
stub_request(:get, 'https://www.example.com/test.jpeg')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_channel) { create(:channel_instagram, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
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!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access }
|
||||
let!(:shared_reel_params) { build(:instagram_shared_reel_event).with_indifferent_access }
|
||||
let!(:instagram_story_reply_event) { build(:instagram_story_reply_event).with_indifferent_access }
|
||||
let!(:instagram_message_reply_event) { build(:instagram_message_reply_event).with_indifferent_access }
|
||||
let(:fb_object) { double }
|
||||
let(:contact) { create(:contact, id: 'Sender-id-1', name: 'Jane Dae') }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: instagram_inbox.id, source_id: 'Sender-id-1') }
|
||||
let!(:contact) { create(:contact, id: 'Sender-id-1', name: 'Jane Dae') }
|
||||
let!(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: instagram_inbox.id, source_id: 'Sender-id-1') }
|
||||
let(:conversation) do
|
||||
create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id)
|
||||
end
|
||||
let(:message) do
|
||||
create(:message, account_id: account.id, inbox_id: instagram_inbox.id, conversation_id: conversation.id, message_type: 'outgoing',
|
||||
@@ -29,16 +28,27 @@ describe Messages::Instagram::MessageBuilder do
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
it 'creates contact and message for the facebook inbox' do
|
||||
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
|
||||
)
|
||||
before do
|
||||
instagram_channel.update(access_token: 'valid_instagram_token')
|
||||
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/Sender-id-1\?.*})
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: {
|
||||
name: 'Jane',
|
||||
username: 'some_user_name',
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png',
|
||||
id: 'Sender-id-1',
|
||||
follower_count: 100,
|
||||
is_user_follow_business: true,
|
||||
is_business_follow_user: true,
|
||||
is_verified_user: false
|
||||
}.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates contact and message for the instagram direct inbox' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
@@ -48,30 +58,19 @@ describe Messages::Instagram::MessageBuilder do
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
contact = instagram_channel.inbox.contacts.first
|
||||
message = instagram_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
message = instagram_inbox.messages.first
|
||||
expect(message.content).to eq('This is the first message from the customer')
|
||||
end
|
||||
|
||||
it 'discard echo message already sent by chatwoot' do
|
||||
conversation
|
||||
message
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
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
|
||||
)
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
messaging[:message][:mid] = 'message-id-1' # Set same source_id as the existing message
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: true).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
@@ -81,220 +80,244 @@ describe Messages::Instagram::MessageBuilder do
|
||||
end
|
||||
|
||||
it 'creates message for shared reel' do
|
||||
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
|
||||
)
|
||||
messaging = shared_reel_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_channel.inbox.messages.first
|
||||
message = instagram_inbox.messages.first
|
||||
expect(message.attachments.first.file_type).to eq('ig_reel')
|
||||
expect(message.attachments.first.external_url).to eq(
|
||||
shared_reel_params[:entry][0]['messaging'][0]['message']['attachments'][0]['payload']['url']
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates message with for reply with story id' do
|
||||
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
|
||||
)
|
||||
messaging = instagram_story_reply_event[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
it 'creates message with story id' do
|
||||
story_source_id = instagram_story_reply_event[:entry][0]['messaging'][0]['message']['mid']
|
||||
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/#{story_source_id}\?.*})
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: {
|
||||
story: {
|
||||
mention: {
|
||||
id: 'chatwoot-app-user-id-1'
|
||||
}
|
||||
},
|
||||
from: {
|
||||
username: instagram_inbox.channel.instagram_id
|
||||
}
|
||||
}.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
|
||||
messaging = instagram_story_reply_event[:entry][0]['messaging'][0]
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_channel.inbox.messages.first
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This is the story reply')
|
||||
expect(message.content_attributes[:story_sender]).to eq(instagram_inbox.channel.instagram_id)
|
||||
expect(message.content_attributes[:story_id]).to eq('chatwoot-app-user-id-1')
|
||||
expect(message.content_attributes[:story_url]).to eq('https://chatwoot-assets.local/sample.png')
|
||||
end
|
||||
|
||||
it 'creates message with for reply with mid' do
|
||||
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
|
||||
)
|
||||
# create first message to ensure reply to is valid
|
||||
first_message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(first_message, instagram_inbox).perform
|
||||
it 'creates message with reply to mid' do
|
||||
# Create first message to ensure reply to is valid
|
||||
first_messaging = dm_params[:entry][0]['messaging'][0]
|
||||
described_class.new(first_messaging, instagram_inbox).perform
|
||||
|
||||
# create the second message with the reply to mid set
|
||||
# Create second message with reply to mid
|
||||
messaging = instagram_message_reply_event[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
first_message = instagram_channel.inbox.messages.first
|
||||
message = instagram_channel.inbox.messages.last
|
||||
|
||||
expect(message.content).to eq('This is message with replyto mid')
|
||||
expect(message.content_attributes[:in_reply_to_external_id]).to eq(first_message.source_id)
|
||||
expect(message.content_attributes[:in_reply_to]).to eq(first_message.id)
|
||||
first_message = instagram_inbox.messages.first
|
||||
reply_message = instagram_inbox.messages.last
|
||||
|
||||
expect(reply_message.content).to eq('This is message with replyto mid')
|
||||
expect(reply_message.content_attributes[:in_reply_to_external_id]).to eq(first_message.source_id)
|
||||
end
|
||||
|
||||
it 'raises exception on deleted story' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError.new(
|
||||
190,
|
||||
'This Message has been deleted by the user or the business.'
|
||||
))
|
||||
it 'handles deleted story' do
|
||||
story_source_id = story_mention_params[:entry][0][:messaging][0]['message']['mid']
|
||||
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/#{story_source_id}\?.*})
|
||||
.to_return(status: 404, body: { error: { message: 'Story not found', code: 1_609_005 } }.to_json)
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: false).perform
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
# we would have contact created, message created but the empty message because the story mention has been deleted later
|
||||
# As they show it in instagram that this story is no longer available
|
||||
# and external attachments link would be reachable
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
contact = instagram_channel.inbox.contacts.first
|
||||
message = instagram_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(message.content).to eq('This story is no longer available.')
|
||||
expect(message.attachments.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'does not create message for unsupported file type' do
|
||||
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
|
||||
)
|
||||
story_mention_params[:entry][0][:messaging][0]['message']['attachments'][0]['type'] = 'unsupported_type'
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: false).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
|
||||
# we would have contact created but message and attachments won't be created
|
||||
expect(instagram_inbox.conversations.count).to be 0
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 0
|
||||
end
|
||||
|
||||
contact = instagram_channel.inbox.contacts.first
|
||||
it 'does not create message if the message is already exists' do
|
||||
message
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
messaging[:message][:mid] = 'message-id-1' # Set same source_id as the existing message
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: false).perform
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
end
|
||||
|
||||
it 'handles authorization errors' do
|
||||
instagram_channel.update(access_token: 'invalid_token')
|
||||
|
||||
# Stub the request to return authorization error status
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/Sender-id-1\?.*})
|
||||
.to_return(
|
||||
status: 401,
|
||||
body: { error: { message: 'unauthorized access token', code: 190 } }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
# The method should complete without raising an error
|
||||
expect do
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is disabled' do
|
||||
before do
|
||||
instagram_inbox.update!(lock_to_single_conversation: false)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
described_class.new(message, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
contact_inbox.reload
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
expect(Conversation.count).to eq(initial_count + 1)
|
||||
end
|
||||
|
||||
it 'will not create a new conversation if last conversation is not resolved' do
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id, status: :open,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id,
|
||||
contact_id: contact.id, status: :open)
|
||||
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
contact_inbox.reload
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if last conversation is resolved' do
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id, status: :resolved,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id,
|
||||
contact_id: contact.id, status: :resolved)
|
||||
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
described_class.new(message, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
contact_inbox.reload
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.last.id).not_to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
expect(Conversation.count).to eq(initial_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is enabled' do
|
||||
before do
|
||||
instagram_inbox.update!(lock_to_single_conversation: true)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
described_class.new(message, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
contact_inbox.reload
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
expect(Conversation.count).to eq(initial_count + 1)
|
||||
end
|
||||
|
||||
it 'reopens last conversation if last conversation is resolved' do
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id, status: :resolved,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id,
|
||||
contact_id: contact.id, status: :resolved)
|
||||
|
||||
inital_count = Conversation.count
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
contact_inbox.reload
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count)
|
||||
expect(Conversation.count).to eq(initial_count)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fetch_story_link' do
|
||||
let(:story_data) do
|
||||
{
|
||||
'story' => {
|
||||
'mention' => {
|
||||
'link' => 'https://example.com/story-link',
|
||||
'id' => '18094414321535710'
|
||||
}
|
||||
},
|
||||
'from' => {
|
||||
'username' => 'instagram_user',
|
||||
'id' => '2450757355263608'
|
||||
},
|
||||
'id' => 'story-source-id-123'
|
||||
}.with_indifferent_access
|
||||
end
|
||||
|
||||
before do
|
||||
# Stub the HTTP request to Instagram API
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?fields=story,from})
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: story_data.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'saves story information when story mention is processed' do
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to include('instagram_user')
|
||||
expect(message.attachments.count).to eq(1)
|
||||
expect(message.content_attributes[:story_sender]).to eq('instagram_user')
|
||||
expect(message.content_attributes[:story_id]).to eq('18094414321535710')
|
||||
expect(message.content_attributes[:image_type]).to eq('story_mention')
|
||||
end
|
||||
|
||||
it 'handles deleted stories' do
|
||||
# Override the stub for this test to return a 404 error
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?fields=story,from})
|
||||
.to_return(
|
||||
status: 404,
|
||||
body: { error: { message: 'Story not found', code: 1_609_005 } }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This story is no longer available.')
|
||||
expect(message.attachments.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,378 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Messages::Instagram::Messenger::MessageBuilder do
|
||||
subject(:instagram_message_builder) { described_class }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
stub_request(:get, 'https://www.example.com/test.jpeg')
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:instagram_messenger_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_messenger_inbox) { create(:inbox, channel: instagram_messenger_channel, account: account, greeting_enabled: false) }
|
||||
let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access }
|
||||
let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access }
|
||||
let!(:shared_reel_params) { build(:instagram_shared_reel_event).with_indifferent_access }
|
||||
let!(:instagram_story_reply_event) { build(:instagram_story_reply_event).with_indifferent_access }
|
||||
let!(:instagram_message_reply_event) { build(:instagram_message_reply_event).with_indifferent_access }
|
||||
let(:fb_object) { double }
|
||||
let(:contact) { create(:contact, id: 'Sender-id-1', name: 'Jane Dae') }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: instagram_messenger_inbox.id, source_id: 'Sender-id-1') }
|
||||
let(:conversation) do
|
||||
create(:conversation, account_id: account.id, inbox_id: instagram_messenger_inbox.id, contact_id: contact.id,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
end
|
||||
let(:message) do
|
||||
create(:message, account_id: account.id, inbox_id: instagram_messenger_inbox.id, conversation_id: conversation.id, message_type: 'outgoing',
|
||||
source_id: 'message-id-1')
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
it 'creates contact and message for the facebook inbox' do
|
||||
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_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
contact = instagram_messenger_channel.inbox.contacts.first
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(message.content).to eq('This is the first message from the customer')
|
||||
end
|
||||
|
||||
it 'discard echo message already sent by chatwoot' do
|
||||
message
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
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_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_messenger_inbox, outgoing_echo: true).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
end
|
||||
|
||||
it 'creates message for shared reel' do
|
||||
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_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
messaging = shared_reel_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
expect(message.attachments.first.file_type).to eq('ig_reel')
|
||||
expect(message.attachments.first.external_url).to eq(
|
||||
shared_reel_params[:entry][0]['messaging'][0]['message']['attachments'][0]['payload']['url']
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates message with for reply with story id' do
|
||||
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_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
messaging = instagram_story_reply_event[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This is the story reply')
|
||||
expect(message.content_attributes[:story_sender]).to eq(instagram_messenger_inbox.channel.instagram_id)
|
||||
expect(message.content_attributes[:story_id]).to eq('chatwoot-app-user-id-1')
|
||||
expect(message.content_attributes[:story_url]).to eq('https://chatwoot-assets.local/sample.png')
|
||||
end
|
||||
|
||||
it 'creates message with for reply with mid' do
|
||||
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_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
# create first message to ensure reply to is valid
|
||||
first_message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
described_class.new(first_message, instagram_messenger_inbox).perform
|
||||
|
||||
# create the second message with the reply to mid set
|
||||
messaging = instagram_message_reply_event[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
first_message = instagram_messenger_channel.inbox.messages.first
|
||||
message = instagram_messenger_channel.inbox.messages.last
|
||||
|
||||
expect(message.content).to eq('This is message with replyto mid')
|
||||
expect(message.content_attributes[:in_reply_to_external_id]).to eq(first_message.source_id)
|
||||
expect(message.content_attributes[:in_reply_to]).to eq(first_message.id)
|
||||
end
|
||||
|
||||
it 'raises exception on deleted story' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError.new(
|
||||
190,
|
||||
'This Message has been deleted by the user or the business.'
|
||||
))
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_messenger_inbox, outgoing_echo: false).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
# we would have contact created, message created but the empty message because the story mention has been deleted later
|
||||
# As they show it in instagram that this story is no longer available
|
||||
# and external attachments link would be reachable
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
contact = instagram_messenger_channel.inbox.contacts.first
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(message.content).to eq('This story is no longer available.')
|
||||
expect(message.attachments.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'does not create message for unsupported file type' do
|
||||
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_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
story_mention_params[:entry][0][:messaging][0]['message']['attachments'][0]['type'] = 'unsupported_type'
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_messenger_inbox, outgoing_echo: false).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
# we would have contact created but message and attachments won't be created
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 0
|
||||
|
||||
contact = instagram_messenger_channel.inbox.contacts.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is disabled' do
|
||||
before do
|
||||
instagram_messenger_inbox.update!(lock_to_single_conversation: false)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
contact_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
|
||||
it 'will not create a new conversation if last conversation is not resolved' do
|
||||
existing_conversation = create(
|
||||
:conversation,
|
||||
account_id: account.id,
|
||||
inbox_id: instagram_messenger_inbox.id,
|
||||
contact_id: contact.id,
|
||||
status: :open,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' }
|
||||
)
|
||||
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
contact_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if last conversation is resolved' do
|
||||
existing_conversation = create(
|
||||
:conversation,
|
||||
account_id: account.id,
|
||||
inbox_id: instagram_messenger_inbox.id,
|
||||
contact_id: contact.id,
|
||||
status: :resolved,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' }
|
||||
)
|
||||
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
contact_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.last.id).not_to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is enabled' do
|
||||
before do
|
||||
instagram_messenger_inbox.update!(lock_to_single_conversation: true)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
contact_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
|
||||
it 'reopens last conversation if last conversation is resolved' do
|
||||
existing_conversation = create(
|
||||
:conversation,
|
||||
account_id: account.id,
|
||||
inbox_id: instagram_messenger_inbox.id,
|
||||
contact_id: contact.id,
|
||||
status: :resolved,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' }
|
||||
)
|
||||
|
||||
inital_count = Conversation.count
|
||||
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
contact_inbox
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
contact_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fetch_story_link' do
|
||||
before do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
end
|
||||
|
||||
let(:story_data) do
|
||||
{
|
||||
'story' => {
|
||||
'mention' => {
|
||||
'link' => 'https://example.com/story-link',
|
||||
'id' => '18094414321535710'
|
||||
}
|
||||
},
|
||||
'from' => {
|
||||
'username' => 'instagram_user',
|
||||
'id' => '2450757355263608'
|
||||
},
|
||||
'id' => 'story-source-id-123'
|
||||
}.with_indifferent_access
|
||||
end
|
||||
|
||||
it 'saves story information when story mention is processed' do
|
||||
allow(fb_object).to receive(:get_object).and_return(story_data)
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact_inbox
|
||||
builder = described_class.new(messaging, instagram_messenger_inbox)
|
||||
builder.perform
|
||||
|
||||
message = instagram_messenger_inbox.messages.first
|
||||
|
||||
expect(message.content).to include('instagram_user')
|
||||
expect(message.attachments.count).to eq(1)
|
||||
expect(message.content_attributes[:story_sender]).to eq('instagram_user')
|
||||
expect(message.content_attributes[:story_id]).to eq('18094414321535710')
|
||||
expect(message.content_attributes[:image_type]).to eq('story_mention')
|
||||
end
|
||||
|
||||
it 'handles story mentions specifically in the Instagram builder' do
|
||||
# First allow contact info fetch
|
||||
allow(fb_object).to receive(:get_object).and_return({
|
||||
name: 'Jane',
|
||||
id: 'Sender-id-1'
|
||||
}.with_indifferent_access)
|
||||
|
||||
# Then allow story data fetch
|
||||
allow(fb_object).to receive(:get_object).with(anything, fields: %w[story from])
|
||||
.and_return(story_data)
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact_inbox
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
message = instagram_messenger_inbox.messages.first
|
||||
|
||||
expect(message.content_attributes[:story_sender]).to eq('instagram_user')
|
||||
expect(message.content_attributes[:image_type]).to eq('story_mention')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -34,25 +34,21 @@ RSpec.describe V2::Reports::InboxSummaryBuilder do
|
||||
let(:business_hours) { false }
|
||||
|
||||
it 'includes correct stats for each inbox' do
|
||||
expect(report).to eq(
|
||||
[
|
||||
{
|
||||
id: i1.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 0,
|
||||
avg_resolution_time: nil,
|
||||
avg_first_response_time: 50.0,
|
||||
avg_reply_time: 35.0
|
||||
}, {
|
||||
id: i2.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 1,
|
||||
avg_resolution_time: 100.0,
|
||||
avg_first_response_time: nil,
|
||||
avg_reply_time: nil
|
||||
}
|
||||
]
|
||||
)
|
||||
expect(report).to contain_exactly({
|
||||
id: i1.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 0,
|
||||
avg_resolution_time: nil,
|
||||
avg_first_response_time: 50.0,
|
||||
avg_reply_time: 35.0
|
||||
}, {
|
||||
id: i2.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 1,
|
||||
avg_resolution_time: 100.0,
|
||||
avg_first_response_time: nil,
|
||||
avg_reply_time: nil
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,25 +56,21 @@ RSpec.describe V2::Reports::InboxSummaryBuilder do
|
||||
let(:business_hours) { true }
|
||||
|
||||
it 'uses business hours values for calculations' do
|
||||
expect(report).to eq(
|
||||
[
|
||||
{
|
||||
id: i1.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 0,
|
||||
avg_resolution_time: nil,
|
||||
avg_first_response_time: 30.0,
|
||||
avg_reply_time: 15.0
|
||||
}, {
|
||||
id: i2.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 1,
|
||||
avg_resolution_time: 60.0,
|
||||
avg_first_response_time: nil,
|
||||
avg_reply_time: nil
|
||||
}
|
||||
]
|
||||
)
|
||||
expect(report).to contain_exactly({
|
||||
id: i1.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 0,
|
||||
avg_resolution_time: nil,
|
||||
avg_first_response_time: 30.0,
|
||||
avg_reply_time: 15.0
|
||||
}, {
|
||||
id: i2.id,
|
||||
conversations_count: 1,
|
||||
resolved_conversations_count: 1,
|
||||
avg_resolution_time: 60.0,
|
||||
avg_first_response_time: nil,
|
||||
avg_reply_time: nil
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -49,8 +49,24 @@ RSpec.describe Instagram::CallbacksController do
|
||||
expect(Channel::Instagram.last.instagram_id).to eq('12345')
|
||||
expect(Inbox.last.name).to eq('test_user')
|
||||
|
||||
expect(Inbox.last.channel.reauthorization_required?).to be false
|
||||
expect(response).to redirect_to(app_instagram_inbox_agents_url(account_id: account.id, inbox_id: Inbox.last.id))
|
||||
end
|
||||
|
||||
it 'updates existing channel with new token' do
|
||||
# Create an existing channel
|
||||
existing_channel = create(:channel_instagram, account: account, instagram_id: '12345', access_token: 'old_token')
|
||||
create(:inbox, channel: existing_channel, account: account, name: 'old_username')
|
||||
|
||||
expect do
|
||||
get :show, params: valid_params
|
||||
end.to not_change(Channel::Instagram, :count).and not_change(Inbox, :count)
|
||||
|
||||
existing_channel.reload
|
||||
expect(existing_channel.access_token).to eq('long_lived_test_token')
|
||||
expect(existing_channel.instagram_id).to eq('12345')
|
||||
expect(existing_channel.reauthorization_required?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user denies authorization' do
|
||||
|
||||
@@ -256,7 +256,7 @@ FactoryBot.define do
|
||||
},
|
||||
'timestamp': '2021-09-08T06:34:04+0000',
|
||||
'message': {
|
||||
'mid': 'message-id-1',
|
||||
'mid': 'mention-message-id-1',
|
||||
'attachments': [
|
||||
{
|
||||
'type': 'story_mention',
|
||||
|
||||
@@ -84,5 +84,29 @@ RSpec.describe SendReplyJob do
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
|
||||
it 'calls ::Instagram::Direct::SendOnInstagramService when its instagram message' do
|
||||
instagram_channel = create(:channel_instagram)
|
||||
message = create(:message, conversation: create(:conversation, inbox: instagram_channel.inbox))
|
||||
allow(Instagram::SendOnInstagramService).to receive(:new).with(message: message).and_return(process_service)
|
||||
expect(Instagram::SendOnInstagramService).to receive(:new).with(message: message)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
|
||||
it 'calls ::Instagram::Messenger::SendOnInstagramService when its an instagram_direct_message from facebook channel' do
|
||||
stub_request(:post, /graph.facebook.com/)
|
||||
facebook_channel = create(:channel_facebook_page)
|
||||
facebook_inbox = create(:inbox, channel: facebook_channel)
|
||||
conversation = create(:conversation,
|
||||
inbox: facebook_inbox,
|
||||
additional_attributes: { 'type' => 'instagram_direct_message' })
|
||||
message = create(:message, conversation: conversation)
|
||||
|
||||
allow(Instagram::Messenger::SendOnInstagramService).to receive(:new).with(message: message).and_return(process_service)
|
||||
expect(Instagram::Messenger::SendOnInstagramService).to receive(:new).with(message: message)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
require 'rails_helper'
|
||||
require 'webhooks/twitter'
|
||||
|
||||
describe Webhooks::InstagramEventsJob do
|
||||
subject(:instagram_webhook) { described_class }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph.facebook.com/)
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
stub_request(:get, 'https://www.example.com/test.jpeg')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
end
|
||||
@@ -14,39 +13,52 @@ describe Webhooks::InstagramEventsJob do
|
||||
let(:return_object) do
|
||||
{ name: 'Jane',
|
||||
id: 'Sender-id-1',
|
||||
account_id: instagram_inbox.account_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png',
|
||||
username: 'some_user_name' }
|
||||
end
|
||||
let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_messenger_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_messenger_inbox) { create(:inbox, channel: instagram_messenger_channel, account: account, greeting_enabled: false) }
|
||||
|
||||
let!(:instagram_channel) { create(:channel_instagram, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
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!(:standby_params) { build(:instagram_message_standby_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!(: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!(:messaging_seen_event) { build(:messaging_seen_event).with_indifferent_access }
|
||||
let!(:unsupported_message_event) { build(:instagram_message_unsupported_event).with_indifferent_access }
|
||||
let(:fb_object) { double }
|
||||
|
||||
# Combined message events into one helper
|
||||
let(:message_events) do
|
||||
{
|
||||
dm: build(:instagram_message_create_event).with_indifferent_access,
|
||||
standby: build(:instagram_message_standby_event).with_indifferent_access,
|
||||
unsend: build(:instagram_message_unsend_event).with_indifferent_access,
|
||||
attachment: build(:instagram_message_attachment_event).with_indifferent_access,
|
||||
story_mention: build(:instagram_story_mention_event).with_indifferent_access,
|
||||
story_mention_echo: build(:instagram_story_mention_event_with_echo).with_indifferent_access,
|
||||
messaging_seen: build(:messaging_seen_event).with_indifferent_access,
|
||||
unsupported: build(:instagram_message_unsupported_event).with_indifferent_access
|
||||
}
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'with direct_message params' do
|
||||
context 'when handling messaging events for Instagram via Facebook page' do
|
||||
let(:fb_object) { double }
|
||||
|
||||
before do
|
||||
instagram_inbox.destroy
|
||||
end
|
||||
|
||||
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_object.with_indifferent_access
|
||||
)
|
||||
instagram_webhook.perform_now(dm_params[:entry])
|
||||
instagram_webhook.perform_now(message_events[:dm][:entry])
|
||||
|
||||
instagram_inbox.reload
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to be 1
|
||||
expect(instagram_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.content_attributes['is_unsupported']).to be_nil
|
||||
expect(instagram_messenger_inbox.contacts.count).to be 1
|
||||
expect(instagram_messenger_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.last.content_attributes['is_unsupported']).to be_nil
|
||||
end
|
||||
|
||||
it 'creates standby message in the instagram inbox' do
|
||||
@@ -54,53 +66,40 @@ describe Webhooks::InstagramEventsJob do
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
return_object.with_indifferent_access
|
||||
)
|
||||
instagram_webhook.perform_now(standby_params[:entry])
|
||||
instagram_webhook.perform_now(message_events[:standby][:entry])
|
||||
|
||||
instagram_inbox.reload
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to be 1
|
||||
expect(instagram_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_messenger_inbox.contacts.count).to be 1
|
||||
expect(instagram_messenger_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
message = instagram_inbox.messages.last
|
||||
message = instagram_messenger_inbox.messages.last
|
||||
expect(message.content).to eq('This is the first standby message from the customer, after 24 hours.')
|
||||
end
|
||||
|
||||
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_object.with_indifferent_access
|
||||
)
|
||||
instagram_webhook.perform_now(test_params[:entry])
|
||||
|
||||
instagram_inbox.reload
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.content).to eq('random_text')
|
||||
expect(instagram_inbox.messages.last.source_id).to eq('random_mid')
|
||||
end
|
||||
|
||||
it 'handle instagram unsend message event' do
|
||||
message = create(:message, inbox_id: instagram_inbox.id, source_id: 'message-id-to-delete')
|
||||
message = create(:message, inbox_id: instagram_messenger_inbox.id, source_id: 'message-id-to-delete')
|
||||
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,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
message.attachments.new(file_type: :image, external_url: 'https://www.example.com/test.jpeg')
|
||||
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
instagram_webhook.perform_now(unsend_event[:entry])
|
||||
instagram_webhook.perform_now(message_events[:unsend][:entry])
|
||||
|
||||
expect(instagram_inbox.messages.last.content).to eq 'This message was deleted'
|
||||
expect(instagram_inbox.messages.last.deleted).to be true
|
||||
expect(instagram_inbox.messages.last.attachments.count).to be 0
|
||||
expect(instagram_inbox.messages.last.reload.deleted).to be true
|
||||
expect(instagram_messenger_inbox.messages.last.content).to eq 'This message was deleted'
|
||||
expect(instagram_messenger_inbox.messages.last.deleted).to be true
|
||||
expect(instagram_messenger_inbox.messages.last.attachments.count).to be 0
|
||||
expect(instagram_messenger_inbox.messages.last.reload.deleted).to be true
|
||||
end
|
||||
|
||||
it 'creates incoming message with attachments in the instagram inbox' do
|
||||
@@ -108,13 +107,13 @@ describe Webhooks::InstagramEventsJob do
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
return_object.with_indifferent_access
|
||||
)
|
||||
instagram_webhook.perform_now(attachment_params[:entry])
|
||||
instagram_webhook.perform_now(message_events[:attachment][:entry])
|
||||
|
||||
instagram_inbox.reload
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.attachments.count).to be 1
|
||||
expect(instagram_messenger_inbox.contacts.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.last.attachments.count).to be 1
|
||||
end
|
||||
|
||||
it 'creates incoming message with attachments in the instagram inbox for story mention' do
|
||||
@@ -134,22 +133,145 @@ describe Webhooks::InstagramEventsJob do
|
||||
id: 'instagram-message-id-1234' }.with_indifferent_access
|
||||
)
|
||||
|
||||
instagram_webhook.perform_now(story_mention_params[:entry])
|
||||
instagram_webhook.perform_now(message_events[:story_mention][:entry])
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.last.attachments.count).to be 1
|
||||
|
||||
attachment = instagram_messenger_inbox.messages.last.attachments.last
|
||||
expect(attachment.push_event_data[:data_url]).to eq(attachment.external_url)
|
||||
end
|
||||
|
||||
it 'does not create contact or messages when Facebook API call fails' 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(message_events[:story_mention_echo][:entry])
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.contacts.count).to be 0
|
||||
expect(instagram_messenger_inbox.contact_inboxes.count).to be 0
|
||||
expect(instagram_messenger_inbox.messages.count).to be 0
|
||||
end
|
||||
|
||||
it 'handle messaging_seen callback' do
|
||||
expect(Instagram::ReadStatusService).to receive(:new).with(params: message_events[:messaging_seen][:entry][0][:messaging][0],
|
||||
channel: instagram_messenger_inbox.channel).and_call_original
|
||||
instagram_webhook.perform_now(message_events[:messaging_seen][:entry])
|
||||
end
|
||||
|
||||
it 'handles unsupported message' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
return_object.with_indifferent_access
|
||||
)
|
||||
|
||||
instagram_webhook.perform_now(message_events[:unsupported][:entry])
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.contacts.count).to be 1
|
||||
expect(instagram_messenger_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.last.content_attributes['is_unsupported']).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when handling messaging events for Instagram via Instagram login' do
|
||||
before do
|
||||
instagram_channel.update(access_token: 'valid_instagram_token')
|
||||
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/v22\.0/Sender-id-1\?.*})
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: {
|
||||
name: 'Jane',
|
||||
username: 'some_user_name',
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png',
|
||||
id: 'Sender-id-1',
|
||||
follower_count: 100,
|
||||
is_user_follow_business: true,
|
||||
is_business_follow_user: true,
|
||||
is_verified_user: false
|
||||
}.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates incoming message with correct contact info in the instagram direct inbox' do
|
||||
instagram_webhook.perform_now(message_events[:dm][:entry])
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to eq 1
|
||||
expect(instagram_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_inbox.conversations.count).to eq 1
|
||||
expect(instagram_inbox.messages.count).to eq 1
|
||||
expect(instagram_inbox.messages.last.content_attributes['is_unsupported']).to be_nil
|
||||
end
|
||||
|
||||
it 'sets correct instagram attributes on contact' do
|
||||
instagram_webhook.perform_now(message_events[:dm][:entry])
|
||||
instagram_inbox.reload
|
||||
|
||||
contact = instagram_inbox.contacts.last
|
||||
|
||||
expect(contact.additional_attributes['social_instagram_follower_count']).to eq 100
|
||||
expect(contact.additional_attributes['social_instagram_is_user_follow_business']).to be true
|
||||
expect(contact.additional_attributes['social_instagram_is_business_follow_user']).to be true
|
||||
expect(contact.additional_attributes['social_instagram_is_verified_user']).to be false
|
||||
end
|
||||
|
||||
it 'handle instagram unsend message event' do
|
||||
message = create(:message, inbox_id: instagram_inbox.id, source_id: 'message-id-to-delete', content: 'random_text')
|
||||
|
||||
# Create attachment correctly with account association
|
||||
message.attachments.create!(
|
||||
file_type: :image,
|
||||
external_url: 'https://www.example.com/test.jpeg',
|
||||
account_id: instagram_inbox.account_id
|
||||
)
|
||||
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.attachments.count).to be 1
|
||||
|
||||
attachment = instagram_inbox.messages.last.attachments.last
|
||||
expect(attachment.push_event_data[:data_url]).to eq(attachment.external_url)
|
||||
instagram_webhook.perform_now(message_events[:unsend][:entry])
|
||||
|
||||
expect(instagram_inbox.messages.last.content).to eq 'This message was deleted'
|
||||
expect(instagram_inbox.messages.last.deleted).to be true
|
||||
expect(instagram_inbox.messages.last.attachments.count).to be 0
|
||||
expect(instagram_inbox.messages.last.reload.deleted).to be true
|
||||
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)
|
||||
it 'creates incoming message with attachments in the instagram direct inbox' do
|
||||
instagram_webhook.perform_now(message_events[:attachment][:entry])
|
||||
|
||||
instagram_webhook.perform_now(story_mention_echo_params[:entry])
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.attachments.count).to be 1
|
||||
end
|
||||
|
||||
it 'handles unsupported message' do
|
||||
instagram_webhook.perform_now(message_events[:unsupported][:entry])
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to be 1
|
||||
expect(instagram_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.content_attributes['is_unsupported']).to be true
|
||||
end
|
||||
|
||||
it 'does not create contact or messages when Instagram API call fails' do
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/v22\.0/.*\?.*})
|
||||
.to_return(status: 401, body: { error: { message: 'Invalid OAuth access token' } }.to_json)
|
||||
|
||||
instagram_webhook.perform_now(message_events[:story_mention_echo][:entry])
|
||||
|
||||
instagram_inbox.reload
|
||||
|
||||
@@ -159,24 +281,9 @@ describe Webhooks::InstagramEventsJob do
|
||||
end
|
||||
|
||||
it 'handle messaging_seen callback' do
|
||||
expect(Instagram::ReadStatusService).to receive(:new).with(params: messaging_seen_event[:entry][0][:messaging][0]).and_call_original
|
||||
instagram_webhook.perform_now(messaging_seen_event[:entry])
|
||||
end
|
||||
|
||||
it 'handles unsupported message' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
return_object.with_indifferent_access
|
||||
)
|
||||
|
||||
instagram_webhook.perform_now(unsupported_message_event[:entry])
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.contacts.count).to be 1
|
||||
expect(instagram_inbox.contacts.last.additional_attributes['social_instagram_user_name']).to eq 'some_user_name'
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.content_attributes['is_unsupported']).to be true
|
||||
expect(Instagram::ReadStatusService).to receive(:new).with(params: message_events[:messaging_seen][:entry][0][:messaging][0],
|
||||
channel: instagram_inbox.channel).and_call_original
|
||||
instagram_webhook.perform_now(message_events[:messaging_seen][:entry])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Instagram::Messenger::SendOnInstagramService do
|
||||
subject(:send_reply_service) { described_class.new(message: message) }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
create(:message, message_type: :incoming, inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_messenger_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: instagram_messenger_inbox) }
|
||||
let(:conversation) { create(:conversation, contact: contact, inbox: instagram_messenger_inbox, contact_inbox: contact_inbox) }
|
||||
let(:response) { double }
|
||||
let(:mock_response) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => { message_id: 'anyrandommessageid1234567890' }.to_json,
|
||||
:parsed_response => { 'message_id' => 'anyrandommessageid1234567890' }
|
||||
)
|
||||
end
|
||||
|
||||
let(:error_body) do
|
||||
{
|
||||
'error' => {
|
||||
'message' => 'The Instagram account is restricted.',
|
||||
'type' => 'OAuthException',
|
||||
'code' => 400,
|
||||
'fbtrace_id' => 'anyrandomfbtraceid1234567890'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
let(:error_response) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => false,
|
||||
:body => error_body.to_json,
|
||||
:parsed_response => error_body
|
||||
)
|
||||
end
|
||||
|
||||
let(:response_with_error) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => error_body.to_json,
|
||||
:parsed_response => error_body
|
||||
)
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'with reply' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
allow(HTTParty).to receive(:post).and_return(mock_response)
|
||||
end
|
||||
|
||||
context 'without message_tag HUMAN_AGENT' do
|
||||
before do
|
||||
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: false)
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
|
||||
response = described_class.new(message: message).perform
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing with multiple attachments' do
|
||||
message = build(
|
||||
:message,
|
||||
content: nil,
|
||||
message_type: 'outgoing',
|
||||
inbox: instagram_messenger_inbox,
|
||||
account: account,
|
||||
conversation: conversation
|
||||
)
|
||||
avatar = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||
avatar.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
|
||||
sample = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||
sample.file.attach(io: Rails.root.join('spec/assets/sample.png').open, filename: 'sample.png', content_type: 'image/png')
|
||||
message.save!
|
||||
|
||||
service = described_class.new(message: message)
|
||||
|
||||
# Stub the send_message method on the service instance
|
||||
allow(service).to receive(:send_message)
|
||||
service.perform
|
||||
|
||||
# Now you can set expectations on the stubbed method for each attachment
|
||||
expect(service).to have_received(:send_message).exactly(:twice)
|
||||
end
|
||||
|
||||
it 'if message with attachment is sent from chatwoot and is outgoing' do
|
||||
message = build(:message, message_type: 'outgoing', inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
|
||||
message.save!
|
||||
response = described_class.new(message: message).perform
|
||||
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
end
|
||||
|
||||
it 'if message sent from chatwoot is failed' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(response_with_error)
|
||||
described_class.new(message: message).perform
|
||||
expect(HTTParty).to have_received(:post)
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with message_tag HUMAN_AGENT' do
|
||||
before do
|
||||
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: true)
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
|
||||
allow(HTTParty).to receive(:post).with(
|
||||
{
|
||||
recipient: { id: contact.get_source_id(instagram_messenger_inbox.id) },
|
||||
message: {
|
||||
text: message.content
|
||||
},
|
||||
messaging_type: 'MESSAGE_TAG',
|
||||
tag: 'HUMAN_AGENT'
|
||||
}
|
||||
).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
}
|
||||
)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(HTTParty).to have_received(:post)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when handling errors' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
end
|
||||
|
||||
it 'handles HTTP errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
|
||||
end
|
||||
|
||||
it 'handles response errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_messenger_inbox, account: account, conversation: conversation)
|
||||
|
||||
error_response = instance_double(
|
||||
HTTParty::Response,
|
||||
success?: true,
|
||||
body: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }.to_json,
|
||||
parsed_response: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }
|
||||
)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('100 - Invalid message format')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -30,7 +30,7 @@ describe Instagram::ReadStatusService do
|
||||
mid: message.source_id
|
||||
}
|
||||
}
|
||||
described_class.new(params: params).perform
|
||||
described_class.new(params: params, channel: instagram_channel).perform
|
||||
expect(Conversations::UpdateMessageStatusJob).to have_received(:perform_later).with(conversation.id, message.created_at)
|
||||
end
|
||||
|
||||
@@ -43,7 +43,7 @@ describe Instagram::ReadStatusService do
|
||||
mid: 'random-message-id'
|
||||
}
|
||||
}
|
||||
described_class.new(params: params).perform
|
||||
described_class.new(params: params, channel: instagram_channel).perform
|
||||
expect(Conversations::UpdateMessageStatusJob).not_to have_received(:perform_later)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,14 +3,10 @@ require 'rails_helper'
|
||||
describe Instagram::SendOnInstagramService do
|
||||
subject(:send_reply_service) { described_class.new(message: message) }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
create(:message, message_type: :incoming, inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_channel) { create(:channel_instagram, account: account, instagram_id: 'instagram-message-id-123') }
|
||||
let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
|
||||
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: instagram_inbox) }
|
||||
let(:conversation) { create(:conversation, contact: contact, inbox: instagram_inbox, contact_inbox: contact_inbox) }
|
||||
@@ -19,8 +15,8 @@ describe Instagram::SendOnInstagramService do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => { message_id: 'anyrandommessageid1234567890' }.to_json,
|
||||
:parsed_response => { 'message_id' => 'anyrandommessageid1234567890' }
|
||||
:body => { message_id: 'random_message_id' }.to_json,
|
||||
:parsed_response => { 'message_id' => 'random_message_id' }
|
||||
)
|
||||
end
|
||||
|
||||
@@ -56,24 +52,24 @@ describe Instagram::SendOnInstagramService do
|
||||
describe '#perform' do
|
||||
context 'with reply' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
allow(HTTParty).to receive(:post).and_return(mock_response)
|
||||
end
|
||||
|
||||
context 'without message_tag HUMAN_AGENT' do
|
||||
before do
|
||||
InstallationConfig.where(name: 'ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT').first_or_create(value: false)
|
||||
InstallationConfig.where(name: 'ENABLE_INSTAGRAM_CHANNEL_HUMAN_AGENT').first_or_create(value: false)
|
||||
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 = described_class.new(message: message).perform
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
expect(response['message_id']).to eq('random_message_id')
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing with multiple attachments' do
|
||||
message = build(:message, content: nil, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
message = build(:message, content: nil, message_type: 'outgoing', inbox: instagram_inbox, account: account,
|
||||
conversation: conversation)
|
||||
avatar = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||
avatar.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
|
||||
sample = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||
@@ -82,12 +78,12 @@ describe Instagram::SendOnInstagramService do
|
||||
|
||||
service = described_class.new(message: message)
|
||||
|
||||
# Stub the send_to_facebook_page method on the service instance
|
||||
allow(service).to receive(:send_to_facebook_page)
|
||||
# Stub the send_message method on the service instance
|
||||
allow(service).to receive(:send_message)
|
||||
service.perform
|
||||
|
||||
# Now you can set expectations on the stubbed method for each attachment
|
||||
expect(service).to have_received(:send_to_facebook_page).exactly(:twice)
|
||||
expect(service).to have_received(:send_message).exactly(:twice)
|
||||
end
|
||||
|
||||
it 'if message with attachment is sent from chatwoot and is outgoing' do
|
||||
@@ -97,7 +93,7 @@ describe Instagram::SendOnInstagramService do
|
||||
message.save!
|
||||
response = described_class.new(message: message).perform
|
||||
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
expect(response['message_id']).to eq('random_message_id')
|
||||
end
|
||||
|
||||
it 'if message sent from chatwoot is failed' do
|
||||
@@ -130,7 +126,7 @@ describe Instagram::SendOnInstagramService do
|
||||
}
|
||||
).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
'message_id': 'random_message_id'
|
||||
}
|
||||
)
|
||||
|
||||
@@ -138,39 +134,54 @@ describe Instagram::SendOnInstagramService do
|
||||
expect(HTTParty).to have_received(:post)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when handling errors' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
end
|
||||
context 'when handling errors' do
|
||||
it 'handles HTTP errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
it 'handles HTTP errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
described_class.new(message: message).perform
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
|
||||
end
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
|
||||
end
|
||||
it 'handles response errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
it 'handles response errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
error_response = instance_double(
|
||||
HTTParty::Response,
|
||||
success?: true,
|
||||
body: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }.to_json,
|
||||
parsed_response: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }
|
||||
)
|
||||
|
||||
error_response = instance_double(
|
||||
HTTParty::Response,
|
||||
success?: true,
|
||||
body: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }.to_json,
|
||||
parsed_response: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }
|
||||
)
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
described_class.new(message: message).perform
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('100 - Invalid message format')
|
||||
end
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('100 - Invalid message format')
|
||||
it 'handles reauthorization errors if access token is expired' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
error_response = instance_double(
|
||||
HTTParty::Response,
|
||||
success?: false,
|
||||
body: { 'error' => { 'message' => 'Access token has expired', 'code' => 190 } }.to_json,
|
||||
parsed_response: { 'error' => { 'message' => 'Access token has expired', 'code' => 190 } }
|
||||
)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('190 - Access token has expired')
|
||||
expect(instagram_channel.reload).to be_reauthorization_required
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user