https://github.com/dietime/telegraf-media-group
All that you want to easily process media groups in telegraf.js
https://github.com/dietime/telegraf-media-group
group media middleware telegraf
Last synced: 4 months ago
JSON representation
All that you want to easily process media groups in telegraf.js
- Host: GitHub
- URL: https://github.com/dietime/telegraf-media-group
- Owner: DieTime
- License: mit
- Created: 2024-09-23T21:40:24.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-09-26T08:06:36.000Z (over 1 year ago)
- Last Synced: 2025-10-06T01:57:07.570Z (8 months ago)
- Topics: group, media, middleware, telegraf
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@dietime/telegraf-media-group
- Size: 47.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
All that you want to easily process media groups in telegraf.js
## Introduction
When writing a telegram bot using telegraf.js, you may want to process a group of media sent by a user.
This package provides all that you want to solve this problem:
- ⚡ Middleware
- 🔥 Filters
- 🚀 Wrapper types for context
## Installation
```bash
$ npm i @dietime/telegraf-media-group
```
## Usage
### Attaching `MediaGroup` middleware
You need to attach `MediaGroup` middleware to your bot so that when you receive media group messages, these messages will be accumulated.
```ts
import { MediaGroup } from '@dietime/telegraf-media-group'
bot.use(new MediaGroup({ timeout: 1000 }).middleware())
```
The `timeout` option controls the time the middleware will wait for the next media group message. If no message is received within this timeout, middleware sends a message with accumulated media group messages for processing.
### Processing a photo and video media group
To process a media group that contains both photos and videos, use the `media_group()` filter and wrap the context in the `MediaGroupContext` type for better typing.
```ts
import { media_group, type MediaGroupContext } from '@dietime/telegraf-media-group'
bot.on(media_group(), (ctx: MediaGroupContext) => {
for (const media of ctx.update.media_group) {
if ('photo' in media) {
console.log('Photo:', media.photo)
}
if ('video' in media) {
console.log('Video:', media.video)
}
}
})
```
### Processing a photo-only media group
To process a media group that contains only photos, use `photo_media_group()` filter and wrap the context in `PhotoMediaGroupContext` type for better typing.
```ts
import { photo_media_group, type PhotoMediaGroupContext } from '@dietime/telegraf-media-group'
bot.on(photo_media_group(), (ctx: PhotoMediaGroupContext) => {
for (const media of ctx.update.media_group) {
console.log('Photo:', media.photo)
}
})
```
### Processing a video-only media group
To process a media group that contains only videos, use `video_media_group()` filter and wrap the context in `VideoMediaGroupContext` type for better typing.
```ts
import { video_media_group, type VideoMediaGroupContext } from '@dietime/telegraf-media-group'
bot.on(video_media_group(), (ctx: VideoMediaGroupContext) => {
for (const media of ctx.update.media_group) {
console.log('Video:', media.video)
}
})
```