Skip to content
Tutorials Center
My learning

Topics

Discord and Telegram Bots Tutorial

Learn Discord and Telegram Bots step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Are Chat Bots? Learn what chat bots are, how Discord and Telegram bots receive events and send replies, what they are used for, and what this course can and cannot run in your browser.
  2. 2 Discord and Telegram Compared Compare Discord and Telegram bots side by side - accounts, event delivery, commands, message limits, rich interfaces and permissions - so you can pick the right platform for your project.
  3. 3 Bot Tokens and Project Setup Create Discord and Telegram bot accounts, understand bot tokens, keep secrets out of source code with environment variables, and structure a Node.js bot project that is easy to test.
  4. 4 Your First Telegram Bot Build a Telegram bot from scratch in Node.js using only fetch - call getMe, read updates with long polling, reply with sendMessage and confirm updates with offsets - against a runnable stand-in server.
  5. 5 Your First Discord Bot Build your first Discord bot concept step by step - authenticate with the Bot header, call the REST API to read the bot user and send a message, and see how the gateway delivers events to a client.

Bot Concepts and APIs

  1. 6 HTTP and JSON for Bot APIs Learn the HTTP skills every bot needs - methods, headers, JSON bodies, status codes, timeouts with AbortSignal, 429 rate-limit handling and retries with backoff - with runnable fetch examples.
  2. 7 Telegram Updates and Methods Understand the Telegram Bot API data model - Update, Message, Chat and User objects, bot_command entities, allowed_updates - and the core methods for sending, editing, deleting and answering.
  3. 8 Discord REST API and Snowflakes Learn how the Discord REST API identifies things with snowflake ids, encodes permissions as bitfields, and limits request rates with buckets and headers, with runnable BigInt and rate-limit examples.
  4. 9 Discord Gateway and Intents Learn how the Discord gateway works - the websocket handshake, opcodes, heartbeats, sequence numbers, resuming, and intents as bit flags - with a runnable simulated gateway client in Node.js.

Commands and Events

  1. 10 Parsing Commands Write a robust command parser for chat bots - prefixes and mentions, quoted arguments, flags, typed and validated parameters, aliases and generated help - with runnable Node.js examples.
  2. 11 Discord Slash Commands Learn Discord application commands - defining names, options and subcommands, registering them over REST, handling interaction payloads, and replying with immediate or deferred responses - with runnable Node.js code.
  3. 12 Telegram Handlers and Routing Build a small update router for Telegram bots - command, text, regex and callback handlers, a context object with reply helpers, chat-type filters and fake-API tests - and see the same ideas in grammY.
  4. 13 Middleware and Error Handling Learn the middleware pattern for chat bots - an onion-style compose function, logging, timing, per-user throttling, deduplication, error boundaries, and safe handling of unhandled rejections - with runnable examples.

Messages and Embeds

  1. 14 Formatting Text and Escaping Format Telegram and Discord messages safely - HTML and MarkdownV2 parse modes, escaping user text, Discord markdown, mentions, timestamps and allowed_mentions - with runnable helper functions.
  2. 15 Discord Embeds Learn to build Discord embeds - title, description, fields, author, footer, images, colors and timestamps - with the official length limits enforced by a runnable embed builder and a paginated help example.
  3. 16 Buttons and Inline Keyboards Add interactive buttons to Telegram and Discord bots - inline keyboards, callback_data, action rows, button styles, custom_id design, select menus and updating messages - with runnable builders and a vote example.
  4. 17 Files and Uploads Send and receive files with Telegram and Discord bots - multipart/form-data with FormData and Blob, file_id reuse, getFile downloads, attachment:// references in embeds, size limits and safe filename handling.
  5. 18 Message Length and Rate Limits Split long bot messages at safe boundaries for Telegram (4096) and Discord (2000), keep code fences intact, and build a rate-limited send queue with per-chat and global limits for broadcasts.

State and Storage

  1. 19 Keeping Bot State in Memory Learn how Discord and Telegram bots keep state in memory with Maps, counters and TTL caches, and why memory state vanishes on restart and must be bounded.
  2. 20 SQLite Storage for Bots Store warnings, settings and balances for a Discord or Telegram bot in SQLite with node:sqlite: schema, upserts, transactions, migrations and backups.
  3. 21 Per-User and Per-Guild Settings Build a settings service for Discord and Telegram bots: defaults, validation, SQLite persistence, a write-through cache and a /settings command handler.
  4. 22 Conversation State and Multi-Step Dialogs Build multi-step dialogs for Telegram and Discord bots with a finite state machine: collect answers, validate each step, allow cancel, and expire stale chats.

Webhooks and Hosting

  1. 23 Long Polling vs Webhooks Understand how bots receive events: Telegram getUpdates long polling with offsets, webhooks, and the Discord gateway versus interactions endpoint, with runnable demos.
  2. 24 A Telegram Webhook Server Write a secure Telegram webhook receiver in Node.js: secret token header, body size limit, fast acknowledgment, a work queue, retries and replying via the Bot API.
  3. 25 Discord Interactions Endpoint and Ed25519 Signatures Build a Discord interactions HTTP endpoint in Node.js: verify Ed25519 request signatures with node:crypto, answer PING, respond to slash commands and defer slow work.
  4. 26 Hosting and Process Management for Bots Run a bot reliably: environment config validation, health checks, graceful shutdown on SIGTERM, restart policies, systemd and Docker, and where each bot type can be hosted.

Moderation and Security

  1. 27 Token and Secret Safety Protect Discord and Telegram bot tokens: keep them out of Git, load from the environment, redact logs, scan for leaks, rotate after exposure, and use least privilege.
  2. 28 Rate Limits, Retries and Backoff Handle Discord and Telegram rate limits correctly: read 429 responses and retry_after, add exponential backoff with jitter, and queue outgoing calls with a token bucket.
  3. 29 Moderation Commands and Permissions Build safe moderation commands for Discord and Telegram bots: permission bitfields with BigInt, role hierarchy, who may act on whom, warning escalation and audit logs.
  4. 30 Anti-Spam, AutoMod and Input Validation Detect floods, duplicates, mention spam and unwanted links in chat bots, normalize tricky Unicode, validate command arguments and escape output for Telegram and Discord.

Patterns and Deployment

  1. 31 Command Handler Architecture and Middleware Structure a Discord and Telegram bot so commands share one core: a registry, a context object, middleware for cooldowns and permissions, and thin platform adapters.
  2. 32 Testing Bots with Fakes Test chat bots without a network: a recording fake API, injected clocks and random sources, update fixtures, testing webhook handlers and a node:test layout for your project.
  3. 33 Logging, Monitoring and Deployment Operate a Discord or Telegram bot in production: structured JSON logs, counters and a metrics endpoint, throttled error alerts, a CI deploy pipeline and a go-live checklist.

Projects and Practice

  1. 34 Project: A Reminder Bot Build a complete reminder bot core in Node.js: parse durations, store reminders in SQLite, run a scheduler that survives restarts, list and cancel reminders, with tests.
  2. 35 Project: A Poll Bot with Buttons Build a poll bot for Telegram and Discord: create polls, handle button callbacks and custom_id values, allow one changeable vote per user, draw result bars and close polls.
  3. 36 Project: A Moderation Bot Assemble a moderation bot core: join verification with a timeout, a spam pipeline, an escalating warning ladder, permission checks and an audit log, all testable with a fake clock.
  4. 37 Practice Exercises: Bot Problems with Solutions Six hands-on bot exercises with runnable solutions: quoted command arguments, leaderboards in SQL, idempotent updates, pagination, HMAC webhooks and splitting long messages.