manually handle storing session in file

This commit is contained in:
Rik Berkelder 2025-01-13 23:33:18 +01:00
parent 85128f389a
commit 0acef577a3
1 changed files with 20 additions and 2 deletions

View File

@ -1,14 +1,30 @@
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import { TelegramClient, Api as TgApi } from 'telegram'; import { TelegramClient, Api as TgApi } from 'telegram';
import { StoreSession } from 'telegram/sessions/index.js'; import { StringSession } from 'telegram/sessions/index.js';
import 'dotenv/config'; import 'dotenv/config';
import prompt from 'prompt'; import prompt from 'prompt';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const apiId = parseInt(process.env.API_ID); const apiId = parseInt(process.env.API_ID);
const apiHash = process.env.API_HASH; const apiHash = process.env.API_HASH;
const songPrefix = process.env.SONG_PREFIX || ""; const songPrefix = process.env.SONG_PREFIX || "";
const sessionStore = new StoreSession("tg_session"); const sessionFile = path.join(__dirname, 'tg_session', 'session');
let sessionStore = new StringSession("");
try {
const readSession = await fs.readFile(sessionFile, "utf8");
if (readSession) {
sessionStore = new StringSession(readSession);
}
} catch (e) {
console.error(e);
}
async function getCurrentSong() { async function getCurrentSong() {
return fetch('https://multi-scrobbler.riksolo.com/api/status').then((data) => { return fetch('https://multi-scrobbler.riksolo.com/api/status').then((data) => {
@ -75,6 +91,8 @@ async function initTelegram() {
// save the session so we don't have to log in every time // save the session so we don't have to log in every time
client.session.save(); client.session.save();
await fs.writeFile(sessionFile, sessionStore.save());
return client; return client;
} }