CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-node-telegram-bot-api

Node.js wrapper for the official Telegram Bot API with polling and webhook support, comprehensive message handling, media operations, and bot management features.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

bot-settings.mddocs/

Bot Information and Settings

Bot configuration and information management including commands, descriptions, menu buttons, and administrator rights.

Capabilities

Bot Information

Methods for retrieving and managing basic bot information.

/**
 * Get bot information
 * @param {object} [options] - Additional options
 * @returns {Promise<User>}
 */
getMe(options): Promise<User>;

/**
 * Log out bot from cloud API
 * @param {object} [options] - Additional options
 * @returns {Promise<boolean>}
 */
logOut(options): Promise<boolean>;

/**
 * Close bot instance and free resources
 * @param {object} [options] - Additional options
 * @returns {Promise<boolean>}
 */
close(options): Promise<boolean>;

Usage Example:

// Get bot information
const botInfo = await bot.getMe();
console.log('Bot username:', botInfo.username);
console.log('Bot first name:', botInfo.first_name);
console.log('Bot ID:', botInfo.id);
console.log('Can join groups:', botInfo.can_join_groups);
console.log('Can read all group messages:', botInfo.can_read_all_group_messages);
console.log('Supports inline queries:', botInfo.supports_inline_queries);

// Log out from cloud API (for local Bot API server)
const loggedOut = await bot.logOut();
if (loggedOut) {
  console.log('Bot logged out successfully');
}

// Close bot instance
const closed = await bot.close();
if (closed) {
  console.log('Bot closed successfully');
}

Bot Commands

Methods for managing the bot's command menu and help interface.

/**
 * Set bot's command list
 * @param {object[]} commands - Array of bot commands
 * @param {object} [options] - Command scope and language options
 * @returns {Promise<boolean>}
 */
setMyCommands(commands, options): Promise<boolean>;

/**
 * Delete bot's commands
 * @param {object} [options] - Scope and language options
 * @returns {Promise<boolean>}
 */
deleteMyCommands(options): Promise<boolean>;

/**
 * Get bot's current commands
 * @param {object} [options] - Scope and language options
 * @returns {Promise<BotCommand[]>}
 */
getMyCommands(options): Promise<BotCommand[]>;

Usage Example:

// Set general commands for all users
await bot.setMyCommands([
  { command: 'start', description: 'Start the bot' },
  { command: 'help', description: 'Get help information' },
  { command: 'settings', description: 'Configure bot settings' },
  { command: 'about', description: 'About this bot' }
]);

// Set commands for specific scope (group administrators)
await bot.setMyCommands([
  { command: 'ban', description: 'Ban a user' },
  { command: 'unban', description: 'Unban a user' },
  { command: 'promote', description: 'Promote user to admin' },
  { command: 'demote', description: 'Remove admin privileges' }
], {
  scope: {
    type: 'all_chat_administrators'
  }
});

// Set commands for specific language
await bot.setMyCommands([
  { command: 'start', description: 'Iniciar el bot' },
  { command: 'ayuda', description: 'Obtener información de ayuda' },
  { command: 'configuracion', description: 'Configurar el bot' }
], {
  language_code: 'es'
});

// Set commands for specific chat
await bot.setMyCommands([
  { command: 'stats', description: 'Show chat statistics' },
  { command: 'backup', description: 'Backup chat data' }
], {
  scope: {
    type: 'chat',
    chat_id: chatId
  }
});

// Get current commands
const commands = await bot.getMyCommands();
console.log('Current commands:');
commands.forEach(cmd => {
  console.log(`/${cmd.command} - ${cmd.description}`);
});

// Get commands for specific scope
const adminCommands = await bot.getMyCommands({
  scope: {
    type: 'all_chat_administrators'
  }
});

// Delete all commands
await bot.deleteMyCommands();

// Delete commands for specific scope
await bot.deleteMyCommands({
  scope: {
    type: 'all_private_chats'
  }
});

Bot Profile

Methods for managing bot's profile information like name and description.

/**
 * Set bot's name
 * @param {object} [options] - Name options
 * @returns {Promise<boolean>}
 */
setMyName(options): Promise<boolean>;

/**
 * Get bot's name
 * @param {object} [options] - Language options
 * @returns {Promise<BotName>}
 */
getMyName(options): Promise<BotName>;

/**
 * Set bot's description (shown in chat info)
 * @param {object} [options] - Description options
 * @returns {Promise<boolean>}
 */
setMyDescription(options): Promise<boolean>;

/**
 * Get bot's description
 * @param {object} [options] - Language options
 * @returns {Promise<BotDescription>}
 */
getMyDescription(options): Promise<BotDescription>;

/**
 * Set bot's short description (shown in search results)
 * @param {object} [options] - Short description options
 * @returns {Promise<boolean>}
 */
setMyShortDescription(options): Promise<boolean>;

/**
 * Get bot's short description
 * @param {object} [options] - Language options
 * @returns {Promise<BotShortDescription>}
 */
