Nestgram
  • ⭐About Nestgram
  • 📄Guide
  • 📰News
  • 🔼Handling updates
    • Handling commands
    • Handling text messages
    • Handling other updates
    • Entities: url, email and other
    • Other arguments in handler
    • Handling media files and download it
  • 🪶Nestgram Features
    • Services
    • Middlewares and Params
    • Sending messages correctly
    • Answer class
    • Scopes
    • States
    • Views
  • 💬Messages
    • Sending a photo, video and other media
    • Sending a media group
    • Send location (live) or venue
    • Send contact
    • Send poll
    • Send dice
    • Edit/delete messages
    • Copy or Forward a message
  • ⌨️Keyboards
    • Keyboard types, building keyboard
    • Handle underTheMessage keyboard button click by Alert or Toast
    • Keyboard layouts
  • ⚙️Config
    • CLI
    • Webhooks and run config
    • Api class
    • Modules
      • Mongo module
    • Controller Helper class
  • 🤖API Reference
    • Set chat action
    • Save user profile photo
    • Ban, unban user or chat
    • Restrict or Promote user
    • Set chat permissions
    • Set chat admin custom title
    • Chat invite links
    • Join requests
    • Update chat info, photo, title, description and more
    • Pin or unpin messages
    • Get chat info, leave chat
    • Check user subscription
    • Chat sticker set
    • My commands
    • My default admin rights
    • Menu button
Powered by GitBook
On this page
  • Handle when the user sends some media file
  • Download sent media
  • Download sent media using SaveFile class
  1. Handling updates

Handling media files and download it

Handle when the user sends some media file (e.g. video, photo, audio) and download it

PreviousOther arguments in handlerNextServices

Last updated 2 years ago

Handle when the user sends some media file

Use @OnMedia, @OnPhoto, @OnVideo, @OnAudio decorators to handle when the user sends some media file. You can read more about this

Download sent media

Use the .saveFile method in the Answer class to do this and pass the path to it. You can use this method to download any sent media (e.g. audio, video, photo). Below, you can see how to download the photo

.saveFile method takes the path in which you want to save file as argument

You can read more about Answer class

.saveFile method take arguments:

Argument
Description
Required
1

Path in which you want to save file

Required

2

File id you want to save

Optional. Current file id by default (downloads media file sent by the user)

app.controller.ts
import { Answ, Answer, OnPhoto, Controller } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

@Controller()
export class AppController {
  constructor(private readonly appService?: AppService) {}

  @OnPhoto()
  async savePhoto(@Answ() answer: Answer): Promise<string> {
    await answer.saveFile(path.resolve(__dirname, 'image.png'));
    return 'Media file downloaded';
  }
}

Download sent media using SaveFile class

You can also use SaveFile class-method to download media sent by the user. We recommend using this class-method if possible

SaveFile class-method take arguments:

Argument
Description
Required
1

Path in which you want to save file

Required

2

File id you want to save

Optional. Current file id by default (downloads media file sent by the user)

app.controller.ts
import { Controller, SaveFile, OnPhoto } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @OnPhoto()
  async savePhoto(): Promise<SaveFile> {
    return new SaveFile(path.resolve(__dirname, 'image.png')).next('File saved!');
  }
}

About .next syntax you can read

🔼
here
here
here