diff --git a/app/builders/messages/instagram/message_builder.rb b/app/builders/messages/instagram/message_builder.rb index 1be960d21..e917af5db 100644 --- a/app/builders/messages/instagram/message_builder.rb +++ b/app/builders/messages/instagram/message_builder.rb @@ -70,17 +70,28 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder @messaging[:message][:text] end + def story_reply_attributes + message[:reply_to][:story] if message[:reply_to].present? && message[:reply_to][:story].present? + end + def build_message return if @outgoing_echo && already_sent_from_chatwoot? return if message_content.blank? && all_unsupported_files? @message = conversation.messages.create!(message_params) + save_story_id attachments.each do |attachment| process_attachment(attachment) end end + def save_story_id + return if story_reply_attributes.blank? + + @message.save_story_info(story_reply_attributes) + end + def build_conversation @contact_inbox ||= contact.contact_inboxes.find_by!(source_id: message_source_id) Conversation.create!(conversation_params.merge( diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index 80a6b3449..985c1ce11 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -22,6 +22,14 @@ :bcc="emailHeadAttributes.bcc" :is-incoming="isIncoming" /> +
+ {{ $t('CONVERSATION.REPLIED_TO_STORY') }} + +
diff --git a/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue b/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue index eb51af58a..750c4a982 100644 --- a/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue +++ b/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue @@ -195,7 +195,7 @@ export default { return ''; } const { storySender, storyId } = this; - return `https://www.instagram.com/stories/${storySender}/${storyId}`; + return `https://www.instagram.com/stories/direct/${storySender}_${storyId}`; }, showStatusIndicators() { if ((this.isOutgoing || this.isTemplate) && !this.isPrivate) { diff --git a/app/javascript/dashboard/i18n/locale/en/conversation.json b/app/javascript/dashboard/i18n/locale/en/conversation.json index 78b7e660c..a20522108 100644 --- a/app/javascript/dashboard/i18n/locale/en/conversation.json +++ b/app/javascript/dashboard/i18n/locale/en/conversation.json @@ -37,6 +37,7 @@ "UNKNOWN_FILE_TYPE": "Unknown File", "SAVE_CONTACT": "Save", "UPLOADING_ATTACHMENTS": "Uploading attachments...", + "REPLIED_TO_STORY": "Replied to your story", "SUCCESS_DELETE_MESSAGE": "Message deleted successfully", "FAIL_DELETE_MESSSAGE": "Couldn't delete message! Try again", "NO_RESPONSE": "No response", diff --git a/app/models/message.rb b/app/models/message.rb index 00107c7cc..9000af0ec 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -198,6 +198,17 @@ class Message < ApplicationRecord outgoing? && human_response? && not_created_by_automation? && !private? end + def save_story_info(story_info) + self.content_attributes = content_attributes.merge( + { + story_id: story_info['id'], + story_sender: inbox.channel.instagram_id, + story_url: story_info['url'] + } + ) + save! + end + private def ensure_content_type diff --git a/spec/builders/messages/instagram/message_builder_spec.rb b/spec/builders/messages/instagram/message_builder_spec.rb index 2220c3295..e33c697eb 100644 --- a/spec/builders/messages/instagram/message_builder_spec.rb +++ b/spec/builders/messages/instagram/message_builder_spec.rb @@ -13,6 +13,7 @@ describe ::Messages::Instagram::MessageBuilder do 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!(:instagram_story_reply_event) { build(:instagram_story_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') } @@ -44,6 +45,29 @@ describe ::Messages::Instagram::MessageBuilder do expect(message.content).to eq('This is the first message from the customer') 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 + + described_class.new(messaging, instagram_inbox).perform + + message = instagram_channel.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 '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( diff --git a/spec/factories/instagram/instagram_message_create_event.rb b/spec/factories/instagram/instagram_message_create_event.rb index d024deccc..0220feab5 100644 --- a/spec/factories/instagram/instagram_message_create_event.rb +++ b/spec/factories/instagram/instagram_message_create_event.rb @@ -26,6 +26,39 @@ FactoryBot.define do initialize_with { attributes } end + factory :instagram_story_reply_event, class: Hash do + entry do + [ + { + 'id': 'instagram-message-id-123', + 'time': '2021-09-08T06:34:04+0000', + 'messaging': [ + { + 'sender': { + 'id': 'Sender-id-1' + }, + 'recipient': { + 'id': 'chatwoot-app-user-id-1' + }, + 'timestamp': '2021-09-08T06:34:04+0000', + 'message': { + 'mid': 'message-id-1', + 'text': 'This is the story reply', + 'reply_to': { + 'story': { + 'id': 'chatwoot-app-user-id-1', + 'url': 'https://chatwoot-assets.local/sample.png' + } + } + } + } + ] + } + ] + end + initialize_with { attributes } + end + factory :instagram_test_text_event, class: Hash do entry do [