adonisjs-grammy
makes it easy for you to integrate AdonisJS with the Telegram Bot Framework
-Grammy-, allowing you to implement Webhooks instead of using Long Polling.
Learn more here and find out that
Grammy doesn't support AdonisJS.
node ace add adonisjs-grammy
Adjust the env configuration.
Create a tunnel if running on a local machine (recomended: cloudflared
or loophole
)
Set the webhook to http://<tunneling-addres>/<bot-token>
using the following API.
https://api.telegram.org/bot<bot-token>/setWebhook?url=http://<tunneling-address>/<bot-token>
Continue developing!
Open start/grammy.ts
and do whatever you want, as documented in the official
Grammy documentation.
import grammy from 'adonisjs-grammy/services/main'
// handle the /start command
grammy.command('start', (ctx) => ctx.reply('Welcome! Up and running.'))
// handle other messages
grammy.on('message', (ctx) => ctx.reply('Got another message!'))
The configuration file is located at config/grammy.ts
. Here are the available configuration options:
Variable | Type | Required | Description |
---|---|---|---|
TELEGRAM_API_TOKEN | string | Yes | Your Telegram Bot API token obtained from @BotFather |
TELEGRAM_SECRET_TOKEN | string | No | Optional secret token to secure your webhook endpoint |
The config/grammy.ts
file allows you to customize the following options:
Option | Type | Default | Description |
---|---|---|---|
apiToken | string | process.env.TELEGRAM_API_TOKEN |
The Telegram Bot API token |
secretToken | string | process.env.TELEGRAM_SECRET_TOKEN |
Optional secret token for webhook security |
onTimeout | 'throw' | 'return' | Function | 'throw' | Defines behavior when webhook request times out |
timeoutMilliseconds | number | 10_000 | Webhook request timeout in milliseconds |
botRouteName | string | apiToken | Custom route name for the webhook endpoint |
botConfig | object | undefined | Additional bot configuration options |
Example configuration:
import env from '#start/env'
import { defineConfig } from 'adonisjs-grammy'
const grammyConfig = defineConfig({
apiToken: env.get('TELEGRAM_API_TOKEN'),
secretToken: env.get('TELEGRAM_SECRET_TOKEN'),
// Timeout handling
timeoutMilliseconds: 10_000, // 10 seconds
onTimeout: 'throw', // or 'return', or custom function
// Custom route name (optional)
botRouteName: 'telegram-bot',
// Additional bot configuration
botConfig: {
client: {
baseFetchConfig: {
compress: true,
},
},
},
})
export default grammyConfig
The MIT License (MIT). Please see LICENSE file for more information.
This package is not officially maintained by Telegram. Telegram trademarks and logo are the property of Telegram Messenger LLP.
Table of contents