You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I am encountering a bug When I try to create a new TelegramClient with a new StringSession from a stored value (saved in my DB from the session.save() method).
The bug I'm getting is that the client.isUserAuthorized method returns false even if the user was connected a minute ago (before I restarted the server). Am I missing something?
typeConnectedClient={client: TelegramClient;session: StringSession;userId: string;};
@Injectable()exportclassTelegramAuthService{apiId=parseInt(process.env.TELEGRAM_API_ID);apiHash=process.env.TELEGRAM_API_HASH;connectedClients: ConnectedClient[]=[];constructor(
@InjectModel(Telegram.name)privatereadonlyTelegramModel: Model<Telegram>,
@InjectModel(Crisis.name)privatereadonlycrisisModel: Model<Crisis>,privatereadonlyvolunteerService: VolunteerService){}// The function that should return a client and sessionprivateasyncgetClient(userId: string,userKey: string=""){constconnectedClient=this.connectedClients.find((v)=>v.userId===userId);if(connectedClient){if(!connectedClient.client.connected)awaitconnectedClient.client.connect();returnconnectedClient;}constsession=newStringSession(userKey);constclient=newTelegramClient(session,this.apiId,this.apiHash,{});awaitclient.connect();constnewClient: ConnectedClient={
client,
session,
userId,};this.connectedClients.push(newClient);returnnewClient;}//Returns the stored session if exists to null otherwiseasyncgetUserKey(userId: string): Promise<string|null>{constuserKey: string|null=awaitthis.volunteerService.getVolTelegramKeyById(userId);returnuserKey;}asyncsendTelegramCode(userId: string){const{ client }=awaitthis.getClient(userId);constphoneNumber=awaitthis.getSignedPhoneNumberById(userId);try{constsentCode=awaitclient.sendCode({apiHash: this.apiHash,apiId: this.apiId,},phoneNumber);console.log("Code sent successfully:",sentCode);return{success: true,};}catch(error){return{success: false,seconds: error.seconds,};}}asyncconnectWithKey(config: {userId: string;key: string;}){const{key: userKey, userId }=config;if(!userKey)thrownewBadRequestException("User has no telegram key");console.log(userKey);// print a 350+ length string that is stored in the DBconst{ client }=awaitthis.getClient(userId,userKey);constauthorized=awaitclient.isUserAuthorized();//always comes as falseif(!authorized)thrownewBadRequestException("User is not authorized");return{success: true,};}asyncconnectWithCode(config: {userId: string;code: string;}){const{ code, userId }=config;constphoneNumber=awaitthis.getSignedPhoneNumberById(userId);try{const{ client, session }=awaitthis.getClient(userId);awaitclient.signInUser({apiHash: this.apiHash,apiId: this.apiId,},{phoneCode: async()=>code,phoneNumber: phoneNumber,onError: async(e)=>(console.log(e),true),});constuserKey=session.save();awaitthis.volunteerService.storeVolTelegramKey(userId,userKey);}catch(e){console.error(e);if(e&&typeofe==="object"&&e.code===420){thrownewUnauthorizedException({seconds: e.seconds,message: `User is banned for ${e.errorMessage}`,},`User is banned for ${e.errorMessage}`);}elseif(einstanceofError&&e.message==="AUTH_USER_CANCEL"){thrownewUnauthorizedException("Wrong Code");}else{console.log("error inside catch",e);throwe;}}}//Utils:privateasyncgetSignedPhoneNumberById(userId: string){constnumber=awaitthis.volunteerService.getPhoneNumber(userId);returnthis.toSignedPhoneNumber(number);}privatetoSignedPhoneNumber(phoneNumber: string){phoneNumber=phoneNumber.trim();if(phoneNumber.startsWith("0"))return"+972"+phoneNumber.slice(1);if(phoneNumber.startsWith("972"))return"+"+phoneNumber;returnphoneNumber;}}
The text was updated successfully, but these errors were encountered:
Hi, I am encountering a bug When I try to create a new TelegramClient with a new StringSession from a stored value (saved in my DB from the session.save() method).
The bug I'm getting is that the client.isUserAuthorized method returns false even if the user was connected a minute ago (before I restarted the server). Am I missing something?
The text was updated successfully, but these errors were encountered: