vvlad1973-telegram-framework
TypeScript icon, indicating that this package has built-in type declarations

7.9.7 • Public • Published

Telegram Bot API implementation

Current version: 7.9.5

This package partially implements interaction with Telegram as a bot.

Supported version of Telegram Bot API - 5.6

Description

This package provides:

  • data exchange with Telegram in webhook mode
  • work in asynchronous mode
  • control of the load on the Telegram Bot API
  • prioritization of method calls of Telegram Bot API depending on the type of call
  • control of size of the send queue
  • preprocessing of received data

Installation

npm install vvlad1973-telegram-framework

Usage

import TelegramBot from 'vvlad1973-telegram-framework';

/** 
 * Express or any other HTTP/HTTPS service can be used to process 
 * webhook requests
 */
import express from 'express';
import bodyParser from 'body-parser';

let
  chatId = 90844863,
  text = 'Test message',
  token = '2039667923:AAHwlugV92qBNXrn_XrTGaQjFPuCJAtUnfs',
  webhookUrl = 'https://some.url.for.webhook/',
  response;

/** 
 * Creating an instance of TelegramBot
 */
const bot = new TelegramBot(token);

/** 
 * Creating instance of HTTP-server and starting listening
 */
const app = express();
app.use(bodyParser.json());
app.listen(port);

/** 
 * Subscription on POST-queries to HTTP-server
 */
app.post(`/${token}/`, function (request, response) {
    response.sendStatus(200); // Sending response on POST-query ...
    bot.processUpdate(request.body); // ... and start processing received data
});

/** 
 * Subscription on incoming updates from Telegram
 */
bot.on('*', function (userId, chatId, senderData, properties, contents) {
  console.log(`[userId=${userId}] Received update:`);
  console.log(`\t chat=[${JSON.stringify(chatId, 0, '\t')}]`);
  console.log(`\t senderData=[${JSON.stringify(senderData, 0, '\t')}]`);
  console.log(`\t properties=[${JSON.stringify(properties, 0, '\t')}]`);
  console.log(`\t contents=[${JSON.stringify(contents, 0, '\t')}]`);
  
  /**
   * Sending response message
   */
  bot.sendMessage(chatId, 'OK')
    .then(response => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    });
});

/**
 * Getting info about bot from Telegram
 */
response = await bot.getMe();
console.log(response);

/**
 * Setting webhook
 */
response = await bot.setWebhook(webhookUrl);
console.log(response);

/**
 * Getting webhook info from Telegram
 */
response = await bot.getWebhookInfo();
console.log(response);

/**
 * Sending a message to Telegram chat
 */
response = await bot.sendMessage(chatId, text);
console.log(response);

Implemented methods

Bot control methods

  • getMe
  • getMyCommands
  • setMyCommands
  • deleteMyCommands

Webhook control methods

  • getWebhookInfo
  • setWebhook
  • deleteWebhook

Sending data methods

  • sendMessage
  • sendPhoto
  • sendVideo
  • sendVideoNote
  • sendAudio
  • sendVoice
  • sendDocument
  • sendAnimation
  • forwardMessage
  • copyMessage
  • answerCallbackQuery
  • sendChatAction
  • sendSticker
  • sendDice
  • sendPoll
  • sendMediaGroup
  • sendContact

Modifying messages

  • editMessageText
  • editMessageCaption
  • editMessageReplyMarkup
  • editMessageMedia
  • stopPoll

Control messages in chat

  • deleteMessage
  • pinChatMessage
  • unpinChatMessage
  • unpinAllChatMessages

Chat manageement

  • getChat
  • getChatMember
  • getChatMemberCount
  • getChatAdministrators
  • banChatMember
  • unbanChatMember
  • restrictChatMember
  • promoteChatMember
  • approveChatJoinRequest
  • declineChatJoinRequest

Getting info

  • getFile

Package Sidebar

Install

npm i vvlad1973-telegram-framework

Weekly Downloads

8

Version

7.9.7

License

MIT

Unpacked Size

2.83 MB

Total Files

86

Last publish

Collaborators

  • vvlad1973