diff --git a/sahko.js b/sahko.js index 2e82c64..3361502 100644 --- a/sahko.js +++ b/sahko.js @@ -5,9 +5,33 @@ const sahko = async (ctx) => { const currentDate = date.toISOString().split('T')[0]; const currentHour = date.getHours(); const requestUrl = `https://api.porssisahko.net/v1/price.json?date=${currentDate}&hour=${currentHour}`; + const upcomingPrices = 'https://api.porssisahko.net/v1/latest-prices.json'; const response = await fetch(requestUrl); const json = await response.json(); - ctx.reply(`Sähkö maksaapi ${json.price} cnt/kwh`); + const upcomingResponse = await fetch(upcomingPrices); + const upcomingJson = await upcomingResponse.json(); + const futurePrices = upcomingJson.prices + .filter((item) => { + const startDate = new Date(item.startDate); + return startDate > date; + }) + .reverse() + .slice(0, 10) + .map((item) => { + const startDate = new Date(item.startDate); + const formattedStartDate = startDate.toLocaleTimeString('fi-FI', { + hour: '2-digit', + minute: '2-digit', + }); + return { startDate: formattedStartDate, price: item.price }; + }); + + const formattedFuturePrices = futurePrices + .map((item) => `${item.startDate} ${item.price.toFixed(3)} cnt/kwh`) + .join('\n'); + + ctx.reply(`Sähkö maksaapi ${json.price.toFixed(3)} cnt/kwh\n +Tulevat tunnit: \n${formattedFuturePrices}`); }; module.exports = { sahko };