Skip to content

Instantly share code, notes, and snippets.

@sheronw
Created April 14, 2020 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheronw/cb716022219d4356c863ec29bdc7abec to your computer and use it in GitHub Desktop.
Save sheronw/cb716022219d4356c863ec29bdc7abec to your computer and use it in GitHub Desktop.
Use #IFTTT to send #Toggl Reports to #Telegram
const https = require("https");
const api_token = "你的 API Token";
const workplace_id = "你的 Workplace ID";
const user_agent = "你的邮箱或应用名";
const webhook = `https://maker.ifttt.com/trigger/你的Event名/with/key/你url的key`;
const url = new URL(
"https://toggl.com/reports/api/v2/details?user_agent=" +
user_agent +
"&workspace_id=" +
workplace_id
);
const options = {
headers: {
"Content-Type": "application/json",
},
auth: api_token + ":api_token",
};
https.get(url, options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
let minEntries = JSON.parse(data).data.reduce((acc, cur) => {
// 选取需要的信息
const { description, start, end, project } = cur;
const summary = `${start.slice(5, 10)} ${start.slice(11, 16)}-${end.slice(
11,
16
)}「${project}」${description}<br>`;
return acc + summary;
}, "");
sendToIFTTT(minEntries);
});
});
function sendToIFTTT(data) {
const postData = JSON.stringify({
value1: data,
});
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(postData),
},
};
const req = https.request(webhook, options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
});
req.on("error", (e) => {
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.write(postData);
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment