Add backend scripts needed for the bot

This commit is contained in:
FliegendeWurst 2021-02-27 10:18:43 +01:00
parent be147764ea
commit 77bf2e3edb
3 changed files with 116 additions and 0 deletions

24
event_alerts.js Normal file
View File

@ -0,0 +1,24 @@
// Create as JS Backend note and add these attributes:
// ~targetTemplateEvent=@event template (this note is described below)
// #customRequestHandler=event_alerts
// Create another note (event template) with this promoted attributes:
// startTime
// Optionally add:
// endTime, location
const {req, res} = api;
const targetTemplate = await api.currentNote.getRelationValue("targetTemplateEvent");
const events = await api.getNotesWithLabel("template", targetTemplate);
let eventsData = [];
for (const event of events) {
const attr = await event.getAttribute("label", "startTime");
eventsData.push({
name: event.title,
startTime: attr.value
});
}
res.send(eventsData);

63
new_reminder.js Normal file
View File

@ -0,0 +1,63 @@
// Create as JS Backend note with these attributes:
// ~targetTemplate=@reminder template
// #customRequestHandler=new_reminder
// Create a new note (reminder template) with these promoted attributes:
// todoDate: date
// todoTime: text
// doneDate: date
// reminder = true
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
const {req, res} = api;
const time = new Date(req.body["time"]);
api.log(time);
time.addHours(1); // TODO: consider summer time
const task = req.body["task"];
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
if (hour < 10) {
hour = '0' + hour;
}
if (minute < 10) {
minute = '0' + minute;
}
if (second < 10) {
second = '0' + second;
}
const year = time.getFullYear();
var month = time.getMonth() + 1;
var day = time.getDate();
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
const todayDateStr = year + "-" + month + "-" + day;
const todayNote = await api.getDateNote(todayDateStr);
const targetTemplate = await api.currentNote.getAttributeValue('relation', 'targetTemplate');
const resp = await api.createNote(
todayNote.noteId,
task,
'',
{
"attributes": [
{"type": "relation", "name": "template", "value": targetTemplate},
{"type": "label", "name": "todoDate", "value": todayDateStr},
{"type": "label", "name": "todoTime", "value": hour + ":" + minute + ":" + second}
]}
);
res.sendStatus(200);

29
task_alerts.js Normal file
View File

@ -0,0 +1,29 @@
// Create as JS Backend note with attributes:
// ~targetTemplate=@task template (included in Trilium task manager)
// #customRequestHandler=task_alerts
// ~targetTemplateReminder=@reminder template (see new_reminder.js)
const {req, res} = api;
const targetTemplate = await api.currentNote.getRelationValue('targetTemplate');
const tasks = await api.getNotesWithLabel("template", targetTemplate);
let tasksData = [];
for (const task of tasks) {
tasksData.push({
attributes: await task.getAttributes(),
...task
});
}
const targetTemplateReminder = await api.currentNote.getRelationValue('targetTemplateReminder');
const reminders = await api.getNotesWithLabel("template", targetTemplateReminder);
for (const task of reminders) {
tasksData.push({
attributes: await task.getAttributes(),
...task
});
}
res.send(tasksData);