npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ardasevinc/fastify-telegraf

v1.3.7

Published

Pure ESM, modern fork of @eomm/fastify-telegram

Downloads

17

Readme

fastify-telegram

Create and manage a Telegram bot easily.

This plugin is a wrapper around Telegraf

Why you should use this plugin compared to what you can find on Google?

  • All the blog posts and tutorials I found are outdated and use old versions of Telegraf
  • The mentioned tutorials start 2 HTTP servers, one for Fastify and one for Node.js HTTP server, because the Telegram wrapper starts its own HTTP server. This is just a waste of resources.

This plugin instead:

  • Integrates the Telegram bot into the Fastify HTTP server
  • You can use all the Fastify features, like hooks and decorators
  • It has good utilities to manage errors and the telegram features (such as polling mode)

Install

npm install @ardasevinc/fastify-telegraf

Compatibility

| Plugin version | Fastify version | Telegraf version | | ------------- |:---------------:| ---------------:| | ^1.0.0 | ^4.0.0 | ^4.0.0 |

Usage

When you register the plugin, it will add a decorator to the Fastify instance.

You can use it to access the Telegraf instance and configure your bot:

const fastify = require('fastify')
const fastifyTelegram = require('@eomm/fastify-telegram')

async function run () {
  const app = fastify({
    pluginTimeout: 10_000 // suggestion: increase the timeout
  })

  await app.register(fastifyTelegram, {
    botToken: '123-abc', // [required] it must be a valid bot token

    // [required] in webhook mode: the base url of your webhook
    // [optional] in long polling mode: undefined
    baseUrl: 'https://example.come',

    // [optional]: customize the decorator name
    decoratorBotName: 'telegramBot',

    // [optional] this string is used to validate the incoming request
    // Ref: https://core.telegram.org/bots/api#setwebhook
    webhookSecret: 'secret',

    // [optional]: the polling mode requires an health check that slows down the startup
    // you can customize the max milliseconds to wait for the health check to pass
    waitForHealthPolling: 3_000, // default: app.initialConfig.pluginTimeout / 6

    // [optional] this function is called when an error is not handled
    // by default it logs the error using `fastify.log.error`
    // Ref: https://telegrafjs.org/index.html#/?id=error-handling
    onUnhandledError: (err, ctx) => {
      console.log(`Ooops, encountered an error for ${ctx.updateType}`, err)
    }
  })

  // `app.telegramBot` is a Telegraf instance
  // Now you can start using it:
  app.telegramBot.on('text', (ctx) => ctx.reply('Hello World'))

  await app.listen({ port: 3001 })
}

run()

Webhook mode

In this mode, the Telegram bot will send the updates to your server, so the plugin must know the public base url of your server.

Then, it will register a POST route at the url /telegraf/<secretPath> where <secretPath> is a random string generated by Telegraf.

You can read the Webhook secret path from the app.telegramWebhook decorator.

Long polling mode

In this mode, the plugin will start a long polling process that will fetch the updates from the Telegram server by calling the method getUpdates.

The timeout is set to 50 seconds by default, and you can't change it: it's a Telegraf limitation.

Options

You can pass the following options to the plugin:

  • botToken: [required] it must be a valid bot token
  • baseUrl: [required] in webhook mode: the base url of your webhook; in long polling mode: undefined
  • decoratorBotName: [optional] customize the decorator name
  • webhookSecret: [optional] this string is used to validate the incoming request
  • onUnhandledError: [optional] this function is called when an error is not handled by default it logs the error using fastify.log.error

Decorators

The plugin adds the following decorators to the Fastify instance:

  • app.telegramBot: the Telegraf instance
  • app.telegramWebhook: the webhook secret path (only in webhook mode)

License

Copyright Manuel Spigolon, Licensed under MIT.