Send Telegram notices, answer phone commands and call the heartbeat (refs #52)
Every notice is written to the queue in the same transaction as what it tells, then sent every ten seconds. Each has a state: pending, sent, or failed with its error. Network failures and 429s are retried with growing pauses (Telegram's own wait when it gives one). Refusals are final. A notice that could not leave within a day is given up instead of arriving late. Every fill now also reaches the phone, and so do pause, resume and restart. Commands are read by long polling and obeyed only from the configured chat and only when fresh, so yesterday's /pausa never pauses today. They are /stato, /posizioni, /oggi, /pausa, /riprendi, /ferma (confirmed with /conferma within two minutes) and /aiuto. Restarting after "Ferma tutto", real money, limits and keys stay in the interface. Also added: - the heartbeat call every minute when an address is configured; - the morning summary at 08:00, caught up within four hours; - an optional hourly or evening status; - a Telegram section in Impostazioni: test-before-save, a test button, the queue's state and the last successful heartbeat; - on desktop, the first-run page recommends the heartbeat. Tests run against a fake Telegram server, including the commands through the real server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d00dfbd32f
commit
84c540786c
16 files changed
+1416
-17
No files matched your search
+107
-2
@@ -34,6 +34,11 @@ internal static partial class Forms
|
||||
signed.MapPost("/chiavi/{broker}", SaveKeysAsync);
|
||||
signed.MapPost("/chiavi/{broker}/togli", RemoveKeysAsync);
|
||||
signed.MapPost("/capitale/{broker}", SetCapitalAsync);
|
||||
signed.MapPost("/telegram", SaveTelegramAsync);
|
||||
signed.MapPost("/telegram/prova", TestTelegramAsync);
|
||||
signed.MapPost("/telegram/togli", RemoveTelegramAsync);
|
||||
signed.MapPost("/segnale-di-vita", SaveHeartbeatAsync);
|
||||
signed.MapPost("/segnale-di-vita/togli", RemoveHeartbeatAsync);
|
||||
signed.MapPost("/laboratorio", PlanTrialAsync);
|
||||
signed.MapPost("/strategie/{strategy}/{choice}", StrategyStageAsync);
|
||||
signed.MapPost("/rischio/limiti", SetLimitsAsync);
|
||||
@@ -159,7 +164,7 @@ internal static partial class Forms
|
||||
private static async Task<IResult> SetSettingAsync(string key, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
var back = Back(form, "/impostazioni");
|
||||
if (key != SettingsStore.Theme.Key && key != SettingsStore.Currency.Key && key != SettingsStore.CentralBankMeetings.Key)
|
||||
if (key != SettingsStore.Theme.Key && key != SettingsStore.Currency.Key && key != SettingsStore.CentralBankMeetings.Key && key != SettingsStore.TelegramStatus.Key)
|
||||
{
|
||||
return Results.Redirect(With(back, "errore", "impostazione"));
|
||||
}
|
||||
@@ -176,7 +181,7 @@ internal static partial class Forms
|
||||
private static async Task<IResult> BotAsync(string command, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
var back = Back(form, "/");
|
||||
var (ok, message) = await Endpoints.BotCommandAsync(host, command, context, ct).ConfigureAwait(false);
|
||||
var (ok, message) = await Endpoints.BotCommandAsync(host, command, context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
|
||||
return Results.Redirect(ok ? back : With(back, "errore", message.Contains("STOP", StringComparison.Ordinal) ? "file-stop" : "bot"));
|
||||
}
|
||||
|
||||
@@ -274,6 +279,106 @@ internal static partial class Forms
|
||||
return Results.Redirect(back + "?ok=chiavi#conti");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The bot's Telegram (SPEC 14): a test message first, sent at once; token and chat are saved only when it left.
|
||||
/// </summary>
|
||||
private static async Task<IResult> SaveTelegramAsync(IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
const string back = "/impostazioni";
|
||||
if (host.IsDemo || host.Services is not { Telegram: { } telegram } services)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=motore#telegram");
|
||||
}
|
||||
|
||||
if ((await services.Secrets.StatusAsync(Anubis.Notifications.TelegramSettings.TokenSecret, ct).ConfigureAwait(false)).FromEnvironment
|
||||
|| (await services.Secrets.StatusAsync(Anubis.Notifications.TelegramSettings.ChatSecret, ct).ConfigureAwait(false)).FromEnvironment)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=telegram-ambiente#telegram");
|
||||
}
|
||||
|
||||
var token = form["token"].ToString().Trim();
|
||||
var chat = form["chat"].ToString().Trim();
|
||||
if (token.Length is 0 or > 200 || chat.Length is 0 or > 40)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=telegram-vuoto#telegram");
|
||||
}
|
||||
|
||||
if (await telegram.TestAsync(new Anubis.Notifications.TelegramSettings(token, chat), ct).ConfigureAwait(false) is not null)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=telegram-rifiutato#telegram");
|
||||
}
|
||||
|
||||
var who = context.User.Identity?.Name ?? "utente";
|
||||
await services.Secrets.SetAsync(Anubis.Notifications.TelegramSettings.TokenSecret, token, who, ct).ConfigureAwait(false);
|
||||
await services.Secrets.SetAsync(Anubis.Notifications.TelegramSettings.ChatSecret, chat, who, ct).ConfigureAwait(false);
|
||||
return Results.Redirect(back + "?ok=telegram#telegram");
|
||||
}
|
||||
|
||||
// The form is bound even when unused: binding it is what makes the antiforgery check run on the route.
|
||||
private static async Task<IResult> TestTelegramAsync(IFormCollection form, AnubisHost host, CancellationToken ct)
|
||||
{
|
||||
const string back = "/impostazioni";
|
||||
if (host.IsDemo || host.Services is not { Telegram: { } telegram }
|
||||
|| await telegram.ReadAsync(ct).ConfigureAwait(false) is not { } settings)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=telegram-vuoto#telegram");
|
||||
}
|
||||
|
||||
return Results.Redirect(await telegram.TestAsync(settings, ct).ConfigureAwait(false) is null ? back + "?ok=telegram-prova#telegram" : back + "?errore=telegram-prova#telegram");
|
||||
}
|
||||
|
||||
private static async Task<IResult> RemoveTelegramAsync(IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
const string back = "/impostazioni";
|
||||
if (host.IsDemo || host.Services is not { } services)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=motore#telegram");
|
||||
}
|
||||
|
||||
var who = context.User.Identity?.Name ?? "utente";
|
||||
await services.Secrets.RemoveAsync(Anubis.Notifications.TelegramSettings.TokenSecret, who, ct).ConfigureAwait(false);
|
||||
await services.Secrets.RemoveAsync(Anubis.Notifications.TelegramSettings.ChatSecret, who, ct).ConfigureAwait(false);
|
||||
return Results.Redirect(back + "?ok=telegram-tolto#telegram");
|
||||
}
|
||||
|
||||
/// <summary>The address of the checking service (SPEC 14): called once as a test, saved encrypted only when it answered.</summary>
|
||||
private static async Task<IResult> SaveHeartbeatAsync(IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
const string back = "/impostazioni";
|
||||
if (host.IsDemo || host.Services is not { Telegram: { ConfiguredHeartbeat: null } telegram } services)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=motore#telegram");
|
||||
}
|
||||
|
||||
var address = form["indirizzo"].ToString().Trim();
|
||||
if (address.Length is 0 or > 500 || Anubis.Notifications.Heartbeat.Check(address) is not null)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=segnale#telegram");
|
||||
}
|
||||
|
||||
var probe = new Anubis.Notifications.Heartbeat(telegram.Http, host.Clock, _ => Task.FromResult<Uri?>(new Uri(address)), Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance);
|
||||
if (!await probe.BeatAsync(ct).ConfigureAwait(false))
|
||||
{
|
||||
return Results.Redirect(back + "?errore=segnale-fallito#telegram");
|
||||
}
|
||||
|
||||
await services.Secrets.SetAsync(TelegramServices.HeartbeatSecret, address, context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
|
||||
await telegram.Heartbeat.BeatAsync(ct).ConfigureAwait(false);
|
||||
return Results.Redirect(back + "?ok=segnale#telegram");
|
||||
}
|
||||
|
||||
private static async Task<IResult> RemoveHeartbeatAsync(IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
const string back = "/impostazioni";
|
||||
if (host.IsDemo || host.Services is not { } services)
|
||||
{
|
||||
return Results.Redirect(back + "?errore=motore#telegram");
|
||||
}
|
||||
|
||||
await services.Secrets.RemoveAsync(TelegramServices.HeartbeatSecret, context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
|
||||
return Results.Redirect(back + "?ok=segnale-tolto#telegram");
|
||||
}
|
||||
|
||||
private static async Task<IResult> RemoveKeysAsync(string broker, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
|
||||
{
|
||||
const string back = "/impostazioni";
|
||||
|
||||
Reference in new issue
Block a user