-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.js
124 lines (114 loc) · 3.36 KB
/
tweet.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { getPopular } = require("./src/api");
const { tweet } = require("./src/twitter");
const { writeTweet } = require("./src/ai");
const { getLastRunTime, updateLastRunTime } = require("./src/dynamodb");
const { abbreviateNumber } = require("./src/number");
exports.handler = async function (event) {
//Get Data from API
try {
const popularItems = await getPopular();
//Calculate total Vol
let totalVolIRR = 0;
popularItems.forEach((item) => {
totalVolIRR += item.irr.volume;
item.irrfvol = abbreviateNumber(item.irr.volume, 1, false); //Formatted version
});
//Tweets subject
const tweets = {
trends:
"Write about the top 3 crypto trends in the past 24 hours in Iran. Include only the name and volume in IRR currency. Please format each trend on a new line for clarity.",
vol: "Write about the total volume of crypto transactions in Iran in the past 24 hours, all in IRR currency",
};
//Last run check
const lastRun = await getLastRunTime('tweet');
if (Date.now() - (lastRun.timestamp ?? 0) < 3600 * 1000) {
throw new Error("Last run is less than one hour!");
}
//Remove runned one
delete tweets[lastRun.actionSubject];
let post;
let lastKey;
//Pick one and tweet from the list
for await (const [key, value] of Object.entries(tweets)) {
post = buildTweet(
key,
await writeTweet(value),
popularItems,
totalVolIRR
);
lastKey = key;
break;
}
//Send tweet
if (post) {
await tweet(post);
}
//Update last run time to know what was the last tweet
await updateLastRunTime('tweet',{actionSubject: lastKey});
//Out
console.log("Tweet sent successfully!", post);
} catch (err) {
console.error(err);
}
};
/**
*
* @param {string} $type
* @param {string} $content
* @param {object[]} $popularItems
* @param {number} $totalVolIRR
* @returns {string}
*/
function buildTweet($type, $content, $popularItems, $totalVolIRR) {
// Remove double quotes from the beginning and end of the string if they exist
if ($content && $content.startsWith('"') && $content.endsWith('"')) {
$content = $content.slice(1, -1);
}
switch ($type.toLowerCase()) {
case "trends":
let placeholderIndex = 1;
for (let i = 0; i < 3; i++) {
//Name
$content = $content.replace(
`%${placeholderIndex}%`,
$popularItems[i].name_en
);
placeholderIndex++;
//Vol
$content = $content.replace(
`%${placeholderIndex}%`,
$popularItems[i].irrfvol
);
placeholderIndex++;
}
break;
case "vol":
$content = $content.replace(
`%1%`,
abbreviateNumber($totalVolIRR, 1, true)
);
break;
default:
throw new Error("Template could not recognized");
break;
}
return lineBreak($content);
}
/**
* Line break before hashtag
* @param {string} inputText
* @returns {string}
*/
function lineBreak(inputText) {
// Find the index of the first hashtag
const firstHashtagIndex = inputText.indexOf("#");
// Insert a line break before the first hashtag
let formattedText = inputText;
if (firstHashtagIndex !== -1) {
formattedText =
formattedText.slice(0, firstHashtagIndex) +
"\n\n" +
formattedText.slice(firstHashtagIndex);
}
return formattedText;
}