telegrama 0.1.1 β†’ 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df42e5f329649ab529e46515818ec4ddce70388341455cd383a03b9852c84233
4
- data.tar.gz: 1c7411979c5d2376aaa0b9307cde1e51f380548b86fa76ba04025f8e8f39b434
3
+ metadata.gz: 973c12ff30ac29dd071ec63827d398756de990ab574bf9e18011a1509b7d4d4c
4
+ data.tar.gz: ab22a8f8eb8b3805be54f454b35b804495b5a3b3a6c56a024d8e236c463a7a99
5
5
  SHA512:
6
- metadata.gz: e936c11b1532985345a06ee0724110f653dcaa621f2250f31c1ae74d59940471107e6022f3ba0887cc389c958fd33eb4e4a0d7cb2cb26245fb7ce098f8527fb8
7
- data.tar.gz: 9bcbd009525da8d7cec77effb0cad2791da32beb2806bb321aaeedba73f94f1ffa3f34e89d1ee7e9d7ed29e583682be9c9d7297ed4ffdc974a48c3d1926d431f
6
+ metadata.gz: 05d3a61ba2149a9a90f4d1e8d7180770baafc857b26403770c27ee1894c09f685f36bd65c3e5f15ad404648f035c9812691b1619652bd8d3cb6db7fccae51a69
7
+ data.tar.gz: a8708abb6978b2ba6165d16a30c5c8dbc6bd2f18ab78f9eed3bf6e20141c3ef88e27587135a53e6b11257c111f5dcf5d22b2544bc88a45df86a563262ebcbd41
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.1.2] - 2025-02-19
2
+
3
+ - Added optional message prefix and suffix configuration
4
+
1
5
  ## [0.1.1] - 2025-02-18
2
6
 
3
7
  - Rebranded `telegrams` to `telegrama`
