@somethings/telegram-bot-api v4.7.0
Telegram Bot API
This is an accurate and comprehensive Telegram Bot API written in TypeScript. It provides a full up-to-date list of methods and types defined by the official documentation.
Using the API
import {Api} from "@somethings/telegram-bot-api";
const TOKEN = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11";
const api = new Api(TOKEN);
api.getMe().then((user) => {
console.log(user.id);
});This API exposes all officially documented methods as individual functions.
It also provides two mutually exclusive methods: listen and poll, which represent two officially supported ways of getting updates.
listen(port: number, url: string, getUpdate: UpdateGetter)
Starts a HTTP server listening for incoming Webhooks. Note that Telegram only supports communication over HTTPS, so you have to set up a third-party HTTPS capable web-proxy like nginx.
port: number- port to listen on;url: string- URL to match against incoming requests that will use the value specified during initialization viasetWebhook; all other requests will be ignored;getUpdate: UpdateGetter- callback for getting incoming updates.
poll(timeout: number, getUpdate: UpdateGetter)
Establishes an infinite loop of getUpdates long polling calls.
timeout: number- polling timeout;getUpdate: UpdateGetter- callback for getting incoming updates.
Sending files
There are some methods for sending binary files to the Telegram cloud like setWebhook and sendPhoto. This API provides a custom type representing such data - InputFile, which is different from the InputFile type defined in the official documentation. Refer to the code for more information please, it's pretty self-explanatory.
How to update the API
- Clone the repositpry and
npm installall the dependencies; - Run
npm run downloadto download a copy of the official documentation page and save it tosync/api.htmlfile; - Inspect the diff of
sync/api.htmlto determine if the API needs to be updated:- If there are only changes in existing types or methods just run
npm run parseto read documentation fromsync/api.htmlandsync/api.jsonand generateAbstractApi.tswith actual interfaces fields and descriptions; - If there are new types or methods (new H4 tags) or any changes affecting manually managed information update
sync/api.jsonaccordingly and runnpm run parse;
- If there are only changes in existing types or methods just run
- Inspect changes in
AbstractApi.ts; - Check
Api.tsfor possible TypeScript errors as it derives from generatedAbstractApi.tsand possibly add new methods here; - Commit everything.