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
@@ -195,7 +195,7 @@ internal static partial class Endpoints
|
||||
|
||||
api.MapPost("/bot/{command}", async (string command, AnubisHost host, HttpContext context, CancellationToken ct) =>
|
||||
{
|
||||
var (ok, message) = await BotCommandAsync(host, command, context, ct).ConfigureAwait(false);
|
||||
var (ok, message) = await BotCommandAsync(host, command, context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
|
||||
return ok ? Results.Json(new { state = message }, _json) : Error(StatusCodes.Status409Conflict, "comando_non_ammesso", message);
|
||||
});
|
||||
|
||||
@@ -255,7 +255,7 @@ internal static partial class Endpoints
|
||||
/// Pause blocks new entries and keeps stops and exits; "Ferma tutto" is the switch of SPEC 9.3 (its full effect
|
||||
/// arrives with the engine). Resume works only from a pause, never after "Ferma tutto" (SPEC 14).
|
||||
/// </summary>
|
||||
internal static async Task<(bool Ok, string Message)> BotCommandAsync(AnubisHost host, string command, HttpContext context, CancellationToken ct)
|
||||
internal static async Task<(bool Ok, string Message)> BotCommandAsync(AnubisHost host, string command, string by, CancellationToken ct)
|
||||
{
|
||||
var settings = host.Services!.Settings;
|
||||
var current = (await settings.GetAsync(SettingsStore.BotState.Key, ct).ConfigureAwait(false)).Value;
|
||||
@@ -280,7 +280,6 @@ internal static partial class Endpoints
|
||||
: "Comando non ammesso nello stato attuale del bot.");
|
||||
}
|
||||
|
||||
var by = context.User.Identity?.Name ?? "utente";
|
||||
if (host.Services.Trading is { Risk: { } risk } trading)
|
||||
{
|
||||
// The switch of SPEC 9.3 goes through the risk engine: it cancels the orders under way and keeps the stops.
|
||||
@@ -297,6 +296,7 @@ internal static partial class Endpoints
|
||||
try
|
||||
{
|
||||
await risk.RestartAsync(by, ct).ConfigureAwait(false);
|
||||
await NotifyBotAsync(host, "bot_restarted", $"Il bot è ripartito dopo «Ferma tutto» (da {by}).", ct).ConfigureAwait(false);
|
||||
return (true, target);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
@@ -307,9 +307,24 @@ internal static partial class Endpoints
|
||||
}
|
||||
|
||||
await settings.SetAsync(SettingsStore.BotState.Key, target, by, ct).ConfigureAwait(false);
|
||||
if (target != current)
|
||||
{
|
||||
// Blocks and unblocks reach the phone (SPEC 14), whoever gave the command.
|
||||
await NotifyBotAsync(host, "bot_" + target, target switch
|
||||
{
|
||||
"paused" => $"Bot in pausa (da {by}): niente nuovi acquisti, stop e uscite proseguono.",
|
||||
"active" => $"Bot ripartito dalla pausa (da {by}).",
|
||||
"stopped" => $"«Ferma tutto» (da {by}): niente nuove posizioni, gli stop restano. Si riparte solo dall'interfaccia.",
|
||||
_ => $"Stato del bot: {target} (da {by}).",
|
||||
}, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return (true, target);
|
||||
}
|
||||
|
||||
private static Task NotifyBotAsync(AnubisHost host, string code, string message, CancellationToken ct) =>
|
||||
host.Services!.Db.WriteAsync(s => Anubis.Trading.Diary.NotifyAsync(s, host.Clock, code, message, ct), ct);
|
||||
|
||||
// Live status (SPEC 13.4): one message on connect, one at every change and a heartbeat every few seconds, so the
|
||||
// page can tell how old its data are and warn when the link drops.
|
||||
private static async Task StatusSocketAsync(HttpContext context, AnubisHost host, ShellData shell, EventBus bus)
|
||||
|
||||
Reference in new issue
Block a user