data/README.md CHANGED
@@ -1,16 +1,56 @@
1
1
  # πŸ’¬ `telegrama` – a tiny wrapper to send admin Telegram messages
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/telegrama.svg?v=0.1.0)](https://badge.fury.io/rb/telegrama?v=0.1.0)
3
+ [![Gem Version](https://badge.fury.io/rb/telegrama.svg?v=0.1.1)](https://badge.fury.io/rb/telegrama?v=0.1.1)
4
4
 
5
5
  Send quick, simple admin / logging Telegram messages via a Telegram bot.
6
6
 
7
- I'm making this gem because I'm tired of copy-pasting the same Telegram wrapper from Rails project to Rails project just to send myself admin messages and notifications. The goal with this gem is to provide a straightforward, minimal API to send Telegram messages reliably. All I want to do is this:
7
+ Useful for Rails developers using Telegram messages for notifications, admin alerts, errors, logs, daily summaries, and status updates, like:
8
8
 
9
9
  ```ruby
10
10
  Telegrama.send_message("Important admin notification!")
11
11
  ```
12
12
 
13
- This is useful for Rails developers using Telegram messages for notifications, admin alerts, errors, logs, daily summaries, and status updates.
13
+ I use it all the time to alert me of new sales, important notifications, and daily summaries, like this:
14
+
15
+ > πŸ’Έ **New sale!**
16
+ >
17
+ > joh...e@gmail.com paid **$49.99** for Business Plan.
18
+ >
19
+ > πŸ“ˆ MRR: $12,345
20
+ >
21
+ > πŸ“ˆ Total customers: 1,234
22
+ >
23
+ > [πŸ”— View purchase details](https://example.com/admin/subscriptions/123)
24
+
25
+ Which is a beautifully formatted message you'll in Telegram with only this:
26
+
27
+ ```ruby
28
+ message = <<~MSG
29
+ πŸ’Έ *New sale\!*
30
+
31
+ #{customer.email} paid *$#{amount}* for #{product.name}\.
32
+
33
+ πŸ“ˆ MRR: $#{Profitable.mrr}
34
+ πŸ“ˆ Total customers: $#{Profitable.total_customers}
35
+
36
+ [πŸ”— View purchase details](#{admin_subscription_url(subscription)})
37
+ MSG
38
+
39
+ Telegrama.send_message(message, formatting: { obfuscate_emails: true })
40
+ ```
41
+
42
+ Note how the email gets redacted automatically to avoid leaking personal information (`john.doe@gmail.com` -> `joh...e@gmail.com`)
43
+
44
+ The gem sanitizes weird characters, you can also escape Markdown, HTML, etc.
45
+
46
+ For the MRR and revenue metrics you can use my gem [`profitable`](https://github.com/rameerez/profitable); and if you have different group chats for marketing, management, etc. you can send different messages with different information to each of them:
47
+
48
+ ```ruby
49
+ Telegrama.send_message(a_general_message, chat_id: general_chat_id)
50
+ Telegrama.send_message(marketing_message, chat_id: marketing_chat_id)
51
+ ```
52
+
53
+ The goal with this gem is to provide a straightforward, no-frills, minimal API to send Telegram messages reliably for admin purposes, without you having to write your own wrapper over the Telegram API.
14
54
 
15
55
  ## Quick start
16
56
 
@@ -34,6 +74,10 @@ Telegrama.configure do |config|
34
74
  config.chat_id = Rails.application.credentials.dig(Rails.env.to_sym, :telegram, :chat_id)
35
75
  config.default_parse_mode = 'MarkdownV2'
36
76
 
77
+ # Optional prefix/suffix for all messages (useful to identify messages from different apps or environments)
78
+ config.message_prefix = nil # Will be prepended to all messages if set
79
+ config.message_suffix = nil # Will be appended to all messages if set
80
+
37
81
  # Default formatting options
38
82
  config.formatting_options = {
39
83
  escape_markdown: true, # Escape markdown special characters
@@ -65,6 +109,43 @@ Sometimes you want to report user actions including a sufficiently identifiable
65
109
 
66
110
  You can pass an options hash to `Telegrama.send_message` to override default behavior on a per‑message basis:
67
111
 
112
+ ### Message Prefix and Suffix
113
+
114
+ You may have multiple applications sending messages to the same Telegram group chat, and it can be hard to identify which message came from which application. Using message prefixes and suffixes, you can easily label messages from different sources:
115
+
116
+ ```ruby
117
+ # Label which environment this message is coming from
118
+ config.message_prefix = "[#{Rails.env}] \n"
119
+
120
+ # Or for different applications:
121
+ config.message_prefix = "[πŸ›οΈ Shop App] \n"
122
+ config.message_suffix = "\n--\nSent from Shop App"
123
+
124
+ config.message_prefix = "[πŸ“Š Analytics] \n"
125
+ config.message_suffix = "\n--\nSent from Analytics"
126
+ ```
127
+
128
+ This way, when multiple applications send messages to the same chat, you'll see:
129
+ ```
130
+ [πŸ›οΈ Shop App]
131
+ New order received: $99.99
132
+ --
133
+ Sent from Shop App
134
+
135
+ [πŸ“Š Analytics]
136
+ Daily Report: 150 new users today
137
+ --
138
+ Sent from Analytics
139
+ ```
140
+
141
+ Both `message_prefix` and `message_suffix` are optional and can be used independently. They're particularly useful for:
142
+ - Distinguishing between staging and production environments
143
+ - Identifying messages from different microservices
144
+ - Adding environment-specific tags or warnings
145
+ - Including standardized footers or timestamps
146
+
147
+ ### `send_message` options
148
+
68
149
  - **`chat_id`**
69
150
  *Override the default chat ID set in your configuration.*
70
151
  **Usage Example:**
@@ -16,6 +16,12 @@ module Telegrama
16
16
  # Whether to disable web page previews by default.
17
17
  attr_accessor :disable_web_page_preview
18
18
 
19
+ # Optional prefix to prepend to all messages (e.g. "[MyApp] \n")
20
+ attr_accessor :message_prefix
21
+
22
+ # Optional suffix to append to all messages (e.g. "\n-- Sent from MyApp")
23
+ attr_accessor :message_suffix
24
+
19
25
  # =========================================
20
26
  # Formatting Options
21
27
  # =========================================
@@ -45,6 +51,10 @@ module Telegrama
45
51
  @default_parse_mode = 'MarkdownV2'
46
52
  @disable_web_page_preview = true
47
53
 
54
+ # Message prefix/suffix defaults to nil (no prefix/suffix)
55
+ @message_prefix = nil
56
+ @message_suffix = nil
57
+
48
58
  # Sensible defaults for formatting options.
49
59
  @formatting_options = {
50
60
  escape_markdown: true,
@@ -2,7 +2,7 @@ module Telegrama
2
2
  module Formatter
3
3
  MARKDOWN_SPECIAL_CHARS = %w[_ * [ ] ( ) ~ ` > # + - = | { } . !].freeze
4
4
  # Characters that should always be escaped in Telegram messages, even when Markdown is enabled
5
- ALWAYS_ESCAPE_CHARS = %w[. !].freeze
5
+ ALWAYS_ESCAPE_CHARS = %w[. ! -].freeze
6
6
  # Characters used for Markdown formatting that need special handling
7
7
  MARKDOWN_FORMAT_CHARS = %w[* _].freeze
8
8
 
@@ -12,6 +12,14 @@ module Telegrama
12
12
  opts = defaults.merge(options)
13
13
 
14
14
  text = text.to_s
15
+
16
+ # Apply prefix and suffix if configured
17
+ prefix = Telegrama.configuration.message_prefix
18
+ suffix = Telegrama.configuration.message_suffix
19
+
20
+ text = "#{prefix}#{text}" if prefix
21
+ text = "#{text}#{suffix}" if suffix
22
+
15
23
  text = obfuscate_emails(text) if opts[:obfuscate_emails]
16
24
  text = escape_html(text) if opts[:escape_html]
17
25
  if opts[:escape_markdown]
@@ -21,6 +29,7 @@ module Telegrama
21
29
  text = escape_special_chars(text)
22
30
  end
23
31
  text = truncate(text, opts[:truncate]) if opts[:truncate]
32
+
24
33
  text
25
34
  end
26
35
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Telegrama
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegrama
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javi R
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-18 00:00:00.000000000 Z
10
+ date: 2025-02-19 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Send quick, simple admin / logging Telegram messages via a Telegram bot.
13
13
  Useful for Rails developers using Telegram messages for notifications, admin alerts,