feat: Adds internal support for markdown in Telegram outgoing messages (#8640)

This PR adds support for Markdown in the telegram API for send message. The dashboard uses commonmark syntax for markdown but telegram is using MarkdownV2.
More info - https://core.telegram.org/bots/api#markdownv2-style

Adds support for bold - *bold*, _italic text_, __underline__, ~strikethrough~

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Nithin David Thomas
2024-01-16 01:32:59 -08:00
committed by GitHub
parent 4aab63b7df
commit d1e7e75638
2 changed files with 38 additions and 4 deletions

View File

@@ -149,12 +149,29 @@ class Channel::Telegram < ApplicationRecord
})
end
def convert_markdown_to_telegram(text)
# Convert bold - double asterisks to single asterisk in Telegram
text.gsub!(/\*\*(.*?)\*\*/, '*\1*')
# Convert italics - single underscore (same in both CommonMark and Telegram)
# No conversion needed for italics as both use _text_
# Convert underline - not typically used in CommonMark, so we'll leave it as is
# Convert strikethrough - double tilde to single tilde in Telegram
text.gsub!(/~~(.*?)~~/, '~\1~')
text
end
def message_request(chat_id, text, reply_markup = nil, reply_to_message_id = nil)
text_to_md = convert_markdown_to_telegram(text)
HTTParty.post("#{telegram_api_url}/sendMessage",
body: {
chat_id: chat_id,
text: text,
text: text_to_md,
reply_markup: reply_markup,
parse_mode: 'MarkdownV2',
reply_to_message_id: reply_to_message_id
})
end

View File

@@ -10,7 +10,24 @@ RSpec.describe Channel::Telegram do
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
.with(
body: 'chat_id=123&text=test&reply_markup=&reply_to_message_id='
body: 'chat_id=123&text=test&reply_markup=&parse_mode=MarkdownV2&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 markdown' do
message = create(:message, message_type: :outgoing, content: '**test** ~~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('*test* ~test~ *test* ~test~')}&reply_markup=&parse_mode=MarkdownV2&reply_to_message_id="
)
.to_return(
status: 200,
@@ -32,7 +49,7 @@ RSpec.describe Channel::Telegram do
.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&reply_to_message_id='
'callback_data%22%3A%22test%22%7D%5D%5D%7D&parse_mode=MarkdownV2&reply_to_message_id='
)
.to_return(
status: 200,
@@ -49,7 +66,7 @@ RSpec.describe Channel::Telegram do
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
.with(
body: 'chat_id=123&text=test&reply_markup=&reply_to_message_id='
body: 'chat_id=123&text=test&reply_markup=&parse_mode=MarkdownV2&reply_to_message_id='
)
.to_return(
status: 403,