@patricktobias86/node-red-telegram-account 1.1.13 → 1.1.16

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [1.1.16] - 2025-09-22
6
+ ### Added
7
+ - Receiver node option to ignore configurable message types (such as videos or documents) to prevent oversized uploads.
8
+ ### Changed
9
+ - Receiver node collects detailed media type metadata to power the new filter while keeping debug logging informative.
10
+
11
+ ## [1.1.15] - 2025-09-21
12
+ ### Added
13
+ - Receiver node option to drop updates when media exceeds a configurable size threshold, preventing large downloads.
14
+
5
15
  ## [1.1.7] - 2025-07-22
6
16
  ### Added
7
17
  - Mocha tests for the configuration node ensure sessions are reused correctly.
@@ -26,4 +36,3 @@ All notable changes to this project will be documented in this file.
26
36
  ## [1.1.12] - 2025-08-06
27
37
  ### Fixed
28
38
  - Auth node now emits the generated `stringSession` so it can be used by other nodes.
29
-
package/README.md CHANGED
@@ -18,7 +18,7 @@ See [docs/NODES.md](docs/NODES.md) for a detailed description of every node. Bel
18
18
 
19
19
  - **config** – stores your API credentials and caches sessions for reuse.
20
20
  - **auth** – interactive login that outputs a `stringSession` (also set on `msg.stringSession`).
21
- - **receiver** – emits messages for every incoming update (with optional ignore list). Event listeners are cleaned up on node close so redeploys won't duplicate messages.
21
+ - **receiver** – emits messages for every incoming update (with optional ignore list, message type filter, and media size limit). Event listeners are cleaned up on node close so redeploys won't duplicate messages.
22
22
  - **command** – triggers when an incoming message matches a command or regex. Event listeners are removed on redeploy to prevent duplicates.
23
23
  - **send-message** – sends text or media messages with rich options.
24
24
  - **send-files** – uploads one or more files with captions and buttons.
package/docs/NODES.md CHANGED
@@ -8,7 +8,7 @@ Below is a short description of each node. For a full list of configuration opti
8
8
  |------|-------------|
9
9
  | **config** | Configuration node storing API credentials and connection options. Other nodes reference this to share a Telegram client and reuse the session. Connections are tracked in a Map with a reference count so multiple nodes can wait for the same connection. |
10
10
  | **auth** | Starts an interactive login flow. Produces a `stringSession` (available in both <code>msg.payload.stringSession</code> and <code>msg.stringSession</code>) that can be reused with the `config` node. |
11
- | **receiver** | Emits an output message for every incoming Telegram message. Can ignore specific user IDs. Event handlers are automatically removed when the node is closed. |
11
+ | **receiver** | Emits an output message for every incoming Telegram message. Can ignore specific user IDs, skip selected message types (e.g. videos or documents), and optionally drop media above a configurable size. Event handlers are automatically removed when the node is closed. |
12
12
  | **command** | Listens for new messages and triggers when a message matches a configured command or regular expression. The event listener is cleaned up on node close to avoid duplicates. |
13
13
  | **send-message** | Sends text messages or media files to a chat. Supports parse mode, buttons, scheduling, and more. |
14
14
  | **send-files** | Uploads one or more files to a chat with optional caption, thumbnails and other parameters. |
@@ -22,4 +22,3 @@ Below is a short description of each node. For a full list of configuration opti
22
22
  All nodes forward any properties on the incoming `msg` outside of `msg.payload` unchanged.
23
23
 
24
24
  All nodes provide a **Debug** checkbox. When enabled the node will log its input and output messages to aid troubleshooting.
25
-
@@ -7,7 +7,9 @@
7
7
  name: { value: '' },
8
8
  config: { type: 'config', required: false },
9
9
  ignore: { value:""},
10
- debug: { value: false }
10
+ ignoreMessageTypes: { value: "" },
11
+ debug: { value: false },
12
+ maxFileSizeMb: { value: "" }
11
13
  },
