Quickstart: Create your first Telegram bot
mainTo create a basic Telegram bot using grammY, follow these steps:
- Obtain a Bot Token: Message @BotFather on Telegram to create a new bot and receive your API token.
- Install grammY: Use npm to install the package in your project directory.
- Initialize the Bot: Create a script (e.g.,
bot.js) using theBotclass. - Register Listeners: Use
.on()to handle specific updates, such asmessage:text. - Start the Bot: Call
.start()to begin long polling for updates.
Note: This example uses Node.js. For Deno, import from https://deno.land/x/grammy/mod.ts.
const { Bot } = require("grammy");
// Create a bot object
const bot = new Bot("YOUR_BOT_TOKEN");
// Register listeners to handle messages
bot.on("message:text", (ctx) => ctx.reply("Echo: " + ctx.message.text));
// Start the bot (using long polling)
bot.start();