From dc728faafbfc40224fbaf7fc41b6f3b07bf6e316 Mon Sep 17 00:00:00 2001 From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Date: Thu, 6 Feb 2025 14:23:08 +0530 Subject: [PATCH] feat: Adds support for telegram contact sharing (#10841) # Pull Request Template ## Description This PR adds support for displaying shared contacts in a Telegram channel. **NB:** Tested with both old and new bubbles. Multiple numbers for a single contact are not supported at this time, but multiple contacts are supported. In the future, we can add support for displaying contact names as well. ## Type of change - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? **Loom video** https://www.loom.com/share/95efadace3194887bc0663c53e7c08bc?sid=a5c27176-3dd8-456c-80b9-c63dbb89dca1 ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --- .../dashboard/i18n/locale/en/chatlist.json | 3 +++ .../telegram/incoming_message_service.rb | 15 +++++++++++++++ .../telegram/incoming_message_service_spec.rb | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/app/javascript/dashboard/i18n/locale/en/chatlist.json b/app/javascript/dashboard/i18n/locale/en/chatlist.json index dd5e1416c..ec416f6cb 100644 --- a/app/javascript/dashboard/i18n/locale/en/chatlist.json +++ b/app/javascript/dashboard/i18n/locale/en/chatlist.json @@ -99,6 +99,9 @@ }, "fallback": { "CONTENT": "has shared a url" + }, + "contact": { + "CONTENT": "Shared contact" } }, "CHAT_SORT_BY_FILTER": { diff --git a/app/services/telegram/incoming_message_service.rb b/app/services/telegram/incoming_message_service.rb index a36231634..d39005030 100644 --- a/app/services/telegram/incoming_message_service.rb +++ b/app/services/telegram/incoming_message_service.rb @@ -43,6 +43,7 @@ class Telegram::IncomingMessageService def process_message_attachments attach_location attach_files + attach_contact end def update_contact_avatar @@ -136,6 +137,16 @@ class Telegram::IncomingMessageService ) end + def attach_contact + return unless contact_card + + @message.attachments.new( + account_id: @message.account_id, + file_type: :contact, + fallback_title: contact_card['phone_number'].to_s + ) + end + def file @file ||= visual_media_params || params[:message][:voice].presence || params[:message][:audio].presence || params[:message][:document].presence end @@ -154,6 +165,10 @@ class Telegram::IncomingMessageService @location ||= params.dig(:message, :location).presence end + def contact_card + @contact_card ||= params.dig(:message, :contact).presence + end + def visual_media_params params[:message][:photo].presence&.last || params.dig(:message, :sticker, :thumb).presence || params[:message][:video].presence end diff --git a/spec/services/telegram/incoming_message_service_spec.rb b/spec/services/telegram/incoming_message_service_spec.rb index 3cade6343..0702baa32 100644 --- a/spec/services/telegram/incoming_message_service_spec.rb +++ b/spec/services/telegram/incoming_message_service_spec.rb @@ -309,5 +309,22 @@ describe Telegram::IncomingMessageService do expect(telegram_channel.inbox.messages.first.content).to eq('Option 1') end end + + context 'when valid contact message params' do + it 'creates appropriate conversations, message and contacts' do + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'contact': { + 'phone_number': '+918660944581' + } + }.merge(message_params) + }.with_indifferent_access + described_class.new(inbox: telegram_channel.inbox, params: params).perform + expect(telegram_channel.inbox.conversations.count).not_to eq(0) + expect(Contact.all.first.name).to eq('Sojan Jose') + expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('contact') + end + end end end