Ritu Raj’s Blog From Ideas to Execution

Fetch Telegram Channel/Group Data via Bot API in JavaScript

Are you building a Telegram bot and need to fetch channel or group details programmatically? In this practical guide, you’ll learn exactly how to use the Telegram Bot API with JavaScript to retrieve essential information about Telegram groups and channels. This is crucial for bot developers managing communities or automating moderation tasks.

Why Retrieve Telegram Channel/Group Info?

Telegram bots can automate community management, analytics tracking, or content moderation. By fetching details like member counts, descriptions, or permissions, you can:

  • Monitor group growth
  • Validate user access
  • Build custom dashboards

Let’s dive into the implementation!

Prerequisites

  1. Basic JavaScript/Node.js knowledge
  2. Telegram bot token (create via @BotFather)
  3. Node.js installed on your system (v14+ recommended)

Step 1: Set Up Your Project

# Create a new directory
mkdir telegram-bot-info
cd telegram-bot-info

# Initialize a new Node.js project
npm init -y

# Install Dependencies
npm install node-telegram-bot-api

Step 2: Fetching Chat Information

Understanding Chat IDs

Telegram groups/channels have unique negative IDs (e.g., -100123456789). Your bot must be a member of the target chat to access information.

⚠️ NOTE: Never expose your token publicly! Use environment variables.

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

const CHAT_ID = -100123456789; // Replace with your target chat ID

async function getChatInfo() {
  try {
    const chat = await bot.getChat(CHAT_ID);
    console.log('Chat Details:', chat);
  } catch (error) {
    console.error('Error fetching chat:', error.message);
  }
}

getChatInfo();

Key Points:

  • getChat method returns Chat object
  • Use -100 prefix for public channels (e.g., -100123456789)
  • Handle errors for missing permissions/invalid ID

Key Response Fields

{
  "id": -100123456789,
  "title": "Developer Community",
  "type": "supergroup",
  "description": "Official programming discussion group",
  "members_count": 2450
}

Step 3: Handling Common Use Cases

Real-World Example: Check Group Membership

async function isUserMember(userId) {
  const member = await bot.getChatMember(CHAT_ID, userId);
  return ['member', 'administrator', 'creator'].includes(member.status);
}

Troubleshooting Tips

  1. Authorization Error: Ensure your bot is added as admin in channels
  2. Invalid Chat ID: Use @RawDataBot to get correct chat IDs
  3. Rate Limits: Add 1-second delays between API calls

Why This Matters for Bot Developers

Retrieving chat information enables:

  • Automated moderation systems
  • Dynamic content delivery
  • User analytics tracking
  • Community engagement metrics

FAQ Section

Q: Can I get member lists using this method?
A: No, use getChatAdministrators for admins list only

Q: How to get a channel’s linked discussion group?
A: Check linked_chat_id in the response object

Next Steps

  1. Explore Telegram Bot API documentation
  2. Implement chat event listeners
  3. Build member analytics dashboards