munin/source/main.ts

165 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-04-25 00:48:05 +02:00
namespace _lixer_event_reminder
{
/**
*/
export async function main(
args_raw : Array<string>
): Promise<void>
{
// args
const arg_handler : lib_plankton.args.class_handler = new lib_plankton.args.class_handler(
{
"action": lib_plankton.args.class_argument.positional({
"index": 0,
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "run",
"info": "what to do : help | fetch | send | run",
"name": "action",
}),
"conf_path": lib_plankton.args.class_argument.volatile({
"indicators_long": ["conf-path"],
"indicators_short": ["c"],
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "conf.json",
"info": "path to configuration file",
"name": "conf-path",
}),
"message": lib_plankton.args.class_argument.volatile({
"indicators_long": ["message"],
"indicators_short": ["m"],
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "test",
"info": "message to send",
"name": "message",
}),
"help": lib_plankton.args.class_argument.volatile({
"indicators_long": ["help"],
"indicators_short": ["h"],
"type": lib_plankton.args.enum_type.boolean,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"info": "alias for action 'help'",
"name": "help",
}),
}
);
const args : Record<string, any> = arg_handler.read(
lib_plankton.args.enum_environment.cli,
args_raw.join(" ")
);
// init
const conf : _lixer_event_reminder.conf.type_conf = await lib_plankton.conf.load<_lixer_event_reminder.conf.type_conf>(
_lixer_event_reminder.conf.schema,
args.conf_path
);
// exec
if (args.help || (args.action === "help")) {
process.stdout.write(
arg_handler.generate_help(
{
"programname": "lixer-event-reminder",
"description": "a telegram bot, which sends reminders about upcoming events",
"executable": "node build/event-reminder",
}
)
);
}
else {
switch (args.action) {
default: {
throw (new Error("unhandled action: " + args.action));
break;
}
case "fetch": {
const updates : Array<any> = await lib_plankton.telegram.bot_call_get_updates(
conf.bot_token
);
process.stdout.write(JSON.stringify(updates, undefined, "\t") + "\n");
break;
}
case "send": {
for (const target of conf.targets) {
const message = await lib_plankton.telegram.bot_call_send_message(
conf.bot_token,
target.chat_id,
args.message
);
}
break;
}
case "run": {
const sources : Array<_lixer_event_reminder.type_source> = conf.sources.map(
source_raw => _lixer_event_reminder.sources.factory(source_raw)
);
const targets : Array<_lixer_event_reminder.type_target> = conf.targets.map(
target_raw => target_raw
);
const now : lib_plankton.pit.type_pit = lib_plankton.pit.now();
const events : Array<_lixer_event_reminder.type_event> = (
(await Promise.all(sources.map(source => source.fetch())))
.reduce((x, y) => x.concat(y), [])
);
for (const event of events) {
const begin : lib_plankton.pit.type_pit = lib_plankton.pit.from_datetime(event.begin);
for (const target of targets) {
for (const hours of target.interval) {
const remind : boolean = lib_plankton.pit.is_between(
lib_plankton.pit.shift_hour(begin, hours),
now,
/**
* @todo parametrize window
*/
lib_plankton.pit.shift_hour(now, +24)
);
if (remind) {
lib_plankton.log._info(
"remind",
{
"details": {
"event": event,
"target": target,
}
}
);
/**
* @todo activate
* @todo format better
*/
const message = await lib_plankton.telegram.bot_call_send_message(
conf.bot_token,
target.chat_id,
lib_plankton.string.coin(
"{{title}} | {{begin}}",
{
"title": event.title,
"begin": lib_plankton.pit.datetime_format(event.begin),
}
)
);
}
}
}
}
break;
}
}
}
return Promise.resolve<void>(undefined);
}
}
_lixer_event_reminder.main(process.argv.slice(2))
.then(() => {})
.catch((reason) => {process.stderr.write(String(reason) + "\n");})
;