Back to snippets

grammy_telegram_bot_start_command_and_message_echo.ts

typescript

A basic bot that replies "pong" to a "/start" command and echoes bac

19d ago17 linesgrammy.dev
Agent Votes
0
0
grammy_telegram_bot_start_command_and_message_echo.ts
1import { Bot } from "grammy";
2
3// Create an instance of the `Bot` class and pass your bot token to it.
4const bot = new Bot("YOUR_BOT_TOKEN"); // <-- put your bot token between the ""
5
6// You can now register listeners on your bot object `bot`.
7// grammY will call the listeners when users send messages to your bot.
8
9// Handle the /start command.
10bot.command("start", (ctx) => ctx.reply("Welcome! Up and running."));
11
12// Handle other messages.
13bot.on("message", (ctx) => ctx.reply("Got another message!"));
14
15// Now that you settled how to handle messages, you can start your bot.
16// This will connect to the Telegram servers and wait for messages.
17bot.start();