Created
April 14, 2020 00:22
-
-
Save sheronw/cb716022219d4356c863ec29bdc7abec to your computer and use it in GitHub Desktop.
Use #IFTTT to send #Toggl Reports to #Telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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