feat: Support document file uploads on telegram channel (#9266)

This commit is contained in:
Sojan Jose
2024-05-02 14:05:14 -07:00
committed by GitHub
parent 3488a315d0
commit ac93af6028
4 changed files with 277 additions and 108 deletions

View File

@@ -137,68 +137,20 @@ RSpec.describe Channel::Telegram do
end
end
context 'when a empty message and valid attachments' do
context 'when message contains attachments' do
let(:message) do
create(:message, message_type: :outgoing, content: nil,
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
end
it 'send image' do
telegram_attachment_response = double
it 'calls send attachment service' do
telegram_attachment_service = double
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
allow(telegram_attachment_response).to receive(:success?).and_return(true)
allow(telegram_attachment_response).to receive(:parsed_response).and_return({ 'result' => [{ 'message_id' => 'telegram_456' }] })
allow(telegram_channel).to receive(:attachments_request).and_return(telegram_attachment_response)
allow(Telegram::SendAttachmentsService).to receive(:new).with(message: message).and_return(telegram_attachment_service)
allow(telegram_attachment_service).to receive(:perform).and_return('telegram_456')
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_456')
end
it 'send document' do
telegram_attachment_response = double
attachment = message.attachments.new(account_id: message.account_id, file_type: :file)
attachment.file.attach(io: Rails.root.join('spec/assets/attachment.pdf').open, filename: 'attachment.pdf',
content_type: 'application/pdf')
allow(telegram_attachment_response).to receive(:success?).and_return(true)
allow(telegram_attachment_response).to receive(:parsed_response).and_return({ 'result' => [{ 'message_id' => 'telegram_456' }] })
allow(telegram_channel).to receive(:attachments_request).and_return(telegram_attachment_response)
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_456')
end
it 'send image failed' do
telegram_attachment_response = double
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
allow(telegram_attachment_response).to receive(:success?).and_return(false)
allow(telegram_attachment_response).to receive(:parsed_response).and_return({ 'ok' => false, 'error_code' => '400',
'description' => 'Bad Request: invalid file id' })
allow(telegram_channel).to receive(:attachments_request).and_return(telegram_attachment_response)
telegram_channel.send_message_on_telegram(message)
expect(message.reload.status).to eq('failed')
expect(message.reload.external_error).to eq('400, Bad Request: invalid file id')
end
end
context 'when a valid message and valid attachment' do
it 'send both message and attachment' do
message = create(:message, message_type: :outgoing, content: 'test',
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
telegram_message_response = double
telegram_attachment_response = double
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
allow(telegram_message_response).to receive(:success?).and_return(true)
allow(telegram_message_response).to receive(:parsed_response).and_return({ 'result' => { 'message_id' => 'telegram_456' } })
allow(telegram_attachment_response).to receive(:success?).and_return(true)
allow(telegram_attachment_response).to receive(:parsed_response).and_return({ 'result' => [{ 'message_id' => 'telegram_789' }] })
allow(telegram_channel).to receive(:message_request).and_return(telegram_message_response)
allow(telegram_channel).to receive(:attachments_request).and_return(telegram_attachment_response)
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_789')
end
end
end