Skip to main content
The Bot class is the single most important class in grammY. It represents your bot and handles all communication with Telegram.

Creating a Bot

To create a bot, you need a bot token from @BotFather. Once you have your token, instantiate a new Bot:
Never commit your bot token to version control. Use environment variables instead:

Configuration Options

The Bot constructor accepts optional configuration through the BotConfig interface:
ApiClientOptions
Advanced options for the API client that connects to Telegram’s servers
UserFromGetMe
Pre-initialize the bot with cached bot information to skip the initial getMe call. Useful for serverless environments where you restart frequently.
Constructor
Pass a custom context class constructor to use instead of the default Context class

Example with Configuration

Bot API Access

The bot provides full access to the Telegram Bot API through the api property:
Inside middleware, prefer using ctx.api instead of bot.api. The context API has the same methods but may include additional features like webhook reply envelopes.

Registering Middleware

The Bot class extends Composer, giving you access to all middleware registration methods:
See Middleware and Filter Queries for more details.

Running Your Bot

grammY provides a simple built-in long polling method:
The start() method accepts options:
number
default:"100"
Number of updates to fetch per request (1-100)
number
default:"30"
Timeout in seconds for long polling
string[]
Array of update types to receive. If not specified, receives all updates except chat_member, message_reaction, and message_reaction_count.
boolean
Pass true to drop all pending updates before starting
function
Callback function executed after setup completes, before fetching updates. Receives bot.botInfo as an argument.

Example with Options

The built-in bot.start() is designed for small to medium bots. For high-load production bots (>5K messages/hour), use the @grammyjs/runner package for better performance.

Stopping the Bot

To gracefully stop long polling:
This will:
  1. Cancel the current getUpdates request
  2. Prevent further getUpdates calls
  3. Confirm the last received update to Telegram

Bot Information

Access information about your bot through the botInfo property:
The botInfo property is only available after calling await bot.init() or bot.start(). Accessing it before initialization throws an error.

Manual Initialization

If you’re not using bot.start(), initialize the bot manually:

Error Handling

Set an error handler to catch errors in middleware:
See Error Handling for comprehensive error handling strategies.

Update Processing

Handling Updates Manually

For webhooks or custom update sources, use handleUpdate():

Update Flow

When an update arrives:
  1. Bot creates a new Api instance with the bot token
  2. Bot constructs a Context object with the update, API, and bot info
  3. Bot runs the middleware stack with the context
  4. Any errors are caught and passed to the error handler

Lifecycle Methods

Check if Running

Check if Initialized

Default Update Types

By default, grammY requests these update types:
If you register listeners for update types not in allowed_updates, grammY will warn you. Always include the update types you need:

Type Safety

The Bot class is generic, allowing custom context types:
You can also customize the API type:

Best Practices

Development vs ProductionFor development:
For production with high load:
Graceful Shutdown
Error Handling is RequiredAlways set an error handler before starting your bot:
Without an error handler, unhandled errors will crash your bot.