Sending a photo, video and other media

Sending a media by url or path

Send photo

If you want to send photo as message, you can use Photo class and pass it as content to answer.send or MessageSend

Photo class take arguments:

ArgumentDescriptionRequired
1

Type how you want to upload the photo. Pass 'path' if you want to upload local file, or pass 'url' if you want to upload file by url

Required

2

Content. Pass path to the file if you passed 'path' as 1st argument, or pass url if you passed 'url' as 1st argument

Required

3

Optional

4

Cache. Pass false to disable the cache

Optional

app.controller.ts
import { OnCommand, Controller, MessageSend, Photo } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

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

  @OnCommand('start')
  async start(): Promise<MessageSend> {
    return new MessageSend(new Photo('path', path.resolve(__dirname, 'image.jpg')));
  }
}

Send other media

You can also use other classes to send various media files. For example, use another class instead of the Photo class:

  • Photo

  • Video

  • Audio

  • Document

  • Animation

  • Voice

  • VideoNote

Set thumb

You can set the thumb for some media files. Just use the .setThumb method on the media class

.setThumb method takes argument: new instance of the Thumb class

app.controller.ts
import { OnCommand, Controller, MessageSend, Audio, Thumb } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

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

  @OnCommand('start')
  async start(): Promise<MessageSend> {
    return new MessageSend(
      new Audio('path', path.resolve(__dirname, 'audio.mp3'))
        .setThumb(new Thumb('path', path.resolve(__dirname, 'thumb.jpg')))
    );
  }
}

Set caption

To set caption, use .setCaption media class method

.setCaption method takes argument: caption you want to set (string)

app.controller.ts
import { OnCommand, Controller, MessageSend, Photo } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

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

  @OnCommand('start')
  async start(): Promise<MessageSend> {
    return new MessageSend(
      new Photo('path', path.resolve(__dirname, 'photo.jpeg'))
        .setCaption('Photo caption'),
    );
  }
}

Media caches

When you upload a new media file, its ID is stored in the nestgram/media.json file so that when you resubmit this media file, you don't upload it again

If you want to disable cache for the same file, pass false as 4th argument to the media class

Last updated