Skip to content

First Steps

What we'll do

  • Create a bot account
  • Install and configure the project
  • Launch a bot that responds to the /start command
  • Learn to work with environment variables and --watch mode

Creating a Telegram Bot Account

First, create your bot and get a token. You can do this using the @BotFather bot.

Send him the /newbot command and follow the instructions until you receive a token like 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw.

Installation

To begin, we need to create a project and install GramIO using npm. Create a folder and run npm init in it to create a package.json for our project. Also specify "type": "module" so that the config looks something like this:

json
{
    "name": "bot-test",
    "type": "module",
    "version": "1.0.0",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "description": ""
}

Next, we'll install the dependency we need - GramIO.

bash
npm install gramio # or npm i gramio

Your First Bot

Let's write the logic for our first bot. Create an index.js file and write:

js
import { Bot } from "gramio";

const bot = new Bot("12345678:AaBbCcDdEeFfGgHh").command("start", (context) =>
    context.send("You sent /start")
);

bot.start();

where 12345678:AaBbCcDdEeFfGgHh is your bot token.

This code can be broken down into parts:

  • import { Bot } from "gramio"; - this is an import of the main class of our library.
  • const bot = new Bot("12345678:AaBbCcDdEeFfGgHh") - we create a bot variable and initialize the class by passing it our bot token.
  • .command("start", (context) => {...}); - We register our /start command handler that will be called when a user sends this command.
  • context.send("You sent /start") - using this method we send the specified message to the same chat.
  • bot.start(); - with this we start listening for events using Long-Polling (not important for now).

Let's run this code with node index.js and check the bot's operation.

What's .send?

This is a method of the Message context that allows you to send a message to the same chat where the original message came from. Thanks to this, we don't need to fully specify which chat we need to send a message to, but if needed, we can also use the regular methods from the API.

You can send a request to the API outside of event handlers:

js
import { Bot } from "gramio";

const bot = new Bot("12345678:AaBbCcDdEeFfGgHh");

bot.api.sendMessage({
    chat_id: "required chat id",
    text: "You sent /start",
});

But in handlers, you'll need to access the context.bot variable which is our initialized bot class:

js
import { Bot } from "gramio";

const bot = new Bot("12345678:AaBbCcDdEeFfGgHh").command("start", (context) =>
    context.bot.api.sendMessage({
        chat_id: "required chat id",
        text: "You sent /start",
    })
);

bot.start();

Bot Configuration

It's worth remembering that storing the token directly in your code is bad practice. It's recommended to store secrets in environment variables, so let's create a .env file in our project and move the token there:

dotenv
BOT_TOKEN=12345678:AaBbCcDdEeFfGgHh

Declaring environment variables in this file is extremely simple - it's just a key=value pair.

Great, the file is created, but how do we access these environment variables? For this, there's the process.env object where these variables will be available in our program. It's worth remembering that the .env file exists for convenience, and not all programs read it by default. For example, in Node.js we need to specify it using the --env-file argument.

Let's modify our code to use environment variables:

js
import { Bot } from "gramio";

const bot = new Bot(process.env.BOT_TOKEN).command("start", (context) =>
    context.send("You sent /start")
);

bot.start();

And let's run it by specifying our .env file (docs):

bash
node --env-file=.env index.js

Let's also add --watch mode, which will restart our script whenever changes are made to the file:

bash
node --watch --env-file=.env index.js

For convenience, let's put this command in our package.json:

Add a scripts object and dev with the contents of our command. The result should look something like this:

json
{
    "name": "bot-test",
    "type": "module",
    "version": "1.0.0",
    "main": "index.js",
    "scripts": {
        "dev": "node --watch --env-file=.env index.js"
    },
    "author": "",
    "license": "ISC",
    "description": "",
    "dependencies": {
        "gramio": "^0.0.40"
    }
}

Now we can run it like this: npm run dev