2025-04-25 12:50:13 +02:00
|
|
|
/*
|
|
|
|
This file is part of »munin«.
|
|
|
|
|
|
|
|
Copyright 2025 'Fenris Wolf' <fenris@folksprak.org>
|
|
|
|
|
|
|
|
»munin« is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
»munin« is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with »munin«. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace _munin.targets.telegram_bot
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export type type_parameters = {
|
|
|
|
bot_token : string;
|
|
|
|
chat_id : int;
|
2025-05-18 07:42:04 +02:00
|
|
|
hide_tags : boolean;
|
2025-06-30 13:06:51 +02:00
|
|
|
reminders : Array<_munin.type_reminder>;
|
2025-07-01 19:04:41 +02:00
|
|
|
language : string;
|
2025-07-19 12:00:38 +02:00
|
|
|
strings : {
|
|
|
|
notification_head : string;
|
|
|
|
};
|
2025-04-25 12:50:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2025-07-01 19:04:41 +02:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
function get_translation(
|
|
|
|
parameters : type_parameters,
|
|
|
|
path : string
|
|
|
|
) : string
|
|
|
|
{
|
|
|
|
return lib_plankton.translate.get_new(
|
|
|
|
path,
|
|
|
|
{
|
|
|
|
"preferred_language": parameters.language,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-04-25 12:50:13 +02:00
|
|
|
/**
|
|
|
|
*/
|
2025-06-30 13:06:51 +02:00
|
|
|
function render_event(
|
2025-04-25 12:50:13 +02:00
|
|
|
parameters : type_parameters,
|
|
|
|
event : _munin.type_event
|
2025-06-30 13:06:51 +02:00
|
|
|
) : string
|
|
|
|
{
|
|
|
|
return lib_plankton.string.coin(
|
|
|
|
"{{title_label}} | {{macro_tags}}{{title_value}}\n{{time_label}} | {{time_value}}{{macro_location}}{{macro_description}}",
|
|
|
|
{
|
|
|
|
"macro_tags": (
|
|
|
|
(parameters.hide_tags || (event.tags === null))
|
|
|
|
?
|
|
|
|
""
|
|
|
|
:
|
|
|
|
(event.tags.map(tag => ("{" + tag + "}")).join(" ") + " ")
|
|
|
|
),
|
2025-07-01 19:04:41 +02:00
|
|
|
"title_label": get_translation(parameters, "core.event.title.short").toUpperCase(),
|
2025-06-30 13:06:51 +02:00
|
|
|
"title_value": event.title,
|
2025-07-01 19:04:41 +02:00
|
|
|
"time_label": get_translation(parameters, "core.event.time.short").toUpperCase(),
|
2025-06-30 13:06:51 +02:00
|
|
|
"time_value": lib_plankton.pit.timespan_format(
|
|
|
|
event.begin,
|
|
|
|
event.end,
|
|
|
|
{
|
|
|
|
"adjust_to_ce": true,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
"macro_location": (
|
|
|
|
(event.location === null)
|
|
|
|
?
|
|
|
|
""
|
|
|
|
:
|
|
|
|
lib_plankton.string.coin(
|
|
|
|
"\n{{location_label}} | {{location_value}}",
|
|
|
|
{
|
2025-07-01 19:04:41 +02:00
|
|
|
"location_label": get_translation(parameters, "core.event.location.short").toUpperCase(),
|
2025-06-30 13:06:51 +02:00
|
|
|
"location_value": event.location,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"macro_description": (
|
|
|
|
(event.description === null)
|
|
|
|
?
|
|
|
|
""
|
|
|
|
:
|
|
|
|
lib_plankton.string.coin(
|
|
|
|
"\n\n{{description_value}}",
|
|
|
|
{
|
|
|
|
"description_value": event.description,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
async function send(
|
|
|
|
parameters : type_parameters,
|
|
|
|
events : Array<_munin.type_event>
|
2025-04-25 12:50:13 +02:00
|
|
|
) : Promise<void>
|
|
|
|
{
|
|
|
|
await lib_plankton.telegram.bot_call_send_message(
|
|
|
|
parameters.bot_token,
|
|
|
|
parameters.chat_id,
|
|
|
|
lib_plankton.string.coin(
|
2025-07-19 12:00:38 +02:00
|
|
|
"*{{head}}*\n\n{{body}}",
|
2025-04-25 12:50:13 +02:00
|
|
|
{
|
2025-07-19 12:00:38 +02:00
|
|
|
"head": lib_plankton.string.coin(
|
|
|
|
parameters.strings.notification_head,
|
2025-07-01 19:04:41 +02:00
|
|
|
{
|
2025-07-19 12:00:38 +02:00
|
|
|
"core": lib_plankton.string.coin(
|
|
|
|
"{{label_event}}-{{label_reminder}}",
|
|
|
|
{
|
|
|
|
"label_event": lib_plankton.string.capitalize(get_translation(parameters, "core.event.event")),
|
|
|
|
"label_reminder": lib_plankton.string.capitalize(get_translation(parameters, "core.reminder.reminder")),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
"extra": (
|
|
|
|
(events.length <= 1)
|
|
|
|
?
|
|
|
|
""
|
|
|
|
:
|
|
|
|
lib_plankton.string.coin(
|
|
|
|
" ({{count}} {{events}})",
|
|
|
|
{
|
|
|
|
"count": events.length.toFixed(0),
|
|
|
|
"events": get_translation(parameters, "core.event.events"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
),
|
2025-07-01 19:04:41 +02:00
|
|
|
}
|
|
|
|
),
|
2025-07-19 12:00:38 +02:00
|
|
|
"body": (
|
2025-06-30 13:06:51 +02:00
|
|
|
events
|
2025-07-01 19:04:41 +02:00
|
|
|
.map(event => render_event(parameters, event))
|
|
|
|
.join("\n\n--------\n\n")
|
2025-06-12 07:25:15 +02:00
|
|
|
),
|
2025-04-25 12:50:13 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
{
|
2025-05-06 22:12:02 +02:00
|
|
|
"parse_mode": "Markdown",
|
2025-04-25 12:50:13 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export function implementation_target(
|
|
|
|
parameters : type_parameters
|
|
|
|
) : _munin.type_target
|
|
|
|
{
|
|
|
|
return {
|
2025-04-28 21:51:03 +02:00
|
|
|
"reminders": parameters.reminders,
|
|
|
|
"show": () => lib_plankton.string.coin(
|
|
|
|
"telegram:{{chat_id}}",
|
|
|
|
{
|
|
|
|
"chat_id": parameters.chat_id.toFixed(0),
|
|
|
|
}
|
|
|
|
),
|
2025-07-01 19:04:41 +02:00
|
|
|
"send": (events) => send(parameters, events),
|
2025-04-25 12:50:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|