getMyShortDescription(options): Promise<BotShortDescription>;

Usage Example:

// Set bot name
await bot.setMyName({
  name: 'My Awesome Bot'
});

// Set bot name for specific language
await bot.setMyName({
  name: 'Mi Bot Increíble',
  language_code: 'es'
});

// Get bot name
const botName = await bot.getMyName();
console.log('Bot name:', botName.name);

// Set bot description
await bot.setMyDescription({
  description: 'This is an awesome bot that helps you manage your daily tasks and provides useful information. It can handle various commands and interact with multiple services.'
});

// Set description for specific language
await bot.setMyDescription({
  description: 'Este es un bot increíble que te ayuda a gestionar tus tareas diarias y proporciona información útil.',
  language_code: 'es'
});

// Get bot description
const botDescription = await bot.getMyDescription();
console.log('Bot description:', botDescription.description);

// Set short description (for search results)
await bot.setMyShortDescription({
  short_description: 'Task management and utility bot'
});

// Set short description for specific language
await bot.setMyShortDescription({
  short_description: 'Bot de gestión de tareas y utilidades',
  language_code: 'es'
});

// Get short description
const shortDesc = await bot.getMyShortDescription();
console.log('Short description:', shortDesc.short_description);

Chat Menu Button

Methods for configuring the menu button in private chats.

/**
 * Set chat menu button
 * @param {object} [options] - Menu button options
 * @returns {Promise<boolean>}
 */
setChatMenuButton(options): Promise<boolean>;

/**
 * Get chat menu button
 * @param {object} [options] - Chat options
 * @returns {Promise<MenuButton>}
 */
getChatMenuButton(options): Promise<MenuButton>;

Usage Example:

// Set default menu button (shows "Menu")
await bot.setChatMenuButton({
  menu_button: {
    type: 'default'
  }
});

// Set commands menu button
await bot.setChatMenuButton({
  menu_button: {
    type: 'commands'
  }
});

// Set web app menu button
await bot.setChatMenuButton({
  menu_button: {
    type: 'web_app',
    text: 'Open Dashboard',
    web_app: {
      url: 'https://mybot.example.com/dashboard'
    }
  }
});

// Set menu button for specific chat
await bot.setChatMenuButton({
  chat_id: chatId,
  menu_button: {
    type: 'web_app',
    text: 'Chat Settings',
    web_app: {
      url: `https://mybot.example.com/chat/${chatId}/settings`
    }
  }
});

// Get current menu button
const menuButton = await bot.getChatMenuButton();
console.log('Menu button type:', menuButton.type);

if (menuButton.type === 'web_app') {
  console.log('Web app text:', menuButton.text);
  console.log('Web app URL:', menuButton.web_app.url);
}

// Get menu button for specific chat
const chatMenuButton = await bot.getChatMenuButton({
  chat_id: chatId
});

Administrator Rights

Methods for managing bot's default administrator rights.

/**
 * Set default administrator rights for the bot
 * @param {object} [options] - Administrator rights options
 * @returns {Promise<boolean>}
 */
setMyDefaultAdministratorRights(options): Promise<boolean>;

/**
 * Get default administrator rights
 * @param {object} [options] - Rights scope options
 * @returns {Promise<ChatAdministratorRights>}
 */
getMyDefaultAdministratorRights(options): Promise<ChatAdministratorRights>;

Usage Example:

// Set default admin rights for groups
await bot.setMyDefaultAdministratorRights({
  rights: {
    is_anonymous: false,
    can_manage_chat: true,
    can_delete_messages: true,
    can_manage_video_chats: true,
    can_restrict_members: true,
    can_promote_members: false,
    can_change_info: false,
    can_invite_users: true,
    can_pin_messages: true,
    can_manage_topics: true
  },
  for_channels: false
});

// Set different rights for channels
await bot.setMyDefaultAdministratorRights({
  rights: {
    is_anonymous: false,
    can_manage_chat: true,
    can_delete_messages: false,
    can_manage_video_chats: false,
    can_restrict_members: false,
    can_promote_members: false,
    can_change_info: false,
    can_invite_users: false,
    can_post_messages: true,
    can_edit_messages: true,
    can_pin_messages: true,
    can_manage_topics: false
  },
  for_channels: true
});

// Get default admin rights for groups
const groupRights = await bot.getMyDefaultAdministratorRights({
  for_channels: false
});

console.log('Default group admin rights:');
console.log('Can manage chat:', groupRights.can_manage_chat);
console.log('Can delete messages:', groupRights.can_delete_messages);
console.log('Can restrict members:', groupRights.can_restrict_members);

// Get default admin rights for channels
const channelRights = await bot.getMyDefaultAdministratorRights({
  for_channels: true
});

console.log('Default channel admin rights:');
console.log('Can post messages:', channelRights.can_post_messages);
console.log('Can edit messages:', channelRights.can_edit_messages);

Configuration Management