12
14
  inputs: 1,
13
15
  outputs: 1,
@@ -49,15 +51,39 @@
49
51
  </div>
50
52
  <div class="form-row">
51
53
  <label for="node-input-ignore">
52
- <i class="fa fa-tag"></i> Config
54
+ <i class="fa fa-user-times"></i> Ignore list
53
55
  </label>
54
56
  <textarea
55
57
  type="text"
56
58
  id="node-input-ignore"
57
- placeholder="Config"
59
+ placeholder="Enter user IDs, one per line"
58
60
  style="width: 60%"
59
61
  ></textarea>
60
62
  </div>
63
+ <div class="form-row">
64
+ <label for="node-input-ignoreMessageTypes">
65
+ <i class="fa fa-ban"></i> Ignore message types
66
+ </label>
67
+ <textarea
68
+ id="node-input-ignoreMessageTypes"
69
+ placeholder="e.g. video, document, sticker"
70
+ style="width: 60%"
71
+ ></textarea>
72
+ <p class="form-tips">Separate types with commas or new lines.</p>
73
+ </div>
74
+ <div class="form-row">
75
+ <label for="node-input-maxFileSizeMb">
76
+ <i class="fa fa-download"></i> Max media size (MB)
77
+ </label>
78
+ <input
79
+ type="number"
80
+ id="node-input-maxFileSizeMb"
81
+ placeholder="Leave blank to disable"
82
+ min="0"
83
+ step="0.1"
84
+ style="width: 60%"
85
+ />
86
+ </div>
61
87
  </script>
62
88
 
63
89
  <script type="text/html" data-help-name="receiver">
@@ -86,6 +112,16 @@
86
112
  <span class="property-type">string</span>
87
113
  </dt>
88
114
  <dd>A newline-separated list of user IDs to ignore. Messages from these users will not trigger the output.</dd>
115
+
116
+ <dt>Ignore message types
117
+ <span class="property-type">string</span>
118
+ </dt>
119
+ <dd>List of message or media types to skip (for example <code>video</code>, <code>document</code>, or <code>sticker</code>). Separate multiple entries with commas or new lines.</dd>
120
+
121
+ <dt>Max media size (MB)
122
+ <span class="property-type">number</span>
123
+ </dt>
124
+ <dd>Skip updates that include media exceeding this size. Leave blank to process all media.</dd>
89
125
  </dl>
90
126
 
91
127
  <h3>Details</h3>
@@ -128,8 +164,7 @@
128
164
  <ul>
129
165
  <li>Ensure the Telegram bot has sufficient permissions to receive messages in the configured chat or channel.</li>
130
166
  <li>The <b>Ignore List</b> only filters messages based on the sender's user ID.</li>
167
+ <li>Use <b>Ignore message types</b> to drop updates that contain media you don't want to process, such as large videos or documents.</li>
131
168
  <li>For advanced filtering based on message content, consider chaining this node with additional processing nodes in Node-RED.</li>
132
169
  </ul>
133
170
  </script>
134
-
135
-
package/nodes/receiver.js CHANGED
@@ -1,6 +1,153 @@
1
1
  const { NewMessage } = require("telegram/events");
2
2
  const util = require("util");
3
3
 
