# api-telegram-bot

> client for telegram bots api

Latest version **7.0.0-rc1** (published 2025-01-25) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install api-telegram-bot
pnpm add api-telegram-bot
yarn add api-telegram-bot
bun add api-telegram-bot
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 7.0.0-rc1 |
| Published | 2025-01-25 |
| First published | 2017-01-11 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14.16 |
| Dependencies | 3 |
| Unpacked size | 352.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Felipe Bergamin <felipebergamin6@gmail.com> |
| Maintainers | felipebergamin |
| Keywords | node, telegram |

## Links

- npm: https://www.npmjs.com/package/api-telegram-bot
- Repository: https://github.com/felipebergamin/api-telegram-bot
- Homepage: https://github.com/felipebergamin/api-telegram-bot#readme
- Issues: https://github.com/felipebergamin/api-telegram-bot/issues
- npm.io page: https://npm.io/package/api-telegram-bot

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) ^6.3.3
- [axios](https://npm.io/package/axios.md) ^1.7.9
- [form-data](https://npm.io/package/form-data.md) ^4.0.1

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 7.0.0-rc1 (latest) — 2025-01-25
- 6.0.6 (beta) — 2019-07-30
- 6.0.5 — 2019-07-22
- 6.0.4 — 2019-07-16
- 6.0.3 — 2019-07-13
- 6.0.1 — 2019-05-12
- 6.0.0 — 2019-05-05
- 5.2.2 — 2019-04-09
- 5.2.1 — 2019-04-08
- 5.2.0 — 2019-04-08
- 5.1.0 — 2019-03-15
- 5.0.5 — 2019-03-06
- 5.0.4 — 2019-01-25
- 5.0.3 — 2018-10-25
- 5.0.2 — 2018-10-25
- … 15 more at https://npm.io/package/api-telegram-bot/versions

## README

# Telegram Bot API for Node.js

Node.js module for [Telegram Bot API](https://core.telegram.org/bots/api).

Talk with [@botfather](https://telegram.me/BotFather) on Telegram to create your bot.

# API reference

Please refer to the official Telegram Bot API documentation for details on the methods and types available.

[Click Here](https://core.telegram.org/bots/api)

### Examples

There's an [examples](https://github.com/felipebergamin/api-telegram-bot/tree/master/examples) directory on GitHub. Take a look ;)

Before run any example code, please install deps with `yarn` and build the library running `yarn build`.

### Install

```sh
npm install api-telegram-bot
```

### How to use

```js
const { Bot, Polling } = require('api-telegram-bot');

const TOKEN = 'BOT_TOKEN';
const bot = new Bot({ bot_token: TOKEN });
const polling = new Polling(bot);

// subscribe to all message types (texts, photos, videos, and others)
bot.messages().subscribe((data) => {
  // data is a object with 2 props:
  //   data.update - is the update received,
  //   data.actions - actions object with some helper functions
  // log to see what it is:
  console.log(data);
});

// subscribe only to text messages
bot.messages('text').subscribe(({ update, actions }) => {
  /*
   * `update` is the update object received from Telegram
   * `actions` is an object with some functions to manipulate received message:
   *    banChatMember: (until: number) => Promise
   *    deleteMessage: () => Promise
   *    reply: (text: string, optionals?) => Promise
   *
   * note: deleteMessage and banChatMember doesn't works on private chats
   */
  actions.reply(`You said: ${update.message?.text}`);

  setTimeout(() => {
    polling.stopPolling();
  }, 30000);
});

// NOTE: message actions are provided only for message updates (text, photo, ...)
bot.updates('edited_message').subscribe((data) => {
  // no actions here
  console.log(data);
});
```

## Using with webhook

See the `examples/webhook.js` file on GitHub [examples directory](https://github.com/felipebergamin/api-telegram-bot/tree/master/examples).

### Providing a custom http client

You can provide a custom http client factory to the bot constructor. The client must implements the following interface:

```ts
export type HttpClientArgs = {
  baseURL: string;
};

export interface HttpBotClient {
  post: <B = unknown, R = any>(
    method: string,
    // data is a plain object with the api method parameters (the second parameter passed to bot.call())
    data: B,
  ) => Promise<{ data: R }>;
}

const createHttpClient = ({ baseURL }: HttpClientArgs): HttpBotClient => {
  return {
    post(method, data) {
      // your code to make the request
      return fetch(...);
    },
  };
};

const bot = new Bot({
  bot_token: '',
  createHttpClient,
});
```

## Reply Markup Builders

Helpers to create reply markups for Telegram Bot API.

### [Reply Keyboard Markup](https://core.telegram.org/bots/api#replykeyboardmarkup)

```js
const { KeyboardBuilder } = require('api-telegram-bot');

const TOKEN = 'BOT_TOKEN';
const chat_id = 'CONTACT_ID';
const bot = new Bot({ bot_token: TOKEN });

const { keyboard } = KeyboardBuilder()
  .button({ text: "Yes" })
  .button({ text: "No" })
  .newRow()
  .button('Cancel');

bot.call('sendMessage', {
  chat_id,
  reply_markup: { keyboard, resize_keyboard: true },
  text: 'Confirm?'
});
```

![Reply Keyboard Builder Result](https://image.ibb.co/h2g9N6/Screenshot_20171215_102656.png)

## [Inline Keyboard](https://core.telegram.org/bots/api#inlinekeyboardmarkup)

```ts
const { KeyboardBuilder, Bot } = require('api-telegram-bot');

const TOKEN = 'BOT_TOKEN';
const chat_id = 'YOUR_TELEGRAM_ID';

const bot = new Bot({ bot_token: TOKEN });

const { keyboard: inline_keyboard } = KeyboardBuilder()
  .newRow()
  .button({ text: 'Yes', callback_data: 'YES' })
  .button({ text: 'No', callback_data: 'NO' })
  .newRow()
  .button({ text: 'Cancel', callback_data: 'CANCEL' });

bot.call('sendMessage', {
  chat_id,
  reply_markup: { inline_keyboard },
  text: 'Confirm?',
});
```

See the message sent by code above:

![Inline Keyboard Builder Result](https://image.ibb.co/kQOH9m/Screenshot_20171215_095919.png)

## Todo

- [ ] Add more unit tests
- [ ] Improve the web scrapper
- [ ] Refactor the Webhook and Polling classes
- [ ] Add more test cases to `scripts\test.ts`

---
_Source: https://npm.io/package/api-telegram-bot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
