Handling media files and download it

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

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 here

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 here

.saveFile method take arguments:

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:

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

Last updated