UNPKG

@wasserstoff/mangi-tg-bot

Version:

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

68 lines 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apiHash = exports.apiId = void 0; const telegram_1 = require("telegram"); const sessions_1 = require("telegram/sessions"); const telegram_2 = require("telegram"); // Replace with your own api_id and api_hash (from https://my.telegram.org) exports.apiId = 20316022; // your api_id (number) exports.apiHash = "248e85787b42cd6d342ec0f725b38ebb"; // your api_hash (string) let stringSession = new sessions_1.StringSession(""); // empty session string initially async function createGroup(client) { try { console.log("Creating group..."); const result = await client.invoke(new telegram_2.Api.messages.CreateChat({ users: ["username1", "username2"], // replace with valid usernames or IDs title: "My New Group", })); console.log("Group created:", result); } catch (error) { console.error("Error creating group:", error); } } async function createChannel(client) { try { console.log("Creating channel..."); const result = await client.invoke(new telegram_2.Api.channels.CreateChannel({ broadcast: true, title: "My New Channel", about: "This channel is created programmatically using GramJS", })); console.log("Channel created:", result); } catch (error) { console.error("Error creating channel:", error); } } async function main() { console.log("Connecting to Telegram..."); const client = new telegram_1.TelegramClient(stringSession, exports.apiId, exports.apiHash, { connectionRetries: 5 }); await client.start({ phoneNumber: async () => "+918877756207", // Your phone number // password: async () => "your_2fa_password_here", // If 2FA is enabled phoneCode: async () => { // ⚠️ You must still manually read the code from Telegram console.log("💬 Waiting for the code..."); return await new Promise(resolve => { // Simulate receiving code, e.g., from your own backend or wait for user input here setTimeout(() => { resolve("12345"); // Replace with real code (via UI, API, or file) }, 10000); }); }, onError: (err) => console.log(err), }); console.log("You are now connected!"); stringSession = client.session; console.log("My session", stringSession); // Create a group and a channel (uncomment as needed) // await createGroup(client); await createChannel(client); // Optionally, save the session string for future runs: // console.log("Your session string:", client.session.save()); // await client.disconnect(); console.log("Disconnected from Telegram."); } main(); //# sourceMappingURL=test.js.map