From 7b5f1e48760467a6fb1968d8e0cd7df408ce7338 Mon Sep 17 00:00:00 2001 From: "OMAR.A" <58332033+civilcoder55@users.noreply.github.com> Date: Wed, 4 Jan 2023 12:41:54 +0200 Subject: [PATCH] feat: Support for telegram incoming location message (#6158) Support for incoming location messages in the telegram channel. ref: #3398 --- .../telegram/incoming_message_service.rb | 16 ++++++++++++++++ .../telegram/incoming_message_service_spec.rb | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/services/telegram/incoming_message_service.rb b/app/services/telegram/incoming_message_service.rb index e8ab8a72c..2391921bd 100644 --- a/app/services/telegram/incoming_message_service.rb +++ b/app/services/telegram/incoming_message_service.rb @@ -20,6 +20,7 @@ class Telegram::IncomingMessageService sender: @contact, source_id: (params[:message][:message_id]).to_s ) + attach_location attach_files @message.save! end @@ -111,10 +112,25 @@ class Telegram::IncomingMessageService ) end + def attach_location + return unless location + + @message.attachments.new( + account_id: @message.account_id, + file_type: :location, + coordinates_lat: location['latitude'], + coordinates_long: location['longitude'] + ) + end + def file @file ||= visual_media_params || params[:message][:voice].presence || params[:message][:audio].presence || params[:message][:document].presence end + def location + @location ||= params[:message][:location].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 766330605..b651e6fc4 100644 --- a/spec/services/telegram/incoming_message_service_spec.rb +++ b/spec/services/telegram/incoming_message_service_spec.rb @@ -214,5 +214,23 @@ describe Telegram::IncomingMessageService do expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('file') end end + + context 'when valid location message params' do + it 'creates appropriate conversations, message and contacts' do + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'location': { + 'latitude': 37.7893768, + 'longitude': -122.3895553 + } + }.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('location') + end + end end end