11import { 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' ;
34import * as BlockStream from 'block-stream2' ;
45import { Readable , Transform , TransformCallback } from 'stream' ;
56import { 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' ,
0 commit comments