Implement get-folders

This commit is contained in:
Pijus Kamandulis 2025-01-21 00:02:00 +02:00
parent 3390e6f093
commit 8c9e7e5897
5 changed files with 141 additions and 3 deletions

View File

@ -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"

View File

@ -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();
},
});
</script>
@ -70,6 +73,17 @@
/>
</div>
<div class="form-row">
<label for="node-input-sessionLocation">
<i class="fa fa-folder"></i> Session Location
</label>
<input
type="text"
id="node-input-sessionLocation"
placeholder="Enter the session location"
/>
</div>
<p>
<strong>Note:</strong> 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).
</dd>
<dt>
payload.sessionLocation
<span class="property-type">string</span>
</dt>
<dd>
The location to store the session string. Uses stringSession by default.
</dd>
</dl>
<h3>Outputs</h3>

View File

@ -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,
});

77
sample/get-folders.html Normal file
View File

@ -0,0 +1,77 @@
<script type="text/javascript">
RED.nodes.registerType("get-folders", {
category: "telegram-account",
color: "#229ED9",
icon: "tg.png",
align: "right",
defaults: {
name: { value: "" },
config: {
type: "config",
required: false,
},
},
inputs: 1,
outputs: 1,
label: function () {
return this.name || "Get Folders";
},
});
</script>
<script type="text/html" data-template-name="get-folders">
<div class="form-row">
<label for="node-input-name"> <i class="fa fa-tag"></i> Name </label>
<input
type="text"
id="node-input-name"
placeholder="Name"
style="width: 60%"
/>
</div>
<div class="form-row">
<label for="node-input-config"> <i class="fa fa-tag"></i> Config </label>
<input
type="text"
id="node-input-config"
placeholder="Config"
style="width: 60%"
/>
</div>
</script>
<script type="text/html" data-help-name="get-folders">
<p>
The <b>get-folders</b> node retrieves a list of folders in a Telegram
account.
</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>
payload.client
<span class="property-type">object</span>
</dt>
<dd>An optional Telegram client instance if not configured globally.</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<dt>
payload.folders
<span class="property-type">array</span>
</dt>
<dd>
An array of folder objects. Each folder object contains the following
properties:
<ul>
<li><b>id</b> (string): The unique identifier of the folder.</li>
<li><b>name</b> (string): The name of the folder.</li>
<li>
<b>chatIds</b> (array): An array of chat IDs that belong to the
folder.
</li>
</ul>
</dd>
</dl>
</script>

33
sample/get-folders.js Normal file
View File

@ -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);
};