2025-01-21 00:02:00 +02:00

64 lines
1.9 KiB
JavaScript

const { TelegramClient } = require("telegram");
const { StringSession, StoreSession } = require("telegram/sessions");
module.exports = function (RED) {
function TelegramClientConfig(config) {
RED.nodes.createNode(this, config);
const node = this;
this.on("input", async (msg) => {
const api_idString = msg.payload.api_id || config.api_id;
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 =
sessionLocation != ""
? new StoreSession(sessionLocation)
: new StringSession("");
const client = new TelegramClient(session, api_id, api_hash, {
connectionRetries: 5,
});
let resolvePhoneCode;
const context = node.context().flow;
try {
await client.start({
phoneNumber: () => phoneNumber,
password: () => password,
phoneCode: async () => {
return new Promise((resolve) => {
resolvePhoneCode = resolve;
context.set("phoneCode", resolvePhoneCode);
});
},
onError: (err) => node.error(`Error: ${err.message}`),
});
const stringSession = client.session.save(); // Save the session
node.send({
topic: "auth_success",
payload: {
stringSession,
message: "Authorization was successful!",
},
});
} catch (error) {
node.error(`Authorization error: ${error.message}`);
node.send({
topic: "auth_error",
payload: {
error: error.message,
},
});
}
});
}
RED.nodes.registerType("auth", TelegramClientConfig);
};