New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

typescript-telegram-bot-api

Package Overview
Dependencies
Maintainers
0
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-telegram-bot-api

Telegram Bot API wrapper for Node.js written in TypeScript

  • 0.1.19
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
573
increased by5.52%
Maintainers
0
Weekly downloads
 
Created
Source

📦 typescript-telegram-bot-api

CI npm npm GitHub codecov

This is a TypeScript wrapper for the Telegram Bot API. It allows you to easily interact with the Telegram Bot API using TypeScript.

Installation

npm install typescript-telegram-bot-api

Usage

import { TelegramBot } from 'typescript-telegram-bot-api';

const bot = new TelegramBot({ botToken: 'YOUR_BOT_TOKEN' });
bot.startPolling();

bot.on('message', (message) => {
  console.log('Received message:', message.text);
});

bot.on('message:sticker', (message) => {
  console.log('Received sticker:', message.sticker.emoji);
});

api.getMe()
  .then(console.log)
  .catch(console.error);

Supported Methods

Descriptions of methods can be found at https://core.telegram.org/bots/api#available-methods. Method names and parameters correspond to those in the official API documentation. Each method returns a Promise that resolves with data received from the API.

In case of an API error, the Promise will be rejected with a TelegramError containing the error code and description from the API. If the API error includes a retry_after field, the library will retry the request after the specified number of seconds, until a response without an error is received. This behavior can be disabled by setting the autoRetry parameter to false.

If the error is not related to the API, the Promise will be rejected with a different error.

For sending files, you can use not only 'file_id' or 'url', but also stream.Readable or Buffer.

import {createReadStream} from 'fs';
import {readFile} from 'fs/promises';


await bot.sendPhoto({
  chat_id: chat_id,
  photo: 'AgACAgIAAxkDAAIF62Zq43...AgADcwADNQQ',
  caption: 'file_id',
});

// or

await bot.sendPhoto({
  chat_id: chat_id,
  photo: 'https://unsplash.it/640/480',
  caption: 'url',
});

// or

await bot.sendPhoto({
  chat_id: chat_id,
  photo: createReadStream('photo.jpg'),
  caption: 'stream',
});

// or 

await bot.sendPhoto({
  chat_id: chat_id,
  photo: await readFile('photo.jpg'),
  caption: 'buffer',
});

Events

TelegramBot is an EventEmitter that emits the Update event and also emits events for each type of Message, such as message:audio, when the audio field is present in the message object.

bot.on('message', (message) => {
  console.log('Received message:', message.text);
});

bot.on('message_reaction', (messageReactionUpdated) => {
  console.log('Received message_reaction:', messageReactionUpdated);
});

bot.on('message:audio', (message) => {
  console.log('Received audio:', message.audio.file_id);
});

Tests

npm test

CI/CD is set up with GitHub Actions. Tests and linters are run on every pull request.

If you want to run tests locally, follow the instructions in tests/README.md.

Keywords

FAQs

Package last updated on 02 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc