An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          




All that you want to easily process media groups in telegraf.js


Compatible with telegraf.js v4.0
Language: typescript
Package size
License: MIT

## 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)
}
})
```