1.1.0 • Published 8 months ago

adonisjs-grammy v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

adonisjs-grammy

Introduction

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.

Installation

node ace add adonisjs-grammy

Next steps?

  1. Adjust the env configuration.
  2. Create a tunnel if running on a local machine (recomended: cloudflared or loophole)
  3. 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>
  4. Continue developing!

Usage

Open start/grammy.ts and do whatever you want, as documented in the official Grammy documentation.

Example

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!'))

Configuration

The configuration file is located at config/grammy.ts. Here are the available configuration options:

Environment Variables

VariableTypeRequiredDescription
TELEGRAM_API_TOKENstringYesYour Telegram Bot API token obtained from @BotFather
TELEGRAM_SECRET_TOKENstringNoOptional secret token to secure your webhook endpoint

Configuration Options

The config/grammy.ts file allows you to customize the following options:

OptionTypeDefaultDescription
apiTokenstringprocess.env.TELEGRAM_API_TOKENThe Telegram Bot API token
secretTokenstringprocess.env.TELEGRAM_SECRET_TOKENOptional secret token for webhook security
onTimeout'throw' | 'return' | Function'throw'Defines behavior when webhook request times out
timeoutMillisecondsnumber10_000Webhook request timeout in milliseconds
botRouteNamestringapiTokenCustom route name for the webhook endpoint
botConfigobjectundefinedAdditional 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

License

The MIT License (MIT). Please see LICENSE file for more information.

Disclaimer

This package is not officially maintained by Telegram. Telegram trademarks and logo are the property of Telegram Messenger LLP.

1.1.0

8 months ago

1.0.0

1 year ago