Skip to content

Commit

Permalink
feat()!: upload files as bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Morb0 committed Feb 6, 2022
1 parent 7ba63d5 commit 370bace
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 139 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ DATABASE_URL=postgres://postgres:dev@localhost/tgbucket

TELEGRAM_API_ID=
TELEGRAM_API_HASH=
TELEGRAM_SESSION=
TELEGRAM_BOT_TOKEN=
TELEGRAM_CHAT_ID
2 changes: 1 addition & 1 deletion src/configs/mtproto.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { registerAs } from '@nestjs/config';

export const mtprotoConfig = registerAs('mtproto', () => {
const store = JSON.parse(process.env.TELEGRAM_SESSION!);
const store: Record<string, any> = {};
return {
api_id: parseInt(process.env.TELEGRAM_API_ID!, 10),
api_hash: process.env.TELEGRAM_API_HASH!,
Expand Down
29 changes: 24 additions & 5 deletions src/telegram/telegram.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MTProto, MTProtoError } from '@mtproto/core';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as BlockStream from 'block-stream2';
import { Readable, Transform, TransformCallback } from 'stream';
import { pipeline } from 'stream/promises';
Expand All @@ -19,10 +20,26 @@ import {
} from './telegram.types';

@Injectable()
export class TelegramService {
export class TelegramService implements OnModuleInit {
private readonly logger = new Logger(this.constructor.name);

constructor(@Inject(MTPROTO) private readonly mtproto: MTProto) {}
constructor(
@Inject(MTPROTO) private readonly mtproto: MTProto,
private readonly config: ConfigService,
) {}

async onModuleInit(): Promise<void> {
const token = this.config.get('TELEGRAM_BOT_TOKEN');
await this.loginBot(token);
}

async loginBot(token: string): Promise<void> {
const res = await this.callApi('auth.importBotAuthorization', {
bot_auth_token: token,
});
const username = res?.user?.username;
this.logger.log(`Successfully logged in to Telegram as "${username}"`);
}

async uploadFile(fileStream: Readable, size: number): Promise<InputFileBig> {
const partSize = 512 * 1024; // 512kb
Expand Down Expand Up @@ -133,7 +150,8 @@ export class TelegramService {
});
}

async uploadAndSendDocumentToSelf(
async uploadAndSendDocumentToChat(
chatId: string,
inputFile: InputFileBig,
filename?: string,
): Promise<UpdateNewMessage> {
Expand All @@ -147,7 +165,8 @@ export class TelegramService {

const updates = await this.callApi<Updates>('messages.sendMedia', {
peer: {
_: 'inputPeerSelf',
_: 'inputPeerChat',
chat_id: chatId,
},
media: {
_: 'inputMediaUploadedDocument',
Expand Down
6 changes: 5 additions & 1 deletion src/upload/upload.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { FileEntity } from '../files/file.entity';
import { FilesService } from '../files/files.service';
Expand All @@ -12,6 +13,7 @@ export class UploadService {
constructor(
private readonly telegramService: TelegramService,
private readonly filesService: FilesService,
private readonly configService: ConfigService,
) {}

async processFile(file: UploadFile): Promise<FileEntity> {
Expand All @@ -22,8 +24,10 @@ export class UploadService {
);

this.logger.debug('Send media message to Telegram');
const chatId = this.configService.get('TELEGRAM_CHAT_ID');
const newMessageUpdate =
await this.telegramService.uploadAndSendDocumentToSelf(
await this.telegramService.uploadAndSendDocumentToChat(
chatId,
inputFile,
file.filename,
);
Expand Down
131 changes: 0 additions & 131 deletions utils/generate-mtproto-session.util.ts

This file was deleted.

0 comments on commit 370bace

Please sign in to comment.