@patricktobias86/node-red-telegram-account 1.1.3 → 1.1.6

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/README.md CHANGED
@@ -1,11 +1,22 @@
1
1
  # Node-RED nodes to communicate with GramJS
2
2
 
3
3
  <p align="center">
4
- <img alt="Github issues" src="https://img.shields.io/github/issues/patricktobias86/node-red-telegram-account?color=56BEB8" />
5
- <img alt="Github forks" src="https://img.shields.io/github/forks/patricktobias86/node-red-telegram-account?color=56BEB8" />
6
- <img alt="Github stars" src="https://img.shields.io/github/stars/patricktobias86/node-red-telegram-account?color=56BEB8" />
4
+ <img alt="GitHub issues" src="https://img.shields.io/github/issues/patricktobias86/node-red-telegram-account?color=56BEB8" />
5
+ <img alt="GitHub forks" src="https://img.shields.io/github/forks/patricktobias86/node-red-telegram-account?color=56BEB8" />
6
+ <img alt="GitHub stars" src="https://img.shields.io/github/stars/patricktobias86/node-red-telegram-account?color=56BEB8" />
7
7
  </p>
8
8
 
9
9
  ```bash
10
10
  npm i @patricktobias86/node-red-telegram-account
11
- ```
11
+ ```
12
+
13
+ ## Nodes
14
+
15
+ - resolve-userid – resolve a Telegram username to its numeric user ID
16
+
17
+ ## Session management
18
+
19
+ Connections to Telegram are cached by the configuration node. When the flow is
20
+ redeployed, the existing session is reused instead of creating a new one.
21
+ The client is only disconnected once no nodes reference that session anymore.
22
+
package/nodes/config.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const { TelegramClient } = require("telegram");
2
2
  const { StringSession } = require("telegram/sessions");
3
3
 
4
- const activeClients = {}; // Cache: session string → connected client
4
+ const activeClients = {}; // Cache: session string → { client, refCount }
5
5
 
6
6
  module.exports = function (RED) {
7
7
  function TelegramClientConfig(config) {
@@ -18,7 +18,9 @@ module.exports = function (RED) {
18
18
 
19
19
  if (activeClients[sessionStr]) {
20
20
  // Reuse existing client
21
- this.client = activeClients[sessionStr];
21
+ const record = activeClients[sessionStr];
22
+ this.client = record.client;
23
+ record.refCount += 1;
22
24
  node.status({ fill: "green", shape: "dot", text: "Reused existing client" });
23
25
  } else {
24
26
  // Create and connect new client
@@ -28,24 +30,35 @@ module.exports = function (RED) {
28
30
  requestRetries: config.requestRetries || 5,
29
31
  });
30
32
 
33
+ // Pre-store with refCount to ensure reuse during connection setup
34
+ activeClients[sessionStr] = { client: this.client, refCount: 1 };
35
+
31
36
  this.client.connect().then(async () => {
32
37
  const authorized = await this.client.isUserAuthorized();
33
38
  if (!authorized) {
34
39
  node.error("Session is invalid");
35
40
  } else {
36
41
  node.status({ fill: "green", shape: "dot", text: "Connected" });
37
- activeClients[sessionStr] = this.client;
38
42
  }
39
43
  }).catch(err => {
40
44
  node.error("Connection error: " + err.message);
45
+ delete activeClients[sessionStr];
41
46
  });
42
47
  }
43
48
 
44
49
  this.on("close", async () => {
45
- if (this.client && activeClients[sessionStr] === this.client) {
46
- await this.client.disconnect();
47
- delete activeClients[sessionStr];
48
- node.status({ fill: "red", shape: "ring", text: "Disconnected" });
50
+ const record = activeClients[sessionStr];
51
+ if (record && record.client === this.client) {
52
+ record.refCount -= 1;
53
+ if (record.refCount <= 0) {
54
+ try {
55
+ await this.client.disconnect();
56
+ } catch (err) {
57
+ node.error("Disconnect error: " + err.message);
58
+ }
59
+ delete activeClients[sessionStr];
60
+ node.status({ fill: "red", shape: "ring", text: "Disconnected" });
61
+ }
49
62
  }
50
63
  });
51
64
  }
@@ -0,0 +1,56 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('resolve-userid', {
3
+ category: 'telegram-account',
4
+ color: '#229ED9',
5
+ icon: 'tg.png',
6
+ align: 'right',
7
+ defaults: {
8
+ name: { value: '' },
9
+ config: { type: 'config', required: false },
10
+ username: { value: '' }
11
+ },
12
+ inputs: 1,
13
+ outputs: 1,
14
+ paletteLabel: 'resolve userid',
15
+ label: function() {
16
+ return this.name||'resolve userid';
17
+ }
18
+ });
19
+ </script>
20
+
21
+ <script type="text/html" data-template-name="resolve-userid">
22
+ <div class="form-row">
23
+ <label for="node-input-name">
24
+ <i class="fa fa-tag"></i> Name
25
+ </label>
26
+ <input type="text" id="node-input-name" placeholder="Name" style="width: 60%" />
27
+ </div>
28
+ <div class="form-row">
29
+ <label for="node-input-config">
30
+ <i class="fa fa-gear"></i> Config
31
+ </label>
32
+ <input type="text" id="node-input-config" placeholder="Config" style="width: 60%" />
33
+ </div>
34
+ <div class="form-row">
35
+ <label for="node-input-username">
36
+ <i class="fa fa-user"></i> Username
37
+ </label>
38
+ <input type="text" id="node-input-username" placeholder="@username" style="width: 60%" />
39
+ </div>
40
+ </script>
41
+
42
+ <script type="text/html" data-help-name="resolve-userid">
43
+ <p>The <b>resolve-userid</b> node converts a Telegram username into a numeric user ID.</p>
44
+ <h3>Inputs</h3>
45
+ <dl class="message-properties">
46
+ <dt>payload.username <span class="property-type">string</span></dt>
47
+ <dd>The Telegram username to resolve, e.g., <code>@someuser</code>.</dd>
48
+ <dt>payload.client <span class="property-type">object</span></dt>
49
+ <dd>Optional Telegram client instance. If omitted, the client from the configuration node is used.</dd>
50
+ </dl>
51
+ <h3>Outputs</h3>
52
+ <dl class="message-properties">
53
+ <dt>payload.userId <span class="property-type">number | bigint</span></dt>
54
+ <dd>The resolved Telegram user ID, or <code>null</code> if resolution fails.</dd>
55
+ </dl>
56
+ </script>
@@ -0,0 +1,36 @@
1
+ module.exports = function(RED) {
2
+ function ResolveUserId(config) {
3
+ RED.nodes.createNode(this, config);
4
+ this.config = RED.nodes.getNode(config.config);
5
+ var node = this;
6
+
7
+ this.on('input', async function(msg) {
8
+ const username = msg.payload?.username || config.username;
9
+ const client = msg.payload?.client ? msg.payload.client : node.config?.client;
10
+
11
+ if (!username) {
12
+ node.error('No username provided');
13
+ return;
14
+ }
15
+ if (!client) {
16
+ node.error('Telegram client not configured');
17
+ return;
18
+ }
19
+
20
+ try {
21
+ const entity = await client.getEntity(username);
22
+ let userId;
23
+ if (entity?.id) {
24
+ userId = entity.id;
25
+ } else if (entity?.userId) {
26
+ userId = entity.userId;
27
+ }
28
+ node.send({ payload: { userId } });
29
+ } catch (err) {
30
+ node.error('Error resolving username: ' + err.message);
31
+ node.send({ payload: { userId: null } });
32
+ }
33
+ });
34
+ }
35
+ RED.nodes.registerType('resolve-userid', ResolveUserId);
36
+ };
@@ -102,7 +102,7 @@
102
102
  </div>
103
103
 
104
104
  <div class="form-row">
105
- <label for="node-input-chat_id">
105
+ <label for="node-input-chatId">
106
106
  <i class="fa fa-user"></i> Chat ID
107
107
  </label>
108
108
  <input type="text" id="node-input-chatId" placeholder="Peer ID">
@@ -48,7 +48,7 @@ module.exports = function (RED) {
48
48
  supportStreaming: supportStreaming,
49
49
  noforwards: noforwards,
50
50
  commentTo: commentTo !== "" ? commentTo : undefined,
51
- topMsgId: topMsgId !== topMsgId ? commentTo : undefined,
51
+ topMsgId: topMsgId !== "" ? topMsgId : undefined,
52
52
  };
53
53
 
54
54
  if (schedule) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patricktobias86/node-red-telegram-account",
3
- "version": "1.1.3",
3
+ "version": "1.1.6",
4
4
  "description": "Node-RED nodes to communicate with GramJS.",
5
5
  "main": "nodes/config.js",
6
6
  "keywords": [
@@ -35,7 +35,8 @@
35
35
  "telegram-account-delete-message": "nodes/delete-message.js",
36
36
  "telegram-account-iter-dialog": "nodes/iter-dialogs.js",
37
37
  "telegram-account-iter-messages": "nodes/iter-messages.js",
38
- "telegram-account-promote-admin": "nodes/promote-admin.js"
38
+ "telegram-account-promote-admin": "nodes/promote-admin.js",
39
+ "telegram-account-resolve-userid": "nodes/resolve-userid.js"
39
40
  }
40
41
  },
41
42
  "publishConfig": {