Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@eomm/fastify-telegram

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eomm/fastify-telegram

Create and manage a Telegram bot, easily

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
84
decreased by-46.84%
Maintainers
1
Weekly downloads
 
Created
Source

fastify-telegram

JavaScript Style Guide ci

Create and manage a Telegram bot, easily.

This plugin is a good wrapper around Telegraf build by a Fastify maintainer.

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 Node.js HTTP server because the Telgram 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 fastify-telegram

Compatibility

Plugin versionFastify versionTelegraf 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('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.

Keywords

FAQs

Package last updated on 15 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc