begin setting up telegram side of things

This commit is contained in:
Rik Berkelder 2025-01-11 00:05:56 +01:00
parent 73e2dfd793
commit 1e5d72a376
4 changed files with 55 additions and 8 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules
.env

View File

@ -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);
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();

18
package-lock.json generated
View File

@ -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",

View File

@ -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"
}
}