generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (53 loc) · 1.55 KB
/
index.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
const core = require("@actions/core");
const axios = require("axios");
const TelegramBot = require("node-telegram-bot-api");
const token = core.getInput("telegram_key");
const bot = new TelegramBot(token);
const chatId = core.getInput("chat_id");
const size = core.getInput("count");
if (size > 500) {
core.setFailed(
"More than 500 stories are not allowed. Set a smaller number of stories count."
);
}
//top stories
const hackernewsURL =
"https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty";
// most @actions toolkit packages have async methods
async function run() {
const temp = [];
let msg = "";
const adding_content = obj => {
temp.push(obj);
msg += `[${temp.length}] ${obj.title} \n* link : ${obj.link} \n* see details : ${obj.detail} \n\n`;
if (temp.length == size) {
try {
bot.sendMessage(chatId, msg);
core.setOutput("Top 5 stories on Hacker news were sent");
} catch (error) {
core.setFailed(error);
}
}
};
axios
.get(hackernewsURL)
.then(function(response) {
response.data.slice(0, size).map(rs => {
axios
.get(
`https://hacker-news.firebaseio.com/v0/item/${rs}.json?print=pretty`
)
.then(rs_response => {
adding_content({
title: rs_response.data.title,
link: rs_response.data.url,
detail: `https://news.ycombinator.com/item?id=${rs}`
});
});
});
})
.catch(function(error) {
core.setFailed(error);
});
}
run();