https://github.com/dmitmel/telegraf-update-logger
Update logging middleware for Telegraf
https://github.com/dmitmel/telegraf-update-logger
Last synced: 11 months ago
JSON representation
Update logging middleware for Telegraf
- Host: GitHub
- URL: https://github.com/dmitmel/telegraf-update-logger
- Owner: dmitmel
- License: mit
- Created: 2018-04-21T08:10:52.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-08T15:03:51.000Z (over 3 years ago)
- Last Synced: 2025-08-09T17:22:33.352Z (11 months ago)
- Language: TypeScript
- Homepage: https://npmjs.com/package/telegraf-update-logger
- Size: 770 KB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# telegraf-update-logger
[](https://github.com/dmitmel/telegraf-update-logger/actions?query=workflow:CI)
[](http://npmjs.com/package/telegraf-update-logger)
[](https://nodejs.org)
[](http://opensource.org/licenses/MIT)
[](https://github.com/prettier/prettier)
[](https://github.com/RichardLitt/standard-readme)
> [Update](https://core.telegram.org/bots/api#update) logging middleware for [Telegraf](http://telegraf.js.org/)

_Font: [Ubuntu Mono](https://design.ubuntu.com/font/), Theme: [OceanicMaterial](https://github.com/mbadolato/iTerm2-Color-Schemes#oceanicmaterial)_
## Install
```bash
yarn add telegraf-update-logger
```
## Examples
### log all updates to console with colors
```js
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
```js
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
```js
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
```js
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`](#updateloggerformatupdate-update-options-object-string) options**
### updateLogger.format(update: Update, options: object?): string
Formats an update as string.
**Params**:
- **`update`** [Update](https://core.telegram.org/bots/api#update)
- **`options`** `object?` `= {}`
- **`.colors`** `boolean | object` `= false` – enables/disables/sets [colors](https://github.com/chalk/chalk/)
- **`.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](LICENSE) © [Dmytro Meleshko](https://github.com/dmitmel)