Repository navigation
feat(bin): canale Telegram opzionale per una home firstmate - #62
Merged
Merged
Conversation
added 2 commits
October 14, 2026 18:15
Inbound messages are long-polled by a process-event adapter (bin/fm-procevent-telegram.sh), filtered against config/telegram.json's allowlist, recorded durably under state/telegram/, and surfaced as captain-inbox channel notes marked trust=owner|untrusted and authority=data-only. bin/fm-telegram.sh adds status, discover, arm, retire, allowlisted plain-text send with splitting and send records, and list. The channel is inert unless .env holds TELEGRAM_BOT_TOKEN and the config is valid, and the token never reaches a process argv.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Obiettivo
Aggiungere a una home firstmate un canale Telegram opzionale, così che una home (il primo utente è un second mate sempre acceso che fa da assistente per gli sviluppatori in un gruppo Telegram interno) possa ricevere i messaggi indirizzati al proprio bot e rispondere in sicurezza.
Una home che non lo attiva non cambia comportamento in nessun modo.
Decisioni già prese: solo il gruppo interno e le chat in allowlist; il bot risponde da sé alle richieste che non toccano il codice; tutto ciò che scrive codice richiede la conferma del capitano; nessun merge e nessuna operazione di produzione via Telegram; nessun identificativo reale (nome del bot, id di chat o di utente) nel codice versionato.
Cosa cambia
.envdella home non contieneTELEGRAM_BOT_TOKENeconfig/telegram.jsonnon è valido (chat ammesse e id Telegram del proprietario; schema indocs/configuration.md). Il token si legge solo da.env, mai dall'ambiente, e arriva a curl tramite stdin: non compare mai negli argomenti di un processo, nei log o nell'output.bin/fm-procevent-telegram.sh, con la libreria condivisabin/fm-telegram-lib.sh). Il long-poll digetUpdatesgira come sorgente del meccanismo process-event già esistente, quindi nessun nuovo ciclo di supervisione. Ogni messaggio accettato viene salvato instate/telegram/messages/prima di confermare l'offset a Telegram e non viene mai riscritto: un riavvio non perde né duplica messaggi. Ogni messaggio arriva alla home come nota nell'inbox tramite la nuova voce idempotentefm-inbox.sh channel-note, conauthority=data-onlyetrust=untrusted, oppuretrust=ownerse scrive il proprietario. Gli errori transitori si ritentano dentro il poll; quelli definitivi (token revocato, webhook o altro lettore sullo stesso bot, attivazione rimossa) fermano la sorgente e avvisano una sola volta./comando@altrobot). I messaggi di altri bot, le modifiche, i messaggi di servizio e gli aggiornamenti malformati vengono ignorati senza bloccare l'offset.bin/fm-telegram.sh).sendrifiuta le chat fuori allowlist, invia testo semplice salvo--parse-mode, divide i testi lunghi su righe o parole contando le unità UTF-16, aggancia la risposta solo alla prima parte e registra ogni invio instate/telegram/sent/. Ci sono anchestatus,discover(trova gli id di chat e utenti in fase di configurazione senza consumare gli aggiornamenti),arm,retireelist.docs/configuration.mde una breve aggiunta alla sezione 14 diAGENTS.md). Una nota del canale non può spacciarsi per una nota del capitano.docs/telegram.md(BotFather, privacy mode, ingresso nei gruppi disattivato, allowlist, uso da parte di un second mate, note per host remoti), più tabella degli script, README, classificazione dei documenti, righe di layout inAGENTS.mde la gestione dell'avviso Telegram nella skillprocess-event-sources.Rischi
Basso. Il canale è inerte in ogni home che non lo attiva, e l'unico codice condiviso toccato è la nuova voce
channel-notedifm-inbox.sh, mentre le note del capitano restano invariate.La revisione ha trovato due difetti, già corretti nella pipeline: le parti di un messaggio diviso venivano scritte in Latin-1 e i caratteri accentati arrivavano corrotti; un errore dell'intero filtro poteva far ripetere il poll senza pausa.
Nulla è stato provato contro la vera API di Telegram: la prima prova reale sarà la configurazione del bot.
Verifiche
tests/fm-telegram.test.shcontro un'API Telegram simulata (curl finto, nessuna rete): canale spento senza attivazione,discover/arm/retire, filtro e marcatura del proprietario, uscita dai gruppi estranei, nessun doppione su riconsegna, note che non possono rivendicare l'autorità del capitano, ritiro su errore definitivo, invio con divisione e registrazione, testo italiano accentato.Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 2 issues found → auto-fixed ✅
bin/fm-telegram.sh:169- split_text decodes stdin as UTF-8 (perl -CS), but the part files it opens withopen my $fh, ">"have no :utf8 layer. A part whose characters all fall at or below U+00FF (for example Italian text such as "perché sì") is therefore written as Latin-1 bytes. I reproduced this: "perché" came out as the bytes 0x68 0xE9, not UTF-8.jq --rawfilethen reads invalid UTF-8 and replaces those characters with U+FFFD, so the group receives corrupted text. A part that also contains a character above U+00FF is written as UTF-8, but Perl prints a 'Wide character' warning. Fix: addbinmode $fh, ':encoding(UTF-8)'or open with>:encoding(UTF-8), and add a test case with non-ASCII text, which the suite does not have yet.bin/fm-procevent-telegram.sh:255- If the whole decide_updates jq run fails (for example, the config is changed to something the --argjson read cannot parse partway through a round), apply_updates sees no lines, does not advance the offset and returns empty. The poll then loops straight into the next getUpdates call with no sleep. The same pending updates come back at once, so the poller spins hard against the API. A small fix: treat a non-zero exit from decide_updates as a transient failure and back off with the delay.🔧 Fix: Write Telegram parts as UTF-8; back off on decide failure
✅ Re-checked - no issues remain.
bin/fm-procevent-telegram.sh- No test covers the new backoff when a decide_updates run fails, so there is no regression test showing that the offset stays the same and the poller waits before retrying.bash tests/fm-telegram.test.sh(mocked Telegram API via fake curl, no network)✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.