Skip to content

blasdfaa/fastify-grammy

Repository files navigation

fastify-grammy

CI NPM version

🚧 In development

This library is a work in progress and in active development.

Supports Fastify versions 5.x

Install

# npm
npm install fastify-grammy

# pnpm
pnpm install fastify-grammy

Usage

Import fastify-grammy and register.

import Fastify from 'fastify'
import { fastifyGrammy } from 'fastify-grammy'

const fastify = Fastify()

fastify.register(fastifyGrammy, {
  token: 'your-tg-bot-token'
})

fastify.listen({ port: 3000 })

Before using fastify-grammy, make sure to check the Grammy documentation for more information about how to create and configure a Telegram bot.

Middlewares

You can use middlewares with fastify-grammy by passing them in the options object when registering the plugin.

import { session } from 'grammy'

fastify.register(fastifyGrammy, {
  token: 'your-tg-bot-token',
  middlewares: [session()]
})

Using with TypeScript

To use fastify-grammy with TypeScript, you need to extend the Fastify instance with the FastifyGrammyBot type.

Here's an example for a single instance:

import type { FastifyGrammyBot } from 'fastify-grammy'

declare module 'fastify' {
  interface FastifyInstance {
    grammy: FastifyGrammyBot
  }
}

And here's an example for multiple instances:

import type { FastifyGrammyBot } from 'fastify-grammy'

declare module 'fastify' {
  interface FastifyInstance {
    grammy: {
      one: FastifyGrammyBot
      two: FastifyGrammyBot
    }
  }
}

Acknowledgements

This library is based on the Grammy library.

License

Licensed under MIT.