tgram.js
Version:
Lightweight modern library for telegram bot. use Node.js
108 lines (88 loc) • 3.38 kB
JavaScript
import { getUpdates } from "./api/getUpdates.js";
import { sendMessage as rawSendMessage } from "./messages/messages-send.js";
import { createContext as cContext } from "./context/create-context.js";
import { sendPhoto as rawSendPhoto } from "./messages/messages-send-photo.js";
import { sendAudio as rawSendAudio } from "./messages/messages-send-audio.js";
import { sendVideo as rawSendVideo } from "./messages/messages-send-video.js";
import { sendSticker as rawsendSticker } from "./messages/messages-send-sticker.js";
import { sendDocument as rawSendDocument } from "./messages/messages-send-doc.js";
import { sendButton as rawSendButton } from "./messages/button/button-send.js";
/*
* Telegram Class For Connect To Telegram.
* @returns @string
*/
export class TelegramJS {
constructor(options = {}) {
this.token = options.token;
this.prefix = options.prefix || "/";
this.middlewares = [];
this._commands = new Map();
this.offset = 0;
this.options = { polling: true, ...options };
this.ready = false;
}
async sendMessage(chatId, text) {
return rawSendMessage(this.token, chatId, text);
}
async sendPhotoMessage(chatId, photo, caption) {
return rawSendPhoto(this.token, chatId, photo, captiom);
}
async sendAudioMessage(chatId, audioUrl, filename, caption) {
return rawSendAudio(this.token, chatId, audioUrl, filename, caption);
}
async sendVideoMessage(chatId, videoUrl, caption) {
return rawSendVideo(this.token, chatId, videoUrl, caption)
}
async sendStickerMessage(chatId, stickerUrl, filename) {
return rawSendSticker(this.token, chatId, stickerUrl, filename)
}
async sendDocumentMessage(chatId, document, filename, caption) {
return rawSendDocument(this.token, chatId, document, filename, caption)
}
async sendButtonMessage(chatId, text, button, options) {
return rawSendButton(this.token, chatId, text, button, )
}
use(fn) {
this.middlewares.push(fn);
}
commands(cmd, fn) {
this._commands.set(cmd.toLowerCase(), fn);
}
setup() {
try {
console.log(`[Setup] Commands Loaded: ${this._commands.size}`);
this.ready = true;
} catch (errorSetup) {
console.error(`[Setup Error]`, errorSetup);
process.exit(1);
}
}
async start() {
if (!this.ready) throw new Error("Panggil bot.setup() dulu agar bisa berjalan");
console.log("Polling Started...");
while (true) {
try {
const updates = await getUpdates(this.token, this.offset);
for (const update of updates) {
this.offset = update.update_id + 1;
const tg = cContext(update, this.token, this.prefix);
try {
for (const mw of this.middlewares) await mw(tg);
console.log("[DEBUG] command =", tg.command);
const handler = this._commands.get(tg.command); // \\
if (handler) {
const text = tg.text?.trim().split(" ").slice(1).join(" ") || "";
await handler(tg, text)
}
} catch (err) {
console.error("[Handler Error]", err);
await tg.reply("⚠️ Terjadi error saat memproses perintah.");
}
}
} catch (err) {
console.error("[Polling Error]", err.message);
await new Promise((r) => setTimeout(r, 3000));
}
}
}
}