@gramio/types
TypeScript icon, indicating that this package has built-in type declarations

8.1.0 • Public • Published

Code-generated and Auto-published Telegram Bot API types

Bot API npm JSR JSR Score

Versioning

8.0.x types are for 8.0 Telegram Bot API

Usage as an NPM package

import type { APIMethods, APIMethodReturn } from "@gramio/types";

type SendMessageReturn = Awaited<ReturnType<APIMethods["sendMessage"]>>;
//   ^? type SendMessageReturn = TelegramMessage

type GetMeReturn = APIMethodReturn<"getMe">;
//   ^? type GetMeReturn = TelegramUser

Please see API Types References

Auto-update package

This library is updated automatically to the latest version of the Telegram Bot API in case of changes thanks to CI CD! If the github action failed, there are no changes in the Bot API

Imports (after @gramio/)

  • index - exports everything in the section
  • methods - exports APIMethods which describes the api functions
  • objects - exports objects with the Telegram prefix (for example Update)
  • params - exports params that are used in methods with Params postfix

Write you own type-safe Telegram Bot API wrapper

import type {
    APIMethods,
    APIMethodParams,
    TelegramAPIResponse,
} from "@gramio/types";

const TBA_BASE_URL = "https://api.telegram.org/bot";
const TOKEN = "";

const api = new Proxy({} as APIMethods, {
    get:
        <T extends keyof APIMethods>(_target: APIMethods, method: T) =>
        async (params: APIMethodParams<T>) => {
            const response = await fetch(`${TBA_BASE_URL}${TOKEN}/${method}`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(params),
            });

            const data = (await response.json()) as TelegramAPIResponse;
            if (!data.ok) throw new Error(`Some error occurred in ${method}`);

            return data.result;
        },
});

api.sendMessage({
    chat_id: 1,
    text: "message",
});

Usage with @gramio/keyboards

import { Keyboard } from "@gramio/keyboards";

// the code from the example above

api.sendMessage({
    chat_id: 1,
    text: "message with keyboard",
    reply_markup: new Keyboard().text("button text"),
});

With File uploading support

Documentation

Generate types manually

Prerequire - rust

  1. Clone this repo and open it
git clone https://github.com/gramiojs/types.git
  1. Clone repo with Telegram Bot API schema generator
git clone https://github.com/ark0f/tg-bot-api.git
  1. Run the JSON schema generator in the cloned folder
cd tg-bot-api && cargo run --package gh-pages-generator --bin gh-pages-generator -- dev && cd ..
  1. Run types code-generation from the root of the project
bun generate

or, if you don't use bun, use tsx

npx tsx src/index.ts
  1. Profit! Check out the types of Telegram Bot API in out folder!

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
8.1.0300latest

Version History

VersionDownloads (Last 7 Days)Published
8.1.0300
8.0.621
8.0.50
8.0.40
8.0.34
8.0.23
8.0.11
7.11.0109
7.10.25
7.10.114
7.10.02
7.9.10
7.9.01
7.8.01
7.7.30
7.7.21
7.7.10
7.7.01
7.6.00
7.5.00
7.4.30
7.4.20
7.4.11
7.4.00
7.3.41
7.3.30
7.3.20
7.3.10
7.3.00
7.2.21
7.2.10
7.2.00
7.1.70
7.1.60
7.1.50
7.1.40
7.1.30
7.1.20
7.1.10
7.1.00
7.0.170
7.0.160
7.0.150
7.0.141
7.0.132
7.0.122
7.0.111
7.0.102
7.0.90
7.0.80
7.0.70
7.0.60
7.0.50
7.0.41
7.0.30
7.0.20
7.0.10
7.0.0-rc0

Package Sidebar

Install

npm i @gramio/types

Weekly Downloads

382

Version

8.1.0

License

MIT

Unpacked Size

474 kB

Total Files

8

Last publish

Collaborators

  • skravets