Example patterns for managing bot configuration and settings.

Usage Example:

// Complete bot setup function
async function setupBot() {
  // Get bot info
  const botInfo = await bot.getMe();
  console.log(`Setting up bot: ${botInfo.first_name} (@${botInfo.username})`);
  
  // Set commands
  await bot.setMyCommands([
    { command: 'start', description: 'Start the bot' },
    { command: 'help', description: 'Show help information' },
    { command: 'settings', description: 'Bot settings' },
    { command: 'about', description: 'About this bot' }
  ]);
  
  // Set bot name and descriptions
  await bot.setMyName({ name: 'Task Manager Bot' });
  await bot.setMyDescription({
    description: 'A comprehensive task management bot that helps you organize your daily activities, set reminders, and track your productivity.'
  });
  await bot.setMyShortDescription({
    short_description: 'Task management and productivity bot'
  });
  
  // Set default admin rights
  await bot.setMyDefaultAdministratorRights({
    rights: {
      can_manage_chat: true,
      can_delete_messages: true,
      can_restrict_members: true,
      can_invite_users: true,
      can_pin_messages: true
    }
  });
  
  // Set menu button
  await bot.setChatMenuButton({
    menu_button: {
      type: 'web_app',
      text: 'Open Dashboard',
      web_app: { url: 'https://taskbot.example.com/dashboard' }
    }
  });
  
  console.log('Bot setup completed successfully!');
}

// Configuration validation
async function validateBotConfig() {
  try {
    const [commands, botName, description, shortDesc, adminRights, menuButton] = await Promise.all([
      bot.getMyCommands(),
      bot.getMyName(),
      bot.getMyDescription(),
      bot.getMyShortDescription(),
      bot.getMyDefaultAdministratorRights(),
      bot.getChatMenuButton()
    ]);
    
    console.log('✅ Bot configuration validation:');
    console.log(`Commands: ${commands.length} configured`);
    console.log(`Name: ${botName.name || 'Not set'}`);
    console.log(`Description: ${description.description ? 'Set' : 'Not set'}`);
    console.log(`Short description: ${shortDesc.short_description ? 'Set' : 'Not set'}`);
    console.log(`Admin rights: ${Object.keys(adminRights).length} permissions configured`);
    console.log(`Menu button: ${menuButton.type}`);
    
    return true;
  } catch (error) {
    console.error('❌ Bot configuration validation failed:', error.message);
    return false;
  }
}

// Initialize bot with full configuration
setupBot().then(() => {
  console.log('Bot is ready!');
  validateBotConfig();
});

Types

interface User {
  id: number;
  is_bot: boolean;
  first_name: string;
  last_name?: string;
  username?: string;
  language_code?: string;
  is_premium?: boolean;
  added_to_attachment_menu?: boolean;
  can_join_groups?: boolean;
  can_read_all_group_messages?: boolean;
  supports_inline_queries?: boolean;
}

interface BotCommand {
  command: string;
  description: string;
}

interface BotCommandScope {
  type: 'default' | 'all_private_chats' | 'all_group_chats' | 'all_chat_administrators' | 'chat' | 'chat_administrators' | 'chat_member';
  chat_id?: number | string;
  user_id?: number;
}

interface BotName {
  name: string;
}

interface BotDescription {
  description: string;
}

interface BotShortDescription {
  short_description: string;
}

interface MenuButton {
  type: 'commands' | 'web_app' | 'default';
}

interface MenuButtonWebApp extends MenuButton {
  type: 'web_app';
  text: string;
  web_app: WebApp;
}

interface WebApp {
  url: string;
}

interface ChatAdministratorRights {
  is_anonymous: boolean;
  can_manage_chat: boolean;
  can_delete_messages: boolean;
  can_manage_video_chats: boolean;
  can_restrict_members: boolean;
  can_promote_members: boolean;
  can_change_info: boolean;
  can_invite_users: boolean;
  can_post_messages?: boolean;
  can_edit_messages?: boolean;
  can_pin_messages: boolean;
  can_post_stories?: boolean;
  can_edit_stories?: boolean;
  can_delete_stories?: boolean;
  can_manage_topics: boolean;
}

interface SetMyCommandsOptions {
  scope?: BotCommandScope;
  language_code?: string;
}

interface SetMyNameOptions {
  name?: string;
  language_code?: string;
}

interface SetMyDescriptionOptions {
  description?: string;
  language_code?: string;
}

interface SetMyShortDescriptionOptions {
  short_description?: string;
  language_code?: string;
}

interface SetChatMenuButtonOptions {
  chat_id?: number | string;
  menu_button?: MenuButton;
}

interface SetMyDefaultAdministratorRightsOptions {
  rights?: ChatAdministratorRights;
  for_channels?: boolean;
}

docs

advanced-features.md

bot-settings.md

bot-setup.md

chat-management.md

index.md

interactive-features.md

media-files.md

message-handling.md

messaging.md

tile.json