generated from denorg/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
115 lines (110 loc) · 3.08 KB
/
mod.ts
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
import { load } from "https://deno.land/x/js_yaml_port/js-yaml.js";
import { join } from "https://deno.land/std@0.51.0/path/mod.ts";
import { readFileStr } from "https://deno.land/std@0.51.0/fs/mod.ts";
import ky from "https://unpkg.com/ky/index.js";
export interface RescueTimeDailySummary {
id: number;
date: string;
productivity_pulse: number;
very_productive_percentage: number;
productive_percentage: number;
neutral_percentage: number;
distracting_percentage: number;
very_distracting_percentage: number;
all_productive_percentage: number;
all_distracting_percentage: number;
total_duration_formatted: string;
very_productive_duration_formatted: string;
productive_duration_formatted: string;
neutral_duration_formatted: string;
distracting_duration_formatted: string;
very_distracting_duration_formatted: string;
all_productive_duration_formatted: string;
all_distracting_duration_formatted: string;
}
/** Fetch RescueTime daily summary for user */
export const fetchDailySummary = async (
apiKey: string
): Promise<RescueTimeDailySummary[]> => {
return ky
.get(`https://www.rescuetime.com/anapi/daily_summary_feed?key=${apiKey}`)
.json();
};
/** Post a message to a Slack channel */
export const postToSlack = async (
username: string,
icon_url: string,
url: string,
user: string,
data: RescueTimeDailySummary
) => {
const payload = {
username,
icon_url,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `*RescueTime* summary for *${user}* for ${new Date(
data.date
).toLocaleDateString("en-US", {
weekday: "long",
month: "long",
day: "numeric",
})}`,
},
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: `*${data.productivity_pulse}/100* \n Productivity Pulse`,
},
{
type: "mrkdwn",
text: `*${data.total_duration_formatted}* \n Total Duration`,
},
],
},
],
};
console.log(await ky.post(url, { json: payload }).text());
};
/** Run the RescueTime Slack script */
export const rescuetimeSlack = async () => {
console.log("Started");
const file = await readFileStr(join(".", "rescuetime-slack.yml"));
const config: {
apiKeys: { [index: string]: string };
webhook: string;
botName: string;
botIcon: string;
} = load(file);
config.webhook = config.webhook.replace(
"$WEBHOOK",
Deno.env.get("WEBHOOK") ?? ""
);
for await (const user of Object.keys(config.apiKeys)) {
const summaries = await fetchDailySummary(
config.apiKeys[user].replace(
"$API_KEY",
Deno.env.get(
`API_KEY_${user.toLocaleUpperCase().replace(/ /g, "_")}`
) ?? ""
)
);
if (!summaries.length) continue;
await postToSlack(
config.botName,
config.botIcon,
config.webhook,
user,
summaries[0]
);
console.log(`Posted ${user}'s summary to Slack`);
}
console.log("Success");
};
rescuetimeSlack();