0.10.0 • Published 11 months ago

telebunny v0.10.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Lightweight zero-dependency Node.js module for Telegram Bot API.

Bot API Version Node Version NPM Package Version

https://t.me/FBFunnyBunnyFB

📥 Installation

npm i telebunny

🐇 Usage (Polling)

import { TeleBunny } from "telebunny";

// Replace <TOKEN> with your bot token
const BOT_TOKEN = "<TOKEN>";
// Use "polling" option for fetching new updates using polling method
const TelegramBot = new TeleBunny(BOT_TOKEN, { polling: true });

// Use listeners to get data from Telegram users
// Update type parameter can be found on official Telegram Bot API
TelegramBot.on('message', msg => {
    // Send response message to chat
    TelegramBot.sendMessage(msg.chat.id, "Got message update");
});

// You can also use "got" method as the alias for "on" listeners
TelegramBot.got('edited_message', msg => {
    // TeleBunny class instance supports two different option masks
    // You can pass options to method in such a way:
    // ~ pass all options in first argument as object
    // OR
    // ~ pass options according to API required fields
    TelegramBot.sendMessage(msg.chat.id, "Got edited_message update", {
        allow_sending_without_reply: true,
        reply_to_message_id: msg.message_id
    });
    // Uncomment to test alternative method
    /*
    TelegramBot.sendMessage({
        chat_id: msg.chat.id,
        text: "Got edited_message update (raw options)",
        allow_sending_without_reply: true,
        reply_to_message_id: msg.message_id
    });
    */
})

🐇 Usage (Webhook)

// Production server code example
// You can use any library/framework for handling HTTP/HTTPS requests
import { TeleBunny } from "telebunny";
import { createServer } from "http";

// Replace <TOKEN> with your bot token
const BOT_TOKEN = "<TOKEN>";
// Replace <WEBHOOK> with your url
const WEBHOOK_URL = "<WEBHOOK>"+BOT_TOKEN;
// You don't need to set "polling" option when using webhook
const TelegramBot = new TeleBunny(BOT_TOKEN);

// Set webhook
TelegramBot.setWebhook(WEBHOOK_URL);

// Add listener for inline requests
TelegramBot.on("inline_query", msg => {
    const query = msg.query || "npm telebunny";
    TelegramBot.answerInlineQuery({
        inline_query_id: msg.id,
        button: {
            text: "Search \""+query+"\" in Google",
            web_app: {
                url: "https://www.google.com/search?q="+encodeURIComponent(query)
            }
        }
    });
});

// Create HTTP server
createServer((req, res) => {
    // Process only POST requests for webhook route
    if(req.method !== "POST" || req.url !== "/"+BOT_TOKEN) return;
    const chunks:any = [];
    req.on('data', (chunk) => chunks.push(chunk));
    req.on('end', () => {
        const data = Buffer.concat(chunks);
        // To process incoming messages use processUpdate() method
        // "data" variable must be Buffer or object type
        TelegramBot.processUpdate(data);
    });
    res.writeHead(200);
    res.end();
}).listen(process.env.PORT || 3000);

🔑 License

The MIT License (MIT)

Copyright © 2023 Funny Bunny

0.10.0

11 months ago

0.9.0

11 months ago

0.9.1

11 months ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.1.0

1 year ago