telegraf-update-logger

Update logging middleware for Telegraf

Usage no npm install needed!

<script type="module">
  import telegrafUpdateLogger from 'https://cdn.skypack.dev/telegraf-update-logger';
</script>

README

telegraf-update-logger

CI Codecov npm node MIT License code style: prettier standard-readme compliant

Update logging middleware for Telegraf

Example

Font: Ubuntu Mono, Theme: OceanicMaterial

Install

yarn add telegraf-update-logger

Examples

log all updates to console with colors

const Telegraf = require('telegraf');
const updateLogger = require('telegraf-update-logger');

const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(updateLogger({ colors: true }));
bot.startPolling();

log channel posts to file

const Telegraf = require('telegraf');
const updateLogger = require('telegraf-update-logger');

const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(
  updateLogger({
    filter: (update) => update.channel_post || update.edited_channel_post,
    log: (str) => fs.appendFileSync('messages.log', str),
  }),
);
bot.startPolling();

log all updates with custom colors

const Telegraf = require('telegraf');
const updateLogger = require('telegraf-update-logger');
const chalk = require('chalk');

const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(
  updateLogger({
    colors: {
      id: chalk.red,
      chat: chalk.yellow,
      user: chalk.green,
      type: chalk.bold,
    },
  }),
);
bot.startPolling();

reply to all messages with formatted updates

const Telegraf = require('telegraf');
const updateLogger = require('telegraf-update-logger');

const bot = new Telegraf(process.env.BOT_TOKEN);
bot.on('message', (ctx) => ctx.reply(updateLogger.format(ctx.update)));
bot.startPolling();

API

updateLogger(options: object?): function

Creates a middleware that logs every update and then invokes the next middleware.

Params:

  • options object? = {}
    • .filter (update: Update) => boolean – a function that determines which updates should be logged
    • .log (formattedUpdate: string) => void = console.log – a function that logs formatted updates
    • ... format options

updateLogger.format(update: Update, options: object?): string

Formats an update as string.

Params:

  • update Update
  • options object? = {}
    • .colors boolean | object = false – enables/disables/sets colors
      • .id function – a function that sets colors of message IDs
      • .chat function – a function that sets colors of chat titles
      • .user function – a function that sets colors of user names
      • .type function – a function that sets colors of message types

Contribute

PRs accepted.

License

MIT © Dmytro Meleshko