diff --git a/package.json b/package.json index 741e152..7b46b3f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@random-mirrors/node-red-telegram-account", - "version": "2.0.1", + "version": "2.0.7", "description": "", "main": "index.js", "scripts": { @@ -21,6 +21,7 @@ "telegram-account-send-message": "sample/send-message.js", "telegram-account-send-file": "sample/send-file.js", "telegram-account-entity": "sample/get-entity.js", + "telegram-account-folders": "sample/get-folders.js", "telegram-account-delete-message": "sample/delete-message.js", "telegram-account-iter-dialog": "sample/iter-dialogs.js", "telegram-account-iter-messages": "sample/iter-messages.js" diff --git a/sample/auth.html b/sample/auth.html index 6368f5c..2a96044 100644 --- a/sample/auth.html +++ b/sample/auth.html @@ -7,6 +7,7 @@ api_hash: { value: "", required: true }, phoneNumber: { value: "", required: true }, password: { value: "" }, + sessionLocation: { value: "" }, }, inputs: 1, outputs: 1, @@ -20,6 +21,7 @@ $("#node-input-api_hash").val(this.api_hash || ""); $("#node-input-phoneNumber").val(this.phoneNumber || ""); $("#node-input-password").val(this.password || ""); + $("#node-input-sessionLocation").val(this.sessionLocation || ""); }, oneditsave: function () { // Saving values @@ -27,6 +29,7 @@ this.api_hash = $("#node-input-api_hash").val(); this.phoneNumber = $("#node-input-phoneNumber").val(); this.password = $("#node-input-password").val(); + this.sessionLocation = $("#node-input-sessionLocation").val(); }, }); @@ -70,6 +73,17 @@ /> +
+ + +
+

Note: This configuration is required for connecting to the Telegram API and starting a session. @@ -111,6 +125,14 @@ The password for two-factor authentication (if enabled on the Telegram account). + +

+ payload.sessionLocation + string +
+
+ The location to store the session string. Uses stringSession by default. +

Outputs

diff --git a/sample/auth.js b/sample/auth.js index fa00b23..37dcec2 100644 --- a/sample/auth.js +++ b/sample/auth.js @@ -1,5 +1,5 @@ const { TelegramClient } = require("telegram"); -const { StringSession } = require("telegram/sessions"); +const { StringSession, StoreSession } = require("telegram/sessions"); module.exports = function (RED) { function TelegramClientConfig(config) { @@ -11,9 +11,14 @@ module.exports = function (RED) { const api_hash = msg.payload.api_hash || config.api_hash; const phoneNumber = msg.payload.phoneNumber || config.phoneNumber; const password = msg.payload.password || config.password; + const sessionLocation = + msg.payload.sessionLocation || config.sessionLocation || ""; const api_id = parseInt(api_idString); - const session = new StringSession(""); + const session = + sessionLocation != "" + ? new StoreSession(sessionLocation) + : new StringSession(""); const client = new TelegramClient(session, api_id, api_hash, { connectionRetries: 5, }); diff --git a/sample/get-folders.html b/sample/get-folders.html new file mode 100644 index 0000000..974f4f7 --- /dev/null +++ b/sample/get-folders.html @@ -0,0 +1,77 @@ + + + + + diff --git a/sample/get-folders.js b/sample/get-folders.js new file mode 100644 index 0000000..0e003b8 --- /dev/null +++ b/sample/get-folders.js @@ -0,0 +1,33 @@ +const { Api, TelegramClient } = require("telegram"); + +module.exports = function (RED) { + function GetFolders(config) { + RED.nodes.createNode(this, config); + this.config = RED.nodes.getNode(config.config); + var node = this; + + this.on("input", async function (msg) { + /** @type {TelegramClient} */ + const client = msg.payload?.client + ? msg.payload.client + : this.config.client; + + try { + const { filters } = await client.invoke( + new Api.messages.GetDialogFilters({}) + ); + + // Send folder list as output + node.send({ + payload: { folders: filters }, + }); + } catch (err) { + // Handle errors + node.error("Error fetching folders: " + err.message); + } + }); + } + + // Register the custom node with Node-RED + RED.nodes.registerType("get-folders", GetFolders); +};