All that you want to easily process media groups in telegraf.js
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
$ npm i @dietime/telegraf-media-group
You need to attach MediaGroup
middleware to your bot so that when you receive media group messages, these messages will be accumulated.
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.
To process a media group that contains both photos and videos, use the media_group()
filter and wrap the context in the MediaGroupContext<T>
type for better typing.
import { media_group, type MediaGroupContext } from '@dietime/telegraf-media-group'
bot.on(media_group(), (ctx: MediaGroupContext<Context>) => {
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)
}
}
})
To process a media group that contains only photos, use photo_media_group()
filter and wrap the context in PhotoMediaGroupContext<T>
type for better typing.
import { photo_media_group, type PhotoMediaGroupContext } from '@dietime/telegraf-media-group'
bot.on(photo_media_group(), (ctx: PhotoMediaGroupContext<Context>) => {
for (const media of ctx.update.media_group) {
console.log('Photo:', media.photo)
}
})
To process a media group that contains only videos, use video_media_group()
filter and wrap the context in VideoMediaGroupContext<T>
type for better typing.
import { video_media_group, type VideoMediaGroupContext } from '@dietime/telegraf-media-group'
bot.on(video_media_group(), (ctx: VideoMediaGroupContext<Context>) => {
for (const media of ctx.update.media_group) {
console.log('Video:', media.video)
}
})