import fetch from 'node-fetch'; import { TelegramClient, Api as TgApi } from 'telegram'; import { StoreSession } from 'telegram/sessions/index.js'; import 'dotenv/config'; import prompt from 'prompt'; const apiId = parseInt(process.env.API_ID); const apiHash = process.env.API_HASH; const songPrefix = process.env.SONG_PREFIX || ""; console.log(process.env); 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 => { const players = []; // get all players of all sources data.sources.forEach(source => { if (!source.players) return; const sourcePlayers = Object.values(source.players); sourcePlayers.forEach(player => { players.push(player); }); }); // sort players in order of most recent new song const sortedPlayers = players.sort((a, b) => new Date(a.playFirstSeenAt).getTime() - new Date(b.playFirstSeenAt).getTime()); // default string let nowPlaying = ""; // if a player exists, get it's song if (sortedPlayers.length !== 0) { const playData = sortedPlayers[0].play.data; nowPlaying = `${playData.artists.join(', ')} - ${playData.track}`; } return nowPlaying; }); } async function initTelegram() { // check if we have what we need for telegram if (!apiId) { console.error("missing env var API_ID"); return; }; if (!apiHash) { console.error("missing env var API_HASH"); return; }; // setup client const client = new TelegramClient(sessionStore, apiId, apiHash, { connectionRetries: 5 }); prompt.start(); // start it, prompt for auth if needed await client.start({ phoneNumber: async () => { const res = await prompt.get({ description: 'phone number', type: 'string', required: true, hidden: false }); return res.question; }, password: async () => { const res = await prompt.get({ description: 'password', type: 'string', required: true, hidden: true }); return res.question; }, phoneCode: async () => { const res = await prompt.get({ description: 'phone code', type: 'string', required: true, hidden: false }); return res.question; }, onError: (err) => console.error(err) }); // save the session so we don't have to log in every time client.session.save(); return client; } prompt.start(); // store client here const tg = await initTelegram(); // do we actually have a client? if (!!tg) { let song = ""; setInterval(async () => { const newSong = await getCurrentSong(); if (newSong === song) return; // don't do API calls if the song is the same as it was song = newSong; console.log(`new song: ${song}`); // update bio with new song tg.invoke(new TgApi.account.UpdateProfile({ about: songPrefix + song })); }, 5000); }