Skip to content

Split Plugin

npmnpm downloadsJSRJSR Score

This package can split messages which reach the Telegram limit into multiple parts (messages). This package also split entities so you don't need to do it manually.

Usage

ts
import { splitMessage } from "@gramio/split";

const bot = new Bot(process.env.BOT_TOKEN!).command(
    "start",
    async (context) => {
        const messages = await splitMessage(
            format`${bold("a".repeat(4096 * 2))}`,
            (str) => context.send(str)
            // be worry. if u wants provide an context.send without function wrapper
            // you should use context.send.bind(context) it is required because otherwise it will lose context data
        );

        console.log(messages); // messages is array of second argument results
    }
);

await bot.start();

You can also use it in other frameworks.

ts
import { splitMessage } from "@gramio/split";

const messages = await splitMessage(
    format`${bold("a".repeat(4096 * 2))}`,
    ({ text, entities }) => {
        return someOtherFramework.sendMessage(text, { entities });
    }
);

Configuration

You can also configure maximum text length. By default it's 4096 symbols, but sendPhoto caption has limit 1024 symbols.

ts
const messages = await splitMessage(
    format`${bold("a".repeat(4096))}`,
    ({ text, entities }) => {
        return context.sendPhoto(PHOTO, {
            caption: text,
            caption_entities: entities,
        });
    },
    1024
);

NOTE

This package mostly in the WIP stage.

TODO:

  • [ ] More tests
  • [ ] Plugin with auto-split
  • [ ] Split mode by entities
  • [ ] Auto split action strategies (like sendPhoto caption next splits to sendMessage text)