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