telegram_entities 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59bea042c497fc78acc233e6226d4e74fedd0350b98f5c04d532352531106f9f
4
- data.tar.gz: f01c66585462f4f3e49f32739907a8777b9e2fbeefa056b95ce5c5f7ab20e086
3
+ metadata.gz: 0b280cc27c741aacc2169e8bc5138b1f2a7886d46b17637962a0e02f6bbc6c07
4
+ data.tar.gz: 3542dc2cef1dbbe23be909c1f865d7c1801f2fecb8e154bcef9256342fb51863
5
5
  SHA512:
6
- metadata.gz: 7fa8daab2f8252ce3792be80892beea8e6eab0c2af9ffcf0d5f8978fc0eb7c0d2af64344f2a943fcd56575ef1cf75ba706ca40e657117c3388a974fadc4d2389
7
- data.tar.gz: 26a866427a665ae4198a0c863dbe6a96dd5db423d84a186bdfbe3708e5295cfa798b29d5ecb177719a0a7100a6bb0fa2f781adf0e1936e2e7de6439e5dd3dc6e
6
+ metadata.gz: 06d4410adf179fded1205e9c36acd1aadab8668ab3a195c1916b5fb83c3b8bed30dd1bda96a45f393c6b6c70522fcf5b6c7b00459dcaa73ca6276c4e943c7ca1
7
+ data.tar.gz: 72601b0445e91bd68dc66c51bc0fbb6c76bdae163955ced4db96dde08de3a16d14f468f7a4f662df35c0f2f776587615788510b1f2cd0d78d7e431f064139944
data/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2025-12-04
4
+
5
+ ### Added
6
+ - New method `to_bot_html` for formatting HTML specifically for Telegram Bot API `parse_mode: 'HTML'`
7
+ - Automatic replacement of `<br>` and `<br/>` tags with `\n` characters
8
+ - Automatic removal of wrappers for hashtags, cashtags, bot commands, media timestamps, and bank card numbers (these are automatically processed by Telegram)
9
+ - Proper HTML entity escaping for `<`, `>`, `&`, and `"` symbols
10
+ - Support for named HTML entities (`&lt;`, `&gt;`, `&amp;`, `&quot;`)
11
+ - Support for numeric HTML entities (decimal and hexadecimal)
12
+
13
+ ### Changed
14
+ - Improved documentation with examples for `to_bot_html` method
15
+ - Added reference to Telegram Bot API HTML Style documentation
16
+
3
17
  ## [0.1.0] - 2025-11-22
4
18
 
19
+ ### Added
5
20
  - Initial release
21
+ - Support for all Telegram MessageEntity types
22
+ - Conversion from Markdown to Telegram entities (`from_markdown`)
23
+ - Conversion from HTML to Telegram entities (`from_html`)
24
+ - Conversion from Telegram entities to HTML (`to_html`)
25
+ - Support for Telegram-specific HTML tags (`allow_telegram_tags` option)
26
+ - UTF-16 offset/length handling for all entity types
27
+ - Support for TDLib formattedText conversion (`from_tdlib_formatted_text`)
data/README.md CHANGED
@@ -126,6 +126,33 @@ puts html
126
126
  # => "<strong>Hello</strong> <em>world</em>"
127
127
  ```
128
128
 
129
+ ### Converting Entities to Telegram Bot API HTML Format
130
+
131
+ For sending messages via Telegram Bot API with `parse_mode: 'HTML'`, use `to_bot_html`:
132
+
133
+ > 📖 See [Telegram Bot API HTML Style documentation](https://core.telegram.org/bots/api#html-style) for complete formatting rules.
134
+
135
+ ```ruby
136
+ entities = TelegramEntities.new('Check #hashtag and /start command', [
137
+ {'type' => 'hashtag', 'offset' => 6, 'length' => 8},
138
+ {'type' => 'bot_command', 'offset' => 19, 'length' => 7}
139
+ ])
140
+
141
+ html = entities.to_bot_html
142
+ puts html
143
+ # => "Check #hashtag and /start command"
144
+ # Note: hashtags and bot commands are sent without wrappers (automatically processed by Telegram)
145
+
146
+ # Use in Telegram Bot API
147
+ # bot.send_message(chat_id: chat_id, text: html, parse_mode: 'HTML')
148
+ ```
149
+
150
+ The `to_bot_html` method:
151
+ - Replaces `<br>` tags with `\n`
152
+ - Removes wrappers for hashtags, cashtags, bot commands, media timestamps, and bank card numbers
153
+ - Keeps `tg-spoiler` and `tg-emoji` tags as-is
154
+ - Properly escapes HTML symbols (`<`, `>`, `&`, `"`)
155
+
129
156
  ### Telegram-Specific Tags