4
+ const splitList = (value) => {
5
+ if (typeof value !== 'string') {
6
+ return [];
7
+ }
8
+ return value
9
+ .split(/[\n,\r]/)
10
+ .map((entry) => entry.trim())
11
+ .filter(Boolean);
12
+ };
13
+
14
+ const toLowerCaseSet = (values) => {
15
+ const result = new Set();
16
+ for (const value of values) {
17
+ result.add(value.toLowerCase());
18
+ }
19
+ return result;
20
+ };
21
+
22
+ const addType = (target, value) => {
23
+ if (!value) {
24
+ return;
25
+ }
26
+ target.add(String(value).toLowerCase());
27
+ };
28
+
29
+ const collectDocumentTypes = (document, types) => {
30
+ if (!document) {
31
+ return;
32
+ }
33
+
34
+ addType(types, 'document');
35
+
36
+ if (Array.isArray(document.attributes)) {
37
+ for (const attribute of document.attributes) {
38
+ if (!attribute) {
39
+ continue;
40
+ }
41
+ const attributeName = attribute.className || attribute._;
42
+ addType(types, attributeName);
43
+ switch (attributeName) {
44
+ case 'DocumentAttributeVideo':
45
+ addType(types, 'video');
46
+ break;
47
+ case 'DocumentAttributeAudio':
48
+ addType(types, 'audio');
49
+ if (attribute.voice) {
50
+ addType(types, 'voice');
51
+ }
52
+ break;
53
+ case 'DocumentAttributeAnimated':
54
+ addType(types, 'animation');
55
+ break;
56
+ case 'DocumentAttributeSticker':
57
+ addType(types, 'sticker');
58
+ break;
59
+ default:
60
+ break;
61
+ }
62
+ }
63
+ }
64
+
65
+ if (typeof document.mimeType === 'string') {
66
+ const mimeType = document.mimeType.toLowerCase();
67
+ addType(types, mimeType);
68
+ const slashIndex = mimeType.indexOf('/');
69
+ if (slashIndex > 0) {
70
+ addType(types, mimeType.slice(0, slashIndex));
71
+ }
72
+ if (mimeType.startsWith('video/')) {
73
+ addType(types, 'video');
74
+ } else if (mimeType.startsWith('audio/')) {
75
+ addType(types, 'audio');
76
+ } else if (mimeType.startsWith('image/')) {
77
+ addType(types, 'image');
78
+ }
79
+ }
80
+ };
81
+
82
+ const collectMediaTypes = (media, types) => {
83
+ if (!media) {
84
+ return;
85
+ }
86
+ addType(types, 'media');
87
+ addType(types, media.className || media._);
88
+
89
+ if (media.document) {
90
+ collectDocumentTypes(media.document, types);
91
+ }
92
+ if (media.photo) {
93
+ addType(types, 'photo');
94
+ }
95
+ if (media.webpage) {
96
+ addType(types, 'webpage');
97
+ if (media.webpage.document) {
98
+ collectDocumentTypes(media.webpage.document, types);
99
+ }
100
+ if (media.webpage.photo) {
101
+ addType(types, 'photo');
102
+ }
103
+ }
104
+ if (media.poll) {
105
+ addType(types, 'poll');
106
+ }
107
+ if (media.contact) {
108
+ addType(types, 'contact');
109
+ }
110
+ if (media.geo || media.geoPoint) {
111
+ addType(types, 'location');
112
+ }
113
+ if (media.venue) {
114
+ addType(types, 'venue');
115
+ }
116
+ if (media.game) {
117
+ addType(types, 'game');
118
+ }
119
+ if (media.sticker) {
120
+ addType(types, 'sticker');
121
+ }
122
+ };
123
+
124
+ const collectMessageTypes = (message) => {
125
+ const types = new Set();
126
+ if (!message || typeof message !== 'object') {
127
+ return types;
128
+ }
129
+
130
+ collectMediaTypes(message.media, types);
131
+
132
+ if (typeof message.message === 'string' && message.message.length > 0 && !message.media) {
133
+ addType(types, 'text');
134
+ }
135
+
136
+ if (message.action) {
137
+ const actionName = message.action.className || message.action._;
138
+ addType(types, actionName);
139
+ if (actionName) {
140
+ addType(types, 'service');
141
+ }
142
+ }
143
+
144
+ if (message.ttlPeriod) {
145
+ addType(types, 'self-destructing');
146
+ }
147
+
148
+ return types;
149
+ };
150
+
4
151
  module.exports = function (RED) {
5
152
  function Receiver(config) {
6
153
  RED.nodes.createNode(this, config);
@@ -8,7 +155,90 @@ module.exports = function (RED) {
8
155
  this.debugEnabled = config.debug;
9
156
  var node = this;
10
157
  const client = this.config.client;
11
- const ignore = config.ignore.split(/\n/);
158
+ const ignore = splitList(config.ignore || "");
159
+ const ignoredMessageTypes = toLowerCaseSet(splitList(config.ignoreMessageTypes || ""));
160
+ const maxFileSizeMb = Number(config.maxFileSizeMb);
161
+ const maxFileSizeBytes = Number.isFinite(maxFileSizeMb) && maxFileSizeMb > 0
162
+ ? maxFileSizeMb * 1024 * 1024
163
+ : null;
164
+
165
+ const toNumber = (value) => {
166
+ if (typeof value === 'number') {
167
+ return value;
168
+ }
169
+ if (typeof value === 'bigint') {
170
+ const result = Number(value);
171
+ return Number.isFinite(result) ? result : Number.MAX_SAFE_INTEGER;
172
+ }
173
+ return undefined;
174
+ };
175
+
176
+ const extractPhotoSize = (photo) => {
177
+ if (!photo || !Array.isArray(photo.sizes)) {
178
+ return null;
179
+ }
180
+ let max = 0;
181
+ for (const size of photo.sizes) {
182
+ if (size == null) {
183
+ continue;
184
+ }
185
+ if (Array.isArray(size.sizes)) {
186
+ for (const nested of size.sizes) {
187
+ const nestedValue = toNumber(nested);
188
+ if (nestedValue != null && nestedValue > max) {
189
+ max = nestedValue;
190
+ }
191
+ }
192
+ }
193
+ const value = toNumber(size.size ?? size.length ?? size.bytes);
194
+ if (value != null && value > max) {
195
+ max = value;
196
+ }
197
+ }
198
+ return max > 0 ? max : null;
199
+ };
200
+
201
+ const extractMediaSize = (media) => {
202
+ if (!media) {
203
+ return null;
204
+ }
205
+
206
+ if (media.document && media.document.size != null) {
207
+ const value = toNumber(media.document.size);
208
+ if (value != null) {
209
+ return value;
210
+ }
211
+ }
212
+
213
+ if (media.photo) {
214
+ const photoSize = extractPhotoSize(media.photo);
215
+ if (photoSize != null) {
216
+ return photoSize;
217
+ }
218
+ }
219
+
220
+ if (media.webpage) {
221
+ const { document, photo } = media.webpage;
222
+ const docSize = document && toNumber(document.size);
223
+ if (docSize != null) {
224
+ return docSize;
225
+ }
226
+ const photoSize = extractPhotoSize(photo);
227
+ if (photoSize != null) {
228
+ return photoSize;
229
+ }
230
+ }
231
+
232
+ const className = media.className || media._;
233
+ if ((className === 'MessageMediaDocument' || className === 'MessageMediaPhoto') && media.size != null) {
234
+ const value = toNumber(media.size);
235
+ if (value != null) {
236
+ return value;
237
+ }
238
+ }
239
+
240
+ return null;
241
+ };
12
242
 
13
243
  const event = new NewMessage();
14
244
  const handler = (update) => {
@@ -16,7 +246,38 @@ module.exports = function (RED) {
16
246
  if (debug) {
17
247
  node.log('receiver update: ' + util.inspect(update, { depth: null }));
18
248
  }
19
- if (update.message.fromId != null && update.message.fromId.userId != null && !ignore.includes(update.message.fromId.userId.toString())) {
249
+ const message = update && update.message;
250
+ if (!message) {
251
+ return;
252
+ }
253
+
254
+ if (ignoredMessageTypes.size > 0) {
255
+ const messageTypes = collectMessageTypes(message);
256
+ const shouldIgnoreType = Array.from(messageTypes).some((type) => ignoredMessageTypes.has(type));
257
+ if (shouldIgnoreType) {
258
+ if (debug) {
259
+ node.log(`receiver ignoring update with types: ${Array.from(messageTypes).join(', ')}`);
260
+ }
261
+ return;
262
+ }
263
+ }
264
+
265
+ if (maxFileSizeBytes != null) {
266
+ const mediaSize = extractMediaSize(message.media);
267
+ if (mediaSize != null && mediaSize > maxFileSizeBytes) {
268
+ if (debug) {
269
+ node.log(`receiver ignoring update with media size ${mediaSize} bytes exceeding limit ${maxFileSizeBytes}`);
270
+ }
271
+ return;
272
+ }
273
+ }
274
+
275
+ const senderId = message.fromId && message.fromId.userId;
276
+ if (senderId != null && ignore.includes(senderId.toString())) {
277
+ return;
278
+ }
279
+
280
+ if (message.fromId != null && message.fromId.userId != null) {
20
281
  const out = { payload: { update } };
21
282
  node.send(out);
22
283
  if (debug) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patricktobias86/node-red-telegram-account",
3
- "version": "1.1.13",
3
+ "version": "1.1.16",
4
4
  "description": "Node-RED nodes to communicate with GramJS.",
5
5
  "main": "nodes/config.js",
6
6
  "keywords": [
@@ -40,4 +40,52 @@ describe('Receiver node', function() {
40
40
  assert.strictEqual(removeCalls[0].fn, addCalls[0].fn);
41
41
  assert.strictEqual(removeCalls[0].event, addCalls[0].event);
42
42
  });
43
+
44
+ it('skips media updates when size exceeds threshold', function() {
45
+ const { NodeCtor, addCalls } = load();
46
+ const sent = [];
47
+ const node = new NodeCtor({config:'c', ignore:'', ignoreMessageTypes:'', maxFileSizeMb:'5'});
48
+ node.send = (msg) => sent.push(msg);
49
+ const handler = addCalls[0].fn;
50
+
51
+ handler({ message: { fromId: { userId: 123 }, media: { document: { size: 6 * 1024 * 1024 } } } });
52
+
53
+ assert.strictEqual(sent.length, 0);
54
+ });
55
+
56
+ it('delivers media updates when size is below threshold', function() {
57
+ const { NodeCtor, addCalls } = load();
58
+ const sent = [];
59
+ const node = new NodeCtor({config:'c', ignore:'', ignoreMessageTypes:'', maxFileSizeMb:'5'});
60
+ node.send = (msg) => sent.push(msg);
61
+ const handler = addCalls[0].fn;
62
+
63
+ handler({ message: { fromId: { userId: 123 }, media: { document: { size: 3 * 1024 * 1024 } } } });
64
+
65
+ assert.strictEqual(sent.length, 1);
66
+ });
67
+
68
+ it('skips updates when media type is ignored', function() {
69
+ const { NodeCtor, addCalls } = load();
70
+ const sent = [];
71
+ const node = new NodeCtor({config:'c', ignore:'', ignoreMessageTypes:'video\ndocument', maxFileSizeMb:''});
72
+ node.send = (msg) => sent.push(msg);
73
+ const handler = addCalls[0].fn;
74
+
75
+ handler({ message: { fromId: { userId: 123 }, media: { document: { mimeType: 'video/mp4', attributes: [{ className: 'DocumentAttributeVideo' }] } } } });
76
+
77
+ assert.strictEqual(sent.length, 0);
78
+ });
79
+
80
+ it('delivers updates when media type is not ignored', function() {
81
+ const { NodeCtor, addCalls } = load();
82
+ const sent = [];
83
+ const node = new NodeCtor({config:'c', ignore:'', ignoreMessageTypes:'voice', maxFileSizeMb:''});
84
+ node.send = (msg) => sent.push(msg);
85
+ const handler = addCalls[0].fn;
86
+
87
+ handler({ message: { fromId: { userId: 123 }, media: { document: { mimeType: 'video/mp4', attributes: [{ className: 'DocumentAttributeVideo' }] } } } });
88
+
89
+ assert.strictEqual(sent.length, 1);
90
+ });
43
91
  });