Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dietime/telegraf-media-group

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dietime/telegraf-media-group

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

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

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

$ 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.

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

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

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

Keywords

FAQs

Package last updated on 26 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc