-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.js
92 lines (84 loc) · 2.7 KB
/
telegram.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const { getRecap } = require("./src/api");
const { abbreviateNumber, numFormat } = require("./src/number");
const { updateLastRunTime } = require("./src/dynamodb");
const { createImageFromTemplate, getRandomTheme } = require("./src/html");
const { getENV } = require("./src/env");
const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(getENV("TELEGRAM_BOT_TOKEN"));
exports.handler = async function (event, context) {
// Prevent timeout from waiting event loop - Chromium
context.callbackWaitsForEmptyEventLoop = false;
try {
// Run daily recap
await dailyRecap();
// Update last run time on DynamoDB
updateLastRunTime("telegram", { type: "daily-recap" });
// Log
console.log("Daily recap published successfully on telegram!");
} catch (err) {
console.error(err);
}
};
/**
* Generate a weekly recap image and publish it on Instagram
*/
async function dailyRecap() {
try {
// Get Data from API
const data = await getRecap("daily");
// USDT Token
// const usd= data.find((item) => item.has_iran && item.symbol === 'USDT');
// Total Trade Volume
const totalVol = data
.filter((item) => item.has_iran)
.reduce((acc, item) => acc + item.irr.volume, 0);
// Filter Data
const tokens = data
.filter((item) => item.has_iran)
.map((item) => {
return {
name: item.name_en,
price: "$" + numFormat(item.usd.price_avg),
volume:
abbreviateNumber(Math.round(item.irr.volume), 1, true) + " IRR",
icon: item.icon,
};
})
.slice(0, 10);
// Create Image
const image = await createImageFromTemplate(
"table-" + getRandomTheme(),
{
tokens,
headers: ["Token", "Average Price", "Traded Volume"],
title: "Daily Recap",
subtitle: `Total traded volume today: ${abbreviateNumber(
Math.round(totalVol),
0,
true
)} IRR`,
lastUpdate: new Date().toISOString().slice(0, 10),
},
"daily.jpg"
);
console.log("generated image:", image);
if (!image) {
throw new Error("Image not generated!");
}
// Get Caption from AI
const caption = `
📈 Today's Crypto Market Recap | ${new Date().toISOString().slice(0, 10)}
📊 Total Traded Volume Today: ${numFormat(Math.round(totalVol))} IRR
Source: @irancrypto_market
Follow us on <a href="http://instagram.com/irancryptomarket">Instagram @irancryptomarket</a>
`;
// Publish the image on Telegram channel
await bot.sendPhoto(getENV("TELEGRAM_CHANNEL_ID"), image, {
caption: caption,
parse_mode: "html",
disable_web_page_preview: true,
});
} catch (err) {
throw err;
}
}