Public
Cron
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const TELEGRAM_CHAT_ID = Deno.env.get("TELEGRAM_CHAT_ID");
const TELEGRAM_TOKEN = Deno.env.get("TELEGRAM_TOKEN");
const COINGECKO_API_KEY = Deno.env.get("COINGECKO_API_KEY");
const getCryptoPrices = async () => {
const response = await fetch(
"https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin,ethereum,dogecoin,solana,binancecoin,ripple,pepe",
{
headers: {
"accept": "application/json",
"x-cg-demo-api-key": COINGECKO_API_KEY,
},
},
);
const data = await response.json();
return data.reduce((acc, coin) => {
acc[coin.id] = {
usd: coin.current_price,
usd_24h_change: coin.price_change_percentage_24h,
usd_24h_high: coin.high_24h,
usd_24h_low: coin.low_24h,
};
return acc;
}, {});
};
const formatCryptoPrices = (prices) => {
const formatPrice = (name, priceData, decimals = 2) => {
const usd = priceData.usd ? priceData.usd.toFixed(decimals) : "N/A";
const change = priceData.usd_24h_change ? priceData.usd_24h_change.toFixed(2) : "N/A";
const high = priceData.usd_24h_high ? priceData.usd_24h_high.toFixed(decimals) : "N/A";
const low = priceData.usd_24h_low ? priceData.usd_24h_low.toFixed(decimals) : "N/A";
return `
<u>${name}:</u> <b>$${usd}</b>%0A
<b>24h Change:</b> ${change}%%0A
<b>24h High:</b> $${high}%0A
<b>24h Low:</b> $${low}%0A`;
};
return `
<b>Cryptocurrency Prices</b>💰%0A
${formatPrice("Bitcoin (BTC)", prices.bitcoin)}%0A
${formatPrice("Ethereum (ETH)", prices.ethereum)}%0A
${formatPrice("Dogecoin (DOGE)", prices.dogecoin, 3)}%0A
${formatPrice("Solana (SOL)", prices.solana)}%0A
${formatPrice("Binance Coin (BNB)", prices.binancecoin)}%0A
${formatPrice("Ripple (XRP)", prices.ripple)}%0A
${formatPrice("Pepe (PEPE)", prices.pepe, 6)}%0A`;
};
const sendTextWithTelegram = async (text) => {
const telegramSendUrl =
`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage?chat_id=${TELEGRAM_CHAT_ID}&parse_mode=HTML&disable_notification=true&text=${text}`;
await fetch(telegramSendUrl);
};
export default async function(interval) {
const cryptoPrices = await getCryptoPrices();
const formatted = formatCryptoPrices(cryptoPrices);
await sendTextWithTelegram(formatted);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 29, 2024