Skip to content

Commit

Permalink
chore: remove empty strings where not required
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Oct 3, 2024
1 parent 92cae41 commit 91e76e1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
32 changes: 28 additions & 4 deletions src/bot/mtproto-api/bot/scripts/sms-auth/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ dotenv.config();
export class AuthHandler {
private _github: GithubStorage | undefined;
private _env = {
TELEGRAM_API_HASH: "",
TELEGRAM_API_HASH: null,
TELEGRAM_APP_ID: 0,
TELEGRAM_BOT_TOKEN: "",
};
TELEGRAM_BOT_TOKEN: null,
} as {
TELEGRAM_API_HASH: string | null;
TELEGRAM_APP_ID: number;
TELEGRAM_BOT_TOKEN: string | null;
}

constructor() {
const env = process.env.TELEGRAM_BOT_ENV;
Expand Down Expand Up @@ -79,7 +83,27 @@ export class AuthHandler {
async smsLogin() {
const mtProto = new BaseMtProto();
// empty string as it's a new session
await mtProto.initialize(this._env, "");


if (this._env.TELEGRAM_API_HASH === null) {
throw new Error("Missing required environment variables for Telegram API");
}

if (this._env.TELEGRAM_APP_ID === 0) {
throw new Error("Missing required environment variables for Telegram API");
}

if (this._env.TELEGRAM_BOT_TOKEN === null) {
throw new Error("Missing required environment variables for Telegram API");
}

const envObj = {
TELEGRAM_API_HASH: this._env.TELEGRAM_API_HASH,
TELEGRAM_APP_ID: this._env.TELEGRAM_APP_ID,
TELEGRAM_BOT_TOKEN: this._env.TELEGRAM_BOT_TOKEN,
}

await mtProto.initialize(envObj, null);
try {
await mtProto.client?.start({
phoneNumber: async () => await input.text("Enter your phone number:"),
Expand Down
4 changes: 2 additions & 2 deletions src/bot/mtproto-api/bot/scripts/sms-auth/base-mtproto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class BaseMtProto {
_api: typeof Api = Api;
_session: StringSession | null = null;

async initialize(env: Context["env"]["TELEGRAM_BOT_ENV"]["mtProtoSettings"], session: string) {
async initialize(env: Context["env"]["TELEGRAM_BOT_ENV"]["mtProtoSettings"], session: string | null) {
this._api = Api;
this._session = new StringSession(session);
this._session = new StringSession(session ?? ""); // Empty String Required.
this._client = await this._mtProtoInit(env, this._session);
}

Expand Down
14 changes: 7 additions & 7 deletions src/bot/mtproto-api/bot/scripts/sms-auth/setup-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ dotenv.config();
*/

class SetUpHandler {
private _env: Context["env"] = {
GITHUB_PAT_TOKEN: "",
private _env = {
GITHUB_PAT_TOKEN: null,
TELEGRAM_BOT_ENV: {
botSettings: {
TELEGRAM_BOT_ADMINS: [],
TELEGRAM_BOT_TOKEN: "",
TELEGRAM_BOT_WEBHOOK: "",
TELEGRAM_BOT_WEBHOOK_SECRET: "",
TELEGRAM_BOT_TOKEN: null,
TELEGRAM_BOT_WEBHOOK: null,
TELEGRAM_BOT_WEBHOOK_SECRET: null,
},
mtProtoSettings: {
TELEGRAM_API_HASH: "",
TELEGRAM_API_HASH: null,
TELEGRAM_APP_ID: 0,
},
},
};
} as unknown as Context["env"];

get env() {
return this._env;
Expand Down
10 changes: 8 additions & 2 deletions src/bot/mtproto-api/bot/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import { GithubStorage } from "../../../adapters/github/storage-layer";
export class GithubSession extends StringSession {
github: GithubStorage;
context: Context;
session: string;
session?: string

constructor(github: GithubStorage, context: Context, session?: string) {
super(session);
this.github = github;
this.context = context;
this.session = session || "";
this.session = session
}

async saveSession(): Promise<void> {
if (!this.session) {
throw new Error("No session found. Please run the SMS Login script first.");
}
await this.github.handleSession(this.session, "create");
}

Expand All @@ -44,6 +47,9 @@ export class GithubSession extends StringSession {
}

async deleteSession(): Promise<void> {
if (!this.session) {
throw new Error("No session found. Please run the SMS Login script first.");
}
await this.github.handleSession(this.session, "delete");
}
}

0 comments on commit 91e76e1

Please sign in to comment.