https://github.com/gramiojs/media-group
Media group collector plugin for GramIO
https://github.com/gramiojs/media-group
attachments gramio-plugin media-group telegram-bot telegram-bot-api
Last synced: about 1 year ago
JSON representation
Media group collector plugin for GramIO
- Host: GitHub
- URL: https://github.com/gramiojs/media-group
- Owner: gramiojs
- License: mit
- Created: 2024-05-15T18:31:58.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-24T18:47:54.000Z (over 1 year ago)
- Last Synced: 2025-03-25T06:36:35.065Z (about 1 year ago)
- Topics: attachments, gramio-plugin, media-group, telegram-bot, telegram-bot-api
- Language: TypeScript
- Homepage: https://gramio.dev/plugins/official/media-group
- Size: 41 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Media group plugin
[](https://www.npmjs.org/package/@gramio/media-group)
[](https://jsr.io/@gramio/media-group)
[](https://jsr.io/@gramio/media-group)
This plugin collects `mediaGroup` from messages (**1** attachment = **1** message) using a **delay** if `mediaGroupId` is in the **MessageContext**, pass only **first** message further down the middleware chain with the `mediaGroup` key, which contains an **array** of messages of this **mediaGroup** (it also contains the **first** message). The delay after 10 attachments (max messages in 1 mediaGroup) is automatically skipped.
```ts
import { Bot } from "gramio";
import { mediaGroup } from "@gramio/media-group";
const bot = new Bot(process.env.TOKEN as string)
.extend(mediaGroup())
.on("message", async (context) => {
if (!context.mediaGroup) return;
return context.send(
`Caption from the first message - ${context.caption}. MediaGroup contains ${context.mediaGroup.length} attachments`
);
})
.onStart(({ info }) => console.log(`✨ Bot ${info.username} was started!`));
bot.start();
```
### Setup
You can change the duration of the delay in milliseconds by simply passing it like this:
```typescript
const bot = new Bot(process.env.TOKEN!)
.extend(mediaGroup(1000)) // wait 1 second for message with mediaGroupId (refreshed after new message with it)
.on("message", async (context) => {
if (!context.mediaGroup) return;
return context.send(
`Caption from the first message - ${context.caption}. MediaGroup contains ${context.mediaGroup.length} attachments`
);
})
.onStart(({ info }) => console.log(`✨ Bot ${info.username} was started!`));
bot.start();
```
By default it `150 ms`.
## TODO
- Currently, it stateful, so it does not work with horizontal scaling. Fixes would be hard (for example serialize/deserialize context in redis and etc) and maybe slow in perfomance.