Convert Figma logo to code with AI

yagop logonode-telegram-bot-api

Telegram Bot API for NodeJS

8,302
1,503
8,302
105

Top Related Projects

Modern Telegram Bot Framework for Node.js

2,172

The Telegram Bot Framework.

A powerful JavaScript library for interacting with the Discord API

Quick Overview

Node.js module to interact with the official Telegram Bot API. It provides a simple and intuitive interface for creating Telegram bots, allowing developers to easily send messages, handle commands, and interact with various Telegram features.

Pros

  • Easy to use and well-documented API
  • Supports both polling and webhook methods for receiving updates
  • Extensive coverage of Telegram Bot API features
  • Active community and regular updates

Cons

  • Limited built-in error handling, requiring manual implementation
  • Performance may be affected when handling a large number of concurrent requests
  • Dependency on external libraries may introduce potential vulnerabilities
  • Lack of built-in rate limiting features

Code Examples

  1. Creating a bot and sending a message:
const TelegramBot = require('node-telegram-bot-api');

const bot = new TelegramBot('YOUR_BOT_TOKEN', {polling: true});

bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Hello! I received your message.');
});
  1. Handling commands:
bot.onText(/\/start/, (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Welcome! How can I help you?');
});

bot.onText(/\/help/, (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Here are the available commands: /start, /help');
});
  1. Sending a photo:
bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  bot.sendPhoto(chatId, 'https://example.com/photo.jpg', {caption: 'Check out this photo!'});
});

Getting Started

  1. Install the package:

    npm install node-telegram-bot-api
    
  2. Create a new bot and get the token from BotFather on Telegram.

  3. Create a new JavaScript file (e.g., bot.js) and add the following code:

const TelegramBot = require('node-telegram-bot-api');

const token = 'YOUR_BOT_TOKEN';
const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'I received your message!');
});

console.log('Bot is running...');
  1. Run the bot:
    node bot.js
    

Competitor Comparisons

Modern Telegram Bot Framework for Node.js

Pros of Telegraf

  • More modern and actively maintained, with frequent updates and a larger community
  • Built-in support for middleware and scenes, making complex bot development easier
  • Better TypeScript support and type definitions

Cons of Telegraf

  • Steeper learning curve for beginners due to its more advanced features
  • May be overkill for simple bot projects
  • Some users report occasional breaking changes between versions

Code Comparison

node-telegram-bot-api:

const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
  bot.sendMessage(msg.chat.id, 'Hello!');
});

Telegraf:

const { Telegraf } = require('telegraf');
const bot = new Telegraf(token);

bot.on('message', (ctx) => {
  ctx.reply('Hello!');
});

bot.launch();

Both libraries allow for creating Telegram bots, but Telegraf offers a more modern and feature-rich approach. node-telegram-bot-api is simpler and may be easier for beginners, while Telegraf provides more advanced features and better support for complex bot development. The choice between the two depends on the project requirements and the developer's experience level.

2,172

The Telegram Bot Framework.

Pros of grammY

  • Built with TypeScript, offering better type safety and developer experience
  • More modern API design with better support for middleware and plugins
  • Actively maintained with frequent updates and improvements

Cons of grammY

  • Relatively newer library with a smaller community compared to node-telegram-bot-api
  • May have a steeper learning curve for developers familiar with older Telegram bot libraries

Code Comparison

node-telegram-bot-api:

const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
  bot.sendMessage(msg.chat.id, 'Hello!');
});

grammY:

import { Bot } from "grammy";
const bot = new Bot(token);

bot.on("message", (ctx) => {
  ctx.reply("Hello!");
});

bot.start();

Both libraries allow for easy creation of Telegram bots, but grammY's TypeScript support and more modern API design can lead to more maintainable and scalable code. node-telegram-bot-api has been around longer and has a larger user base, which can be beneficial for finding solutions to common problems. Ultimately, the choice between the two depends on the developer's preferences and project requirements.

A powerful JavaScript library for interacting with the Discord API

Pros of discord.js

  • More comprehensive and feature-rich API for Discord bot development
  • Better documentation and larger community support
  • Built-in support for voice channels and advanced Discord features

Cons of discord.js

  • Steeper learning curve due to more complex API
  • Larger package size and potentially higher resource usage
  • Limited to Discord platform, while node-telegram-bot-api is for Telegram

Code Comparison

discord.js:

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on('messageCreate', message => {
  if (message.content === '!ping') {
    message.reply('Pong!');
  }
});

node-telegram-bot-api:

const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
  if (msg.text === '/ping') {
    bot.sendMessage(msg.chat.id, 'Pong!');
  }
});

Both libraries provide easy-to-use APIs for creating bots, but discord.js offers more advanced features specific to Discord. node-telegram-bot-api is simpler and focused on Telegram functionality. The choice between them depends on the target platform and required features for your bot project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Node.js Telegram Bot API

Node.js module to interact with the official Telegram Bot API.

Bot API npm package Build Status Coverage Status

https://telegram.me/node_telegram_bot_api https://t.me/+_IC8j_b1wSFlZTVk https://telegram.me/Yago_Perez

📦 Install

npm i node-telegram-bot-api

✍️ Note: If you use Typescript you can install this package that contains type definitions for this library

npm install --save-dev @types/node-telegram-bot-api

🚀 Usage

const TelegramBot = require('node-telegram-bot-api');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});

// Matches "/echo [whatever]"
bot.onText(/\/echo (.+)/, (msg, match) => {
  // 'msg' is the received Message from Telegram
  // 'match' is the result of executing the regexp above on the text content
  // of the message

  const chatId = msg.chat.id;
  const resp = match[1]; // the captured "whatever"

  // send back the matched "whatever" to the chat
  bot.sendMessage(chatId, resp);
});

// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
  const chatId = msg.chat.id;

  // send a message to the chat acknowledging receipt of their message
  bot.sendMessage(chatId, 'Received your message');
});

📚 Documentation

Note: Development is done against the development branch. Code for the latest release resides on the master branch. Experimental features reside on the experimental branch.

💭 Community

We thank all the developers in the Open-Source community who continuously take their time and effort in advancing this project. See our list of contributors.

We have a Telegram channel where we post updates on the Project. Head over and subscribe!

We also have a Telegram group to discuss issues related to this library.

Some things built using this library that might interest you:

  • tgfancy: A fancy, higher-level wrapper for Telegram Bot API
  • node-telegram-bot-api-middleware: Middleware for node-telegram-bot-api
  • teleirc: A simple Telegram ↔ IRC gateway
  • bot-brother: Node.js library to help you easily create telegram bots
  • redbot: A Node-RED plugin to create telegram bots visually
  • node-telegram-keyboard-wrapper: A wrapper to improve keyboards structures creation through a more easy-to-see way (supports Inline Keyboards, Reply Keyboard, Remove Keyboard and Force Reply)
  • beetube-bot: A telegram bot for music, videos, movies, EDM tracks, torrent downloads, files and more.
  • telegram-inline-calendar: Date and time picker and inline calendar for Node.js telegram bots.
  • telegram-captcha: Telegram bot to protect Telegram groups from automatic bots.

👥 Contributors

License

The MIT License (MIT)

Copyright © 2019 Yago

NPM DownloadsLast 30 Days