From 7c21cf225516e9c19f26970e503990f5fe31095e Mon Sep 17 00:00:00 2001 From: Akhil G Krishnan Date: Tue, 12 Oct 2021 12:58:30 +0530 Subject: [PATCH] feat: Render audio, video and voice attachments in telegram. (#3155) Co-authored-by: Pranav Raj S --- .../telegram/incoming_message_service.rb | 16 ++- .../telegram/incoming_message_service_spec.rb | 133 ++++++++++++++++++ 2 files changed, 144 insertions(+), 5 deletions(-) diff --git a/app/services/telegram/incoming_message_service.rb b/app/services/telegram/incoming_message_service.rb index 33fd6f1f9..4bcc5b162 100644 --- a/app/services/telegram/incoming_message_service.rb +++ b/app/services/telegram/incoming_message_service.rb @@ -86,13 +86,14 @@ class Telegram::IncomingMessageService end def file_content_type - params[:message][:photo].present? ? :image : file_type(params[:message][:document][:mime_type]) + return :image if params[:message][:photo].present? + return :audio if params[:message][:voice].present? || params[:message][:audio].present? + return :video if params[:message][:video].present? + + file_type(params[:message][:document][:mime_type]) end def attach_files - file = params[:message][:document] - file ||= params[:message][:photo]&.last - return unless file attachment_file = Down.download( @@ -104,9 +105,14 @@ class Telegram::IncomingMessageService file_type: file_content_type, file: { io: attachment_file, - filename: attachment_file.original_filename, + filename: attachment_file, content_type: attachment_file.content_type } ) end + + def file + @file ||= params[:message][:photo].presence&.last || params[:message][:voice].presence || params[:message][:audio].presence || + params[:message][:video].presence || params[:message][:document].presence + end end diff --git a/spec/services/telegram/incoming_message_service_spec.rb b/spec/services/telegram/incoming_message_service_spec.rb index 9a1433794..6335249d9 100644 --- a/spec/services/telegram/incoming_message_service_spec.rb +++ b/spec/services/telegram/incoming_message_service_spec.rb @@ -41,5 +41,138 @@ describe Telegram::IncomingMessageService do expect(telegram_channel.inbox.conversations.count).to eq(0) end end + + context 'when valid audio messages params' do + it 'creates appropriate conversations, message and contacts' do + allow(telegram_channel.inbox.channel).to receive(:get_telegram_file_path).and_return('https://chatwoot-public-assets.s3.amazonaws.com/test-files/rspec/sample.mp3') + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'message_id' => 1, + 'from' => { + 'id' => 23, 'is_bot' => false, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'language_code' => 'en' + }, + 'chat' => { 'id' => 23, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'type' => 'private' }, + 'date' => 1_631_132_077, + 'audio' => { + 'file_id' => 'AwADBAADbXXXXXXXXXXXGBdhD2l6_XX', + 'duration' => 243, + 'mime_type' => 'audio/mpeg', + 'file_size' => 3_897_500, + 'title' => 'Test music file' + } + } + }.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('audio') + end + end + + context 'when valid image attachment params' do + it 'creates appropriate conversations, message and contacts' do + allow(telegram_channel.inbox.channel).to receive(:get_telegram_file_path).and_return('https://chatwoot-public-assets.s3.amazonaws.com/test-files/rspec/sample.png') + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'message_id' => 1, + 'from' => { + 'id' => 23, 'is_bot' => false, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'language_code' => 'en' + }, + 'chat' => { 'id' => 23, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'type' => 'private' }, + 'date' => 1_631_132_077, + 'photo' => [{ + 'file_id' => 'AgACAgUAAxkBAAODYV3aGZlD6vhzKsE2WNmblsr6zKwAAi-tMRvCoeBWNQ1ENVBzJdwBAAMCAANzAAMhBA', + 'file_unique_id' => 'AQADL60xG8Kh4FZ4', 'file_size' => 1883, 'width' => 90, 'height' => 67 + }] + } + }.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('image') + end + end + + context 'when valid video messages params' do + it 'creates appropriate conversations, message and contacts' do + allow(telegram_channel.inbox.channel).to receive(:get_telegram_file_path).and_return('https://chatwoot-public-assets.s3.amazonaws.com/test-files/rspec/sample.mov') + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'message_id' => 1, + 'from' => { + 'id' => 23, 'is_bot' => false, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'language_code' => 'en' + }, + 'chat' => { 'id' => 23, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'type' => 'private' }, + 'date' => 1_631_132_077, + 'video' => { + 'duration' => 1, 'width' => 720, 'height' => 1280, 'file_name' => 'IMG_2170.MOV', 'mime_type' => 'video/mp4', 'thumb' => { + 'file_id' => 'AAMCBQADGQEAA4ZhXd78Xz6_c6gCzbdIkgGiXJcwwwACqwMAAp3x8Fbhf3EWamgCWAEAB20AAyEE', 'file_unique_id' => 'AQADqwMAAp3x8FZy', + 'file_size' => 11_462, 'width' => 180, 'height' => 320 + }, 'file_id' => 'BAACAgUAAxkBAAOGYV3e_F8-v3OoAs23SJIBolyXMMMAAqsDAAKd8fBW4X9xFmpoAlghBA', 'file_unique_id' => 'AgADqwMAAp3x8FY', + 'file_size' => 291_286 + } + } + }.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('video') + end + end + + context 'when valid voice attachment params' do + it 'creates appropriate conversations, message and contacts' do + allow(telegram_channel.inbox.channel).to receive(:get_telegram_file_path).and_return('https://chatwoot-public-assets.s3.amazonaws.com/test-files/rspec/sample.oga') + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'message_id' => 1, + 'from' => { + 'id' => 23, 'is_bot' => false, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'language_code' => 'en' + }, + 'chat' => { 'id' => 23, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'type' => 'private' }, + 'date' => 1_631_132_077, + 'voice' => { + 'duration' => 2, 'mime_type' => 'audio/ogg', 'file_id' => 'AwACAgUAAxkBAANjYVwnWF_w8LYTchqVdK9dY7mbwYEAAskDAALCoeBWFvS2u4zS6HAhBA', + 'file_unique_id' => 'AgADyQMAAsKh4FY', 'file_size' => 11_833 + } + } + }.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('audio') + end + end + + context 'when valid document message params' do + it 'creates appropriate conversations, message and contacts' do + allow(telegram_channel.inbox.channel).to receive(:get_telegram_file_path).and_return('https://chatwoot-public-assets.s3.amazonaws.com/test-files/rspec/sample.pdf') + params = { + 'update_id' => 2_342_342_343_242, + 'message' => { + 'message_id' => 1, + 'from' => { + 'id' => 23, 'is_bot' => false, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'language_code' => 'en' + }, + 'chat' => { 'id' => 23, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'type' => 'private' }, + 'date' => 1_631_132_077, + 'document' => { + 'file_id' => 'AwADBAADbXXXXXXXXXXXGBdhD2l6_XX', + 'file_name' => 'Screenshot 2021-09-27 at 2.01.14 PM.png', + 'mime_type' => 'application/png', + 'file_size' => 536_392 + } + } + }.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('file') + end + end end end