Skip to content

Main bot class

Bot - the main class of the framework. You use it to interact with the Telegram Bot API.

Constructor

There are two ways to pass the token and parameters.

  1. Pass the token as the first argument and (optionally) options
ts
const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        fetchOptions: {
            headers: {
                "X-Hi-Telegram": "10",
            },
        },
    },
});
  1. Pass the options with the required token field
ts
const bot = new Bot({
    token: process.env.BOT_TOKEN,
    api: {
        fetchOptions: {
            headers: {
                "X-Hi-Telegram": "10",
            },
        },
    },
});

Default plugins

Some plugins are used by default, but you can disable them.

ts
const bot = new Bot(process.env.BOT_TOKEN, {
    plugins: {
        // disable formatting. All format`` will be text without formatting
        format: false,
    },
});

API options

fetchOptions

Configure fetch parameters

ts
const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        fetchOptions: {
            headers: {
                "X-Hi-Telegram": "10",
            },
        },
    },
});

baseURL

URL which will be used to send requests to. "https://api.telegram.org/bot" by default.

ts
const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        // random domain
        baseURL: "https://telegram.io/bot",
    },
});

useTest

Should we send requests to test data center? false by default. The test environment is completely separate from the main environment, so you will need to create a new user account and a new bot with @BotFather.

Documentation

ts
const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        useTest: true,
    },
});

retryGetUpdatesWait

Time in milliseconds before calling getUpdates again. 1000 by default.

ts
const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        retryGetUpdatesWait: 300,
    },
});

Proxy support

In GramIO, it is quite simple to set up a proxy for requests.

Node.js

ts
import { ProxyAgent } from "undici";

const proxyAgent = new ProxyAgent("my.proxy.server");

const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        fetchOptions: {
            dispatcher: proxyAgent,
        },
    },
});

WARNING

Despite the fact that undici works under the hood of Node.js, you'll have to install it. Also make sure you don't have "lib": ["DOM"] in your tsconfig.json, otherwise you won't see the dispatcher property in the types (although undici will process it anyway).

Documentation

Bun

ts
const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        fetchOptions: {
            proxy: "https://username:password@proxy.example.com:8080",
        },
    },
});

Guide

Deno

ts
const client = Deno.createHttpClient({
    proxy: { url: "http://host:port/" },
});

const bot = new Bot(process.env.BOT_TOKEN, {
    api: {
        fetchOptions: {
            client,
        },
    },
});

WARNING

This API is unstable, so you should run it with deno run index.ts --unstable

Documentation | Deno.proxy | HTTP_PROXY environment variables