130
157
 
131
158
  When sending HTML to Telegram Bot API, use `allow_telegram_tags: true` to get Telegram-compatible tags. This is especially important for special entity types like spoilers, custom emojis, and cashtags:
@@ -163,6 +190,7 @@ puts html_telegram
163
190
  **When to use each mode:**
164
191
  - **Standard HTML** (`allow_telegram_tags: false`): For displaying in web browsers or general HTML rendering
165
192
  - **Telegram Tags** (`allow_telegram_tags: true`): For sending messages via Telegram Bot API using `parse_mode: 'HTML'`
193
+ - **`to_bot_html`**: Recommended method for Telegram Bot API - automatically formats HTML according to Telegram's requirements (removes auto-processed entity wrappers, replaces `<br>` with `\n`, etc.)
166
194
 
167
195
  ### Real-World Example
168
196
 
@@ -5,6 +5,7 @@ require "nokogiri"
5
5
  module TelegramEntities
6
6
  # Class that represents a message + set of Telegram entities.
7
7
  class Entities
8
+ include TgBotHtmlParseModeFormat
8
9
  attr_accessor :message, :entities
9
10
 
10
11
  # Creates an Entities container using a message and a list of entities.
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TelegramEntities
4
+ # doc: https://core.telegram.org/bots/api#html-style
5
+ module TgBotHtmlParseModeFormat
6
+ # Format HTML for Telegram Bot API parse_mode: 'HTML'
7
+ #
8
+ # According to Telegram Bot API documentation:
9
+ # - All <br>, <br/> tags are replaced with \n
10
+ # - Hashtags, cashtags, bot commands, media timestamps, and bank card numbers
11
+ # are sent without wrappers (they are automatically processed by the bot)
12
+ # - tg-spoiler and tg-emoji tags are left as-is
13
+ # - All <, >, &, " symbols that are not part of a tag or HTML entity
14
+ # are replaced with corresponding HTML entities (&lt;, &gt;, &amp;, &quot;)
15
+ # - All numerical HTML entities are supported (e.g., &#60;, &#x3C;)
16
+ # - Named HTML entities are supported: &lt;, &gt;, &amp;, &quot;
17
+ #
18
+ # @return [String] Formatted HTML text for Telegram Bot API
19
+ def to_bot_html
20
+ html = to_html(true)
21
+
22
+ # Replace all <br> and <br/> tags with \n
23
+ html = html.gsub(/<br\s*\/?>/i, "\n")
24
+
25
+ # Remove wrappers for hashtags, cashtags, bot commands, media timestamps, and bank card numbers
26
+ # These are automatically processed by Telegram, so we just extract the text content
27
+ html = html.gsub(/<tg-hashtag>(.*?)<\/tg-hashtag>/i) { |_| $1 }
28
+ html = html.gsub(/<tg-cashtag>(.*?)<\/tg-cashtag>/i) { |_| $1 }
29
+ html = html.gsub(/<tg-bot-command>(.*?)<\/tg-bot-command>/i) { |_| $1 }
30
+ html = html.gsub(/<tg-media-timestamp[^>]*>(.*?)<\/tg-media-timestamp>/i) { |_| $1 }
31
+ html = html.gsub(/<tg-bank-card-number>(.*?)<\/tg-bank-card-number>/i) { |_| $1 }
32
+
33
+ html
34
+ end
35
+ end
36
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TelegramEntities
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "telegram_entities/version"
4
4
  require_relative "telegram_entities/entity_tools"
5
+ require_relative "telegram_entities/tg_bot_html_parse_mode_format"
5
6
  require_relative "telegram_entities/entities"
6
7
  require_relative "telegram_entities/tdlib_converter"
7
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram_entities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Bobykin
@@ -41,6 +41,7 @@ files:
41
41
  - lib/telegram_entities/entities.rb
42
42
  - lib/telegram_entities/entity_tools.rb
43
43
  - lib/telegram_entities/tdlib_converter.rb
44
+ - lib/telegram_entities/tg_bot_html_parse_mode_format.rb
44
45
  - lib/telegram_entities/version.rb
45
46
  - sig/telegram_entities.rbs
46
47
  homepage: https://github.com/qelphybox/telegram_entities_rb