UNPKG

@wasserstoff/mangi-tg-bot

Version:

A powerful Telegram Bot SDK with built-in authentication, session management, and database integration

118 lines 4.29 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TelegramManager = void 0; const telegram_1 = require("telegram"); const sessions_1 = require("telegram/sessions"); const telegram_2 = require("telegram"); const input = __importStar(require("input")); const node_biginteger_1 = __importDefault(require("node-biginteger")); class TelegramManager { client; stringSession; apiId; apiHash; constructor(apiId, apiHash, session = "") { this.apiId = apiId; this.apiHash = apiHash; this.stringSession = new sessions_1.StringSession(session); this.client = new telegram_1.TelegramClient(this.stringSession, this.apiId, this.apiHash, { connectionRetries: 5 }); } async getClient() { return this.client; } async start(phone) { return await this.client.start({ phoneNumber: phone, phoneCode: async () => { return await input.text("Please enter the code you received: "); }, onError: (err) => console.error(err), }); } async connect() { return await this.client.connect(); } async createGroup(users, title) { try { const result = await this.client.invoke(new telegram_2.Api.messages.CreateChat({ users: users, // provide an array of valid usernames or IDs title: title, })); return result; } catch (error) { console.error("Error creating group:", error); } } async createChannel(title, about) { try { const result = await this.client.invoke(new telegram_2.Api.channels.CreateChannel({ broadcast: true, // true creates a channel; false creates a megagroup title: title, about: about, })); return result; } catch (error) { console.error("Error creating channel:", error); } } async addUserToGroup(chatId, userId) { try { const inputUser = await this.client.getInputEntity(userId); const result = await this.client.invoke(new telegram_2.Api.messages.AddChatUser({ chatId: (0, node_biginteger_1.default)(chatId), userId: inputUser, fwdLimit: 0, // 0 means no history is shared })); return result; } catch (error) { console.error("Error adding user to group:", error); } } async disconnect() { await this.client.disconnect(); } getSession() { return this.stringSession.save(); } } exports.TelegramManager = TelegramManager; //# sourceMappingURL=BotClient.js.map