use html as telegram bot parse mode

This commit is contained in:
Dylan Knutson
2025-08-14 21:31:53 +00:00
parent 3c83ed3ba7
commit 2acf31c70a

View File

@@ -125,7 +125,7 @@ module Tasks
chat_id: chat_id, chat_id: chat_id,
message_id: response_message.message_id, message_id: response_message.message_id,
text: result_text, text: result_text,
parse_mode: "Markdown", parse_mode: "HTML",
) )
# Record total request time # Record total request time
@@ -386,9 +386,10 @@ module Tasks
external_url = post.external_url_for_view external_url = post.external_url_for_view
if (title = post.title) && external_url if (title = post.title) && external_url
response += " - [#{post.to_param} - #{title}](#{external_url})" response +=
" - <a href=\"#{external_url}\">#{post.to_param} - #{html_escape(title)}</a>"
elsif external_url elsif external_url
response += " - [#{post.to_param}](#{external_url})" response += " - <a href=\"#{external_url}\">#{post.to_param}</a>"
else else
response += " - #{post.to_param}" response += " - #{post.to_param}"
end end
@@ -396,9 +397,10 @@ module Tasks
if post.respond_to?(:creator) && (creator = post.send(:creator)) if post.respond_to?(:creator) && (creator = post.send(:creator))
url = creator.external_url_for_view url = creator.external_url_for_view
if url if url
response += " by [#{creator.name_for_view}](#{url})" response +=
" by <a href=\"#{url}\">#{html_escape(creator.name_for_view)}</a>"
else else
response += " by #{creator.name_for_view}" response += " by #{html_escape(creator.name_for_view)}"
end end
end end
response += "\n" response += "\n"
@@ -407,6 +409,17 @@ module Tasks
response response
end end
sig { params(text: String).returns(String) }
def html_escape(text)
# Only escape characters that are explicitly required by Telegram Bot API
# All <, > and & symbols that are not part of a tag or HTML entity must be replaced
# API supports only these named HTML entities: &lt;, &gt;, &amp; and &quot;
text
.gsub("&", "&amp;") # Ampersand (must be first to avoid double-escaping)
.gsub("<", "&lt;") # Less than
.gsub(">", "&gt;") # Greater than
end
# Extract image file information from Telegram message # Extract image file information from Telegram message
sig do sig do
params(message: Telegram::Bot::Types::Message).returns( params(message: Telegram::Bot::Types::Message).returns(