Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

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.

A chat bot is a program that takes part in a chat application like a user does. It reads messages, reacts to commands and buttons, and sends messages back. On Discord and Telegram the bot is not a hack or a trick: both platforms give bots their own account type, their own login credential (a token) and an official HTTP API. If you can write a program that makes web requests, you can write a bot.

Bots do many jobs. A moderation bot deletes spam and bans raiders. A community bot hands out roles, runs polls and posts game stats. A notification bot tells you when a build fails or a price changes. A support bot answers common questions before a human steps in. In this course you will learn the building blocks behind all of them, using JavaScript on Node.js, and you will build several complete bots by the end.

Honesty note. Discord and Telegram are external services, and our in-browser runner has no network and no account of yours. Every runnable example in this course talks to a small stand-in server that runs on your own loopback address (127.0.0.1) inside the sandbox and imitates the real API's request and response shapes. Real library code (for example discord.js or grammY) needs npm packages and a real token, so it is shown read-only. Each stand-in is labelled. What you learn, such as payload shapes, limits and patterns, transfers directly to the real platforms.

The shape of every bot

All bots, on every platform, do the same loop:

  1. Receive an event (a message arrived, a user pressed a button, a member joined).
  2. Decide what to do, using your own logic and, often, some stored state.
  3. Act by calling the platform's API (send a message, edit one, ban a user).

Anatomy of a bot

Events in Messages, commands, clicks
Your logic Rules, state, decisions
Actions out Send, edit, delete, ban

Every bot is this loop, however fancy

1 / 3

The key difference from a normal website is who starts the conversation. A website waits for a browser to request a page. A bot lives in the middle of a conversation among people, so it has to be told about events as they happen. How it is told is the subject of the next chapters.

A bot as a pure function

The simplest way to think about a bot is as a function from "incoming message" to "outgoing reply". Separating that decision from the networking makes bots easy to test, and it is a habit this course keeps throughout.

Node.js A rule-based bot as a function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the last case: a good bot stays silent when a message is not meant for it. A bot that answers every message in a busy group becomes spam within minutes.

A bot as an event loop

Real platforms push many different kinds of events, not just text. A bot usually has one dispatcher that routes each event to a handler. Here is a miniature version with an event queue.

Node.js A tiny event dispatcher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Unknown event types must be ignored, never crash the bot. Platforms add new event types over time, and your bot should keep working when they do.

Discord and Telegram at a glance

Both platforms offer bot accounts, but they differ in culture and features.

Telegram Discord
Where bots live Private chats, groups, channels Servers (guilds), channels, DMs
Receive events Long polling or webhook Gateway (websocket) or HTTP interactions
Main API HTTPS Bot API REST API plus gateway
Commands /command text with a menu Slash commands registered with the platform
Rich UI Inline keyboards, web apps Embeds, buttons, select menus, modals
Make a bot Chat with @BotFather Developer Portal website

The next lesson compares them in detail, and later lessons teach each API on its own.

What bots may and may not do

Bots follow rules, and breaking them gets bots banned. A few that matter from day one:

  • A bot may only speak where it was invited or where a user started a chat with it. On Telegram a bot cannot message someone who has never contacted it.
  • Bots must respect rate limits (a limited number of calls per second).
  • Never collect personal data you do not need, and tell users what the bot stores.
  • Never put a token in public code. Anyone with the token controls the bot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three steps every bot repeats?

    A bot receives an event, runs its logic and then calls the platform API to act. Everything else is detail around that loop.
  2. 2. A bot receives an event type it has never heard of. What should it do?

    Platforms add event types over time. A robust dispatcher ignores unknown events instead of failing.
  3. 3. Why does this course run examples against a stand-in server?

    The sandbox has no internet and no account of yours, so the stand-in imitates the API shapes on loopback. The real APIs use the same shapes.
  4. 4. Which is a good habit for chat bots in busy groups?

    Staying silent for unrelated messages keeps the bot useful and avoids spam. Tokens must never be shared.

Summary

  • A bot is a program with its own platform account that reads events and calls the platform API.
  • Every bot loops: receive, decide, act. Keep the decision logic separate from the networking so you can test it.
  • Events arrive by polling, webhook or gateway depending on the platform.
  • Ignore unknown events, stay silent when a message is not for you, and protect your token.
  • In this course the runnable examples use labelled loopback stand-ins; real library code is read-only.

Next: Discord and Telegram Compared