diff --git a/.gitignore b/.gitignore index 3c3629e..1dcef2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.env \ No newline at end of file diff --git a/index.js b/index.js index a370c44..2d685db 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,20 @@ import fetch from 'node-fetch'; +import readline from "readline"; +import { TelegramClient } from 'telegram'; +import { StoreSession } from 'telegram/sessions/index.js'; +import 'dotenv/config'; -function getCurrentSong() { +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const apiId = process.env.API_ID; +const apiHash = process.env.API_HASH; + +const sessionStore = new StoreSession("tg_session"); + +async function getCurrentSong() { return fetch('https://multi-scrobbler.riksolo.com/api/status').then((data) => { return data.json(); }).then(data => { @@ -20,7 +34,7 @@ function getCurrentSong() { const sortedPlayers = players.sort((a, b) => new Date(a.playFirstSeenAt).getTime() - new Date(b.playFirstSeenAt).getTime()); // default string - let nowPlaying = "Nothing Playing"; + let nowPlaying = ""; // if a player exists, get it's song if (sortedPlayers.length !== 0) { @@ -32,9 +46,21 @@ function getCurrentSong() { }); } +async function initTelegram() { + if (!apiId) { console.error("missing env var API_ID"); return; }; + if (!apiHash) { console.error("missing env var API_HASH"); return; }; + const client = new TelegramClient(sessionStore, apiId, apiHash, { + connectionRetries: 5 + }); -setInterval(async () => { - process.stdout.cursorTo(0); - process.stdout.clearLine(0); - process.stdout.write(await getCurrentSong()); -}, 2000); \ No newline at end of file + await client.start({ + phoneNumber: async (res) => { rl.question("Phone number: ", res); }, + password: async (res) => { rl.question("password: ", res); }, + phoneCode: async (res) => { rl.question("code: ", res); }, + onError: (err) => console.error(err) + }); + console.log(client.session.save()); + await CloseEvent.sendMessage("me", { message: "hi" }); +} + +initTelegram(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 087d64e..1c6d4b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,9 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "dotenv": "^16.4.7", "node-fetch": "^3.3.2", + "readline": "^1.3.0", "telegram": "^2.26.16" } }, @@ -180,6 +182,17 @@ "url": "https://github.com/fb55/domutils?sponsor=1" } }, + "node_modules/dotenv": { + "version": "16.4.7", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/entities": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", @@ -473,6 +486,11 @@ "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==", "license": "MIT" }, + "node_modules/readline": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz", + "integrity": "sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg==" + }, "node_modules/real-cancellable-promise": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/real-cancellable-promise/-/real-cancellable-promise-1.2.0.tgz", diff --git a/package.json b/package.json index 8cf6bc4..b11ca7f 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,9 @@ "license": "ISC", "description": "", "dependencies": { + "dotenv": "^16.4.7", "node-fetch": "^3.3.2", + "readline": "^1.3.0", "telegram": "^2.26.16" } -} \ No newline at end of file +}