mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
- this ensures that the markdown formatted messages from the Chatwoot dashboard will render consistently in telegram UI for the supported types like bold, italics, links etc
205 lines
9.7 KiB
Ruby
205 lines
9.7 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Channel::Telegram do
|
|
let(:telegram_channel) { create(:channel_telegram) }
|
|
|
|
describe '#convert_markdown_to_telegram_html' do
|
|
subject { telegram_channel.send(:convert_markdown_to_telegram_html, text) }
|
|
|
|
context 'when text contains multiple newline characters' do
|
|
let(:text) { "Line one\nLine two\n\nLine four" }
|
|
|
|
it 'preserves multiple newline characters' do
|
|
expect(subject).to eq("Line one\nLine two\n\nLine four")
|
|
end
|
|
end
|
|
|
|
context 'when text contains broken markdown' do
|
|
let(:text) { 'This is a **broken markdown with <b>HTML</b> tags.' }
|
|
|
|
it 'does not break and properly converts to Telegram HTML format and escapes html tags' do
|
|
expect(subject).to eq('This is a **broken markdown with <b>HTML</b> tags.')
|
|
end
|
|
end
|
|
|
|
context 'when text contains markdown and HTML elements' do
|
|
let(:text) { "Hello *world*! This is <b>bold</b> and this is <i>italic</i>.\nThis is a new line." }
|
|
|
|
it 'converts markdown to Telegram HTML format and escapes other html' do
|
|
expect(subject).to eq("Hello <em>world</em>! This is <b>bold</b> and this is <i>italic</i>.\nThis is a new line.")
|
|
end
|
|
end
|
|
|
|
context 'when text contains unsupported HTML tags' do
|
|
let(:text) { 'This is a <span>test</span> with unsupported tags.' }
|
|
|
|
it 'removes unsupported HTML tags' do
|
|
expect(subject).to eq('This is a <span>test</span> with unsupported tags.')
|
|
end
|
|
end
|
|
|
|
context 'when text contains special characters' do
|
|
let(:text) { 'Special characters: & < >' }
|
|
|
|
it 'escapes special characters' do
|
|
expect(subject).to eq('Special characters: & < >')
|
|
end
|
|
end
|
|
|
|
context 'when text contains markdown links' do
|
|
let(:text) { 'Check this [link](http://example.com) out!' }
|
|
|
|
it 'converts markdown links to Telegram HTML format' do
|
|
expect(subject).to eq('Check this <a href="http://example.com">link</a> out!')
|
|
end
|
|
end
|
|
end
|
|
|
|
context 'when a valid message and empty attachments' do
|
|
it 'send message' do
|
|
message = create(:message, message_type: :outgoing, content: 'test',
|
|
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
|
|
|
|
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
|
|
.with(
|
|
body: 'chat_id=123&text=test&reply_markup=&parse_mode=HTML&reply_to_message_id='
|
|
)
|
|
.to_return(
|
|
status: 200,
|
|
body: { result: { message_id: 'telegram_123' } }.to_json,
|
|
headers: { 'Content-Type' => 'application/json' }
|
|
)
|
|
|
|
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_123')
|
|
end
|
|
|
|
it 'send message with markdown converted to telegram HTML' do
|
|
message = create(:message, message_type: :outgoing, content: '**test** *test* ~test~',
|
|
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
|
|
|
|
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
|
|
.with(
|
|
body: "chat_id=123&text=#{
|
|
ERB::Util.url_encode('<strong>test</strong> <em>test</em> ~test~')
|
|
}&reply_markup=&parse_mode=HTML&reply_to_message_id="
|
|
)
|
|
.to_return(
|
|
status: 200,
|
|
body: { result: { message_id: 'telegram_123' } }.to_json,
|
|
headers: { 'Content-Type' => 'application/json' }
|
|
)
|
|
|
|
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_123')
|
|
end
|
|
|
|
it 'send message with reply_markup' do
|
|
message = create(
|
|
:message, message_type: :outgoing, content: 'test', content_type: 'input_select',
|
|
content_attributes: { 'items' => [{ 'title' => 'test', 'value' => 'test' }] },
|
|
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' })
|
|
)
|
|
|
|
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
|
|
.with(
|
|
body: 'chat_id=123&text=test' \
|
|
'&reply_markup=%7B%22one_time_keyboard%22%3Atrue%2C%22inline_keyboard%22%3A%5B%5B%7B%22text%22%3A%22test%22%2C%22' \
|
|
'callback_data%22%3A%22test%22%7D%5D%5D%7D&parse_mode=HTML&reply_to_message_id='
|
|
)
|
|
.to_return(
|
|
status: 200,
|
|
body: { result: { message_id: 'telegram_123' } }.to_json,
|
|
headers: { 'Content-Type' => 'application/json' }
|
|
)
|
|
|
|
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_123')
|
|
end
|
|
|
|
it 'send text message failed' do
|
|
message = create(:message, message_type: :outgoing, content: 'test',
|
|
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
|
|
|
|
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
|
|
.with(
|
|
body: 'chat_id=123&text=test&reply_markup=&parse_mode=HTML&reply_to_message_id='
|
|
)
|
|
.to_return(
|
|
status: 403,
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
body: {
|
|
ok: false,
|
|
error_code: '403',
|
|
description: 'Forbidden: bot was blocked by the user'
|
|
}.to_json
|
|
)
|
|
telegram_channel.send_message_on_telegram(message)
|
|
expect(message.reload.status).to eq('failed')
|
|
expect(message.reload.external_error).to eq('403, Forbidden: bot was blocked by the user')
|
|
end
|
|
end
|
|
|
|
context 'when a empty message and valid 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
|
|
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)
|
|
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
|