Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

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.

Before a bot can do anything it needs an identity. On both platforms that identity is a token: a long secret string that proves a request comes from your bot. Anyone who holds the token can read what the bot reads and do what the bot does, so treating it like a password is the first lesson of bot development. This lesson shows how the accounts are created, how to keep tokens out of your code, and how to lay out a project that stays tidy as it grows.

Creating a Telegram bot

You do this inside Telegram itself, by chatting with the official bot @BotFather. These steps are read-only here because they need your Telegram account:

  1. Open a chat with @BotFather and send /newbot.
  2. Choose a display name (anything) and a username that ends in bot, for example weather_helper_bot.
  3. BotFather replies with the token, shaped like 123456789:AAH... (a numeric id, a colon, a secret part).
  4. Use /setcommands, /setdescription and /setprivacy to configure the bot later. If a token leaks, /revoke generates a new one.

Creating a Discord bot

Discord uses a website, the Developer Portal:

  1. Create a New Application and give it a name. This creates an application id.
  2. Open the Bot tab, add a bot user and press Reset Token to see the token once. Copy it into your secret storage immediately.
  3. On the same tab, switch on only the privileged intents you really need (Server Members, Message Content, Presence).
  4. Under OAuth2 / URL Generator choose the scopes bot and applications.commands, tick the permissions the bot needs, and open the generated link to invite the bot to a server you manage.

Give the bot the least permissions that make it work. A poll bot does not need Administrator.

Where tokens must never go

Never commit a token to Git, paste it in a chat, put it in front-end code or log it. Automated scanners scrape public repositories within seconds, and Discord and GitHub both revoke tokens they find. If a token is exposed, regenerate it at once.

The standard solution is the environment variable. The code reads process.env.BOT_TOKEN; the value lives outside the repository, in the shell, a hosting dashboard, or a .env file that is listed in .gitignore. Since Node 20.6 you can load a .env file without any library using node --env-file=.env bot.js, and process.loadEnvFile() does the same from code.

Node.js Reading config safely

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.

The same idea scales to a small config module that validates everything once at startup: it is better for the bot to refuse to start with a clear message than to crash an hour later with 401 Unauthorized.

Recognising a valid token shape

Telegram tokens always look like <digits>:<35 characters of letters, digits, _ and ->. A cheap format check catches copy-paste mistakes (a trailing space, a missing half) before you make a network call. Discord tokens are three dot-separated parts, where the first part is the base64 encoding of the bot's user id.

Node.js Checking token formats

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.

A format check is not authentication: it only catches typos. The platform decides whether a token is truly valid, via getMe on Telegram and any authorised REST call on Discord.

A project layout that scales

Here is the layout this course uses. It is plain JavaScript, ES modules, no framework:

TEXT
my-bot/
  .env                 (secrets, listed in .gitignore)
  .gitignore
  package.json         ("type": "module")
  src/
    config.js          (reads and validates environment variables)
    api.js             (thin wrapper around fetch for the platform)
    commands/          (one file per command)
    handlers/          (events: messages, buttons, joins)
    index.js           (starts everything)
  test/
    commands.test.js   (tests for pure logic, run with node --test)

Two rules keep this healthy. First, commands are pure functions that take input and return a reply object; they never call fetch themselves. Second, only api.js talks to the network, so in tests you replace it with a fake. You will see that pattern in Telegram Handlers and Routing.

JSON
{
  "name": "my-bot",
  "type": "module",
  "scripts": {
    "start": "node --env-file=.env src/index.js",
    "test": "node --test"
  }
}

The real project would add a library such as grammY (Telegram), discord.js (Discord) or none at all. Libraries are optional: this course uses plain fetch first so you understand what the libraries do for you.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a bot token?

    The token is the bot's password. Whoever has it can act as the bot.
  2. 2. Where should a token live?

    Environment variables (or a hosting secrets manager) keep the token out of version control.
  3. 3. What does a regular-expression check on a Telegram token prove?

    A format check only catches typos. Only the platform can confirm that a token is valid, for example with getMe.
  4. 4. Your token appeared in a public repository. What do you do?

    Deleting history does not help because copies exist within seconds. Regenerate the token.

Summary

  • Telegram bots are created in a chat with @BotFather; Discord bots in the Developer Portal with an OAuth2 invite link.
  • A token is a password. Keep it in environment variables or a secrets manager, never in code, logs or Git.
  • Validate configuration at startup and mask tokens in any output.
  • A format check catches typos, but only the platform can verify a token.
  • Keep commands as pure functions and confine network calls to one module.

Next: Your First Telegram Bot