From e771059809b6ff8281a8d51b059164e9c1b2fb0f Mon Sep 17 00:00:00 2001 From: borovlioff Date: Tue, 31 Dec 2024 19:04:46 +0300 Subject: [PATCH] Add auth --- sample/auth.html | 138 +++++++++++++++++++++++++++++++++++++++++++++++ sample/auth.js | 60 +++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 sample/auth.html create mode 100644 sample/auth.js diff --git a/sample/auth.html b/sample/auth.html new file mode 100644 index 0000000..28e44c0 --- /dev/null +++ b/sample/auth.html @@ -0,0 +1,138 @@ + + + + + + diff --git a/sample/auth.js b/sample/auth.js new file mode 100644 index 0000000..6ac6186 --- /dev/null +++ b/sample/auth.js @@ -0,0 +1,60 @@ +const { TelegramClient } = require("telegram"); +const { StringSession } = 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 api_id = parseInt(api_idString); + + const session = 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(`Ошибка: ${err.message}`), + }); + + const stringSession = client.session.save(); // Сохраняем сессию + node.send({ + topic: "auth_success", + payload: { + stringSession, + message: "Авторизация прошла успешно!", + }, + }); + + } catch (error) { + node.error(`Ошибка авторизации: ${error.message}`); + node.send({ + topic: "auth_error", + payload: { + error: error.message, + }, + }); + } + }); + } + + RED.nodes.registerType("auth", TelegramClientConfig); +};