Skip to content

Commit 370bace

Browse files
committed
feat()!: upload files as bot
1 parent 7ba63d5 commit 370bace

File tree

5 files changed

+32
-139
lines changed

5 files changed

+32
-139
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ DATABASE_URL=postgres://postgres:dev@localhost/tgbucket
22

33
TELEGRAM_API_ID=
44
TELEGRAM_API_HASH=
5-
TELEGRAM_SESSION=
5+
TELEGRAM_BOT_TOKEN=
6+
TELEGRAM_CHAT_ID

src/configs/mtproto.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { registerAs } from '@nestjs/config';
33

44
export const mtprotoConfig = registerAs('mtproto', () => {
5-
const store = JSON.parse(process.env.TELEGRAM_SESSION!);
5+
const store: Record<string, any> = {};
66
return {
77
api_id: parseInt(process.env.TELEGRAM_API_ID!, 10),
88
api_hash: process.env.TELEGRAM_API_HASH!,

src/telegram/telegram.service.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MTProto, MTProtoError } from '@mtproto/core';
2-
import { Inject, Injectable, Logger } from '@nestjs/common';
2+
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
3+
import { ConfigService } from '@nestjs/config';
34
import * as BlockStream from 'block-stream2';
45
import { Readable, Transform, TransformCallback } from 'stream';
56
import { pipeline } from 'stream/promises';
@@ -19,10 +20,26 @@ import {
1920
} from './telegram.types';
2021

2122
@Injectable()
22-
export class TelegramService {
23+
export class TelegramService implements OnModuleInit {
2324
private readonly logger = new Logger(this.constructor.name);
2425

25-
constructor(@Inject(MTPROTO) private readonly mtproto: MTProto) {}
26+
constructor(
27+
@Inject(MTPROTO) private readonly mtproto: MTProto,
28+
private readonly config: ConfigService,
29+
) {}
30+
31+
async onModuleInit(): Promise<void> {
32+
const token = this.config.get('TELEGRAM_BOT_TOKEN');
33+
await this.loginBot(token);
34+
}
35+
36+
async loginBot(token: string): Promise<void> {
37+
const res = await this.callApi('auth.importBotAuthorization', {
38+
bot_auth_token: token,
39+
});
40+
const username = res?.user?.username;
41+
this.logger.log(`Successfully logged in to Telegram as "${username}"`);
42+
}
2643

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

136-
async uploadAndSendDocumentToSelf(
153+
async uploadAndSendDocumentToChat(
154+
chatId: string,
137155
inputFile: InputFileBig,
138156
filename?: string,
139157
): Promise<UpdateNewMessage> {
@@ -147,7 +165,8 @@ export class TelegramService {
147165

148166
const updates = await this.callApi<Updates>('messages.sendMedia', {
149167
peer: {
150-
_: 'inputPeerSelf',
168+
_: 'inputPeerChat',
169+
chat_id: chatId,
151170
},
152171
media: {
153172
_: 'inputMediaUploadedDocument',

src/upload/upload.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable, Logger } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
23

34
import { FileEntity } from '../files/file.entity';
45
import { FilesService } from '../files/files.service';
@@ -12,6 +13,7 @@ export class UploadService {
1213
constructor(
1314
private readonly telegramService: TelegramService,
1415
private readonly filesService: FilesService,
16+
private readonly configService: ConfigService,
1517
) {}
1618

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

2426
this.logger.debug('Send media message to Telegram');
27+
const chatId = this.configService.get('TELEGRAM_CHAT_ID');
2528
const newMessageUpdate =
26-
await this.telegramService.uploadAndSendDocumentToSelf(
29+
await this.telegramService.uploadAndSendDocumentToChat(
30+
chatId,
2731
inputFile,
2832
file.filename,
2933
);

utils/generate-mtproto-session.util.ts

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)