<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anis Hammouche</title>
    <description>The latest articles on DEV Community by Anis Hammouche (@assinscreedfc).</description>
    <link>https://dev.to/assinscreedfc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2011629%2Fe9ff6c74-6a67-4fdf-8cb3-6e334f8df6f7.png</url>
      <title>DEV Community: Anis Hammouche</title>
      <link>https://dev.to/assinscreedfc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/assinscreedfc"/>
    <language>en</language>
    <item>
      <title>Producing Factur-X from a TypeScript app with a Python microservice</title>
      <dc:creator>Anis Hammouche</dc:creator>
      <pubDate>Sat, 05 Dec 2026 22:57:29 +0000</pubDate>
      <link>https://dev.to/assinscreedfc/producing-factur-x-from-a-typescript-app-with-a-python-microservice-n0g</link>
      <guid>https://dev.to/assinscreedfc/producing-factur-x-from-a-typescript-app-with-a-python-microservice-n0g</guid>
      <description>&lt;p&gt;Since 1 September 2026, every company in France has to be able to receive electronic invoices. Large companies and mid-size ones (ETI) must also issue them from that date. Small and medium businesses and micro-companies follow on 1 September 2027, always through an approved platform. I build Nolvi, an AGPL app for freelancers and agencies, and its invoices needed to be valid Factur-X files. Here is how that part works, and where it hurt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a second language
&lt;/h2&gt;

&lt;p&gt;Nolvi is TypeScript end to end, except one directory: &lt;code&gt;services/facturx/&lt;/code&gt;, a FastAPI service outside the pnpm workspace. The reason is practical. The &lt;code&gt;factur-x&lt;/code&gt; library (Akretion) and WeasyPrint, which can write PDF/A-3b directly, are Python. Using them means a second runtime and a second Docker image.&lt;/p&gt;

&lt;p&gt;A Factur-X file is a PDF/A-3 whose attachment is a structured invoice. The FNFE-MPE page lists five profiles (MINIMUM, BASIC WL, BASIC, EN 16931, EXTENDED) and says Factur-X is technically the same standard as ZUGFeRD. I target EN 16931 only, and the test suite asserts that the XMP says &lt;code&gt;EN 16931&lt;/code&gt; and never one of the others. The embedded XML is CII, the UN/CEFACT syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  The contract: no arithmetic in Python
&lt;/h2&gt;

&lt;p&gt;The NestJS side computes every amount once, with decimal.js, freezes it in a snapshot at issue time, and sends strings. The Python side never adds anything, and a test parses &lt;code&gt;cii.py&lt;/code&gt; to check that it contains no money arithmetic. The Pydantic model types money as a string with a regex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nc"&gt;Money&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StringConstraints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"^-?\d+\.\d{2}$"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;cii.py&lt;/code&gt; copies those strings into business terms: the per-rate base into BT-116, the HT total into BT-109, and so on. If the totals are wrong, the service does not repair them, it fails. A service that quietly recomputes can end up with an XML that disagrees with the PDF the customer reads.&lt;/p&gt;

&lt;p&gt;The tricky part is rounding. EN 16931 wants the sum of line totals to equal the HT total (rule BR-CO-10), and with several VAT rates and fractional unit prices, rounding each line on its own drifts by a cent. The fix is on the TypeScript side: each line receives the difference between two rounded running totals per rate group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;exact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unitPrice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;rounded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDecimalPlaces&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MONEY_SCALE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ROUND_HALF_UP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;exact&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rounded&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rounded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;minus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rounded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MONEY_SCALE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every line stays within a cent of its exact product and each group sums to its base. I found this out the hard way: an early version froze HT at 16.67 while the bases summed to 16.66.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pipeline
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;build_facturx&lt;/code&gt; runs three steps: build the CII, render the base PDF, embed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The base PDF comes from WeasyPrint 69 with &lt;code&gt;pdf_variant="pdf/a-3b"&lt;/code&gt; and no attachment. A Ghostscript fallback exists behind &lt;code&gt;FACTURX_BASE_ENGINE=ghostscript&lt;/code&gt;, in case WeasyPrint output ever fails the validator.&lt;/li&gt;
&lt;li&gt;The schematron runs on the XML before anything is embedded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;factur-x&lt;/code&gt; 7.10 embeds the XML:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;facturx_pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_from_binary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;base_pdfa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cii_xml&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;flavor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"factur-x"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"en16931"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;afrelationship&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Alternative"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;check_xsd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;check_schematron&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The library defaults to &lt;code&gt;Data&lt;/code&gt; for the relationship, so &lt;code&gt;Alternative&lt;/code&gt; is explicit.&lt;/p&gt;

&lt;h2&gt;
  
  
  The schematron that the library stopped running
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;check_schematron=False&lt;/code&gt; is deliberate. As the comment in &lt;code&gt;schematron.py&lt;/code&gt; explains, from factur-x 6 on the library posts the XML to an external Saxon HTTP server, and when none answers it logs a warning and carries on. A green build could mean the business rules never ran. So I run the XSLT schematron files shipped inside the package myself, in process, with &lt;code&gt;saxonche&lt;/code&gt;, and the gate fails closed: a Saxon error, a timeout, an empty report, or a report where no rule fired all raise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{{{SVRL_NAMESPACE}}}schematron-output"&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;".//{{{SVRL_NAMESPACE}}}fired-rule"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;SchematronError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Schematron '{check_type}' returned a report in which no rule fired"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Threading cost me the most time. SaxonC aborts the whole process when it is entered from a thread that did not initialise it, and FastAPI runs the build in a thread pool. All Saxon work therefore goes through one dedicated single-thread executor, and every Saxon object is created and released inside one call. A test fires eight calls from four threads at it. The service has to stay a single uvicorn process for this to hold.&lt;/p&gt;

&lt;h2&gt;
  
  
  What CI checks, and what runs in production
&lt;/h2&gt;

&lt;p&gt;A GitHub Actions job builds the Docker image (WeasyPrint libraries, Ghostscript, a Temurin 17 JRE, veraPDF 1.26.2 with a pinned SHA-256) and runs the whole pytest suite inside it. Debian's own OpenJDK crashed the veraPDF installer, hence Temurin.&lt;/p&gt;

&lt;p&gt;The suite generates 17 fixture documents (deposit invoices, final invoices after a deposit, credit notes, public-sector and foreign buyers) plus four VAT regime fixtures. For each file, the tests check that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;verapdf -f 3b&lt;/code&gt; reports the file compliant;&lt;/li&gt;
&lt;li&gt;the XML extracted back from the PDF passes the EN 16931 schematron;&lt;/li&gt;
&lt;li&gt;the XMP declares &lt;code&gt;EN 16931&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The documents with a French buyer also go through the French CTC schematron.&lt;/p&gt;

&lt;p&gt;None of this runs on a customer's invoice. At runtime the service runs the XSD check and the base schematron, and nothing else. veraPDF never sees a real invoice: it is a CI check on fixtures, like the French CTC rules. So a template regression would show up in CI, not in the service.&lt;/p&gt;

&lt;p&gt;The XMP was the other pain point. After the bump to factur-x 7.10, veraPDF rejected the metadata: &lt;code&gt;rdf:Description&lt;/code&gt; lacked &lt;code&gt;rdf:about=""&lt;/code&gt;, and &lt;code&gt;rdf:RDF&lt;/code&gt; carried no attribute at all, the namespace being declared only on its parent. &lt;code&gt;embed.py&lt;/code&gt; now rewrites the XMP packet with pypdf and lxml, and raises if the packet does not have the shape it expects. It is a workaround for a specific library version, which is pinned in &lt;code&gt;pyproject.toml&lt;/code&gt; for that reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Numbering
&lt;/h2&gt;

&lt;p&gt;Invoices are numbered &lt;code&gt;FA-2026-0001&lt;/code&gt;, credit notes &lt;code&gt;AV-2026-0001&lt;/code&gt;, one counter per organisation, year and series. The number is allocated when the invoice moves from draft to issued, never at draft time, inside the same transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$executeRaw&lt;/span&gt;&lt;span class="sb"&gt;`INSERT INTO "InvoiceCounter" ("id", "orgId", "year", "series", "last")
    VALUES (gen_random_uuid(), ${orgId}, ${year}, ${series}::"InvoiceSeries", 0)
    ON CONFLICT ("orgId", "year", "series") DO NOTHING`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$queryRaw&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;`SELECT "last" FROM "InvoiceCounter"
    WHERE "orgId" = ${orgId} AND "year" = ${year} AND "series" = ${series}::"InvoiceSeries"
    FOR UPDATE`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I chose a row lock over a Postgres &lt;code&gt;SEQUENCE&lt;/code&gt; because &lt;code&gt;nextval&lt;/code&gt; never rolls back, so a failed issue would leave a hole. One spec issues ten numbers in parallel and expects exactly 1 to 10. Another rolls a transaction back and expects the next call to return the same number. A gap-free chronological series is a requirement of my own design here; I did not cite the legal text, so check the rule with your accountant before relying on it.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;orgId&lt;/code&gt; in those raw queries is written out by hand, because the tenant wrapper does not scope raw SQL. Forgetting it would lock another tenant's counter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure handling
&lt;/h2&gt;

&lt;p&gt;The service is best effort. NestJS calls it after the issue transaction has committed, with a 10 second default timeout, so a timeout can never cancel a legally allocated number. A failure sets &lt;code&gt;facturxStatus&lt;/code&gt; to &lt;code&gt;FAILED&lt;/code&gt; with a fixed or local message, never the service's raw response. Regeneration replays the frozen snapshot, so it fixes outages, not bad data.&lt;/p&gt;

&lt;p&gt;The code is at &lt;a href="https://github.com/nolvihq/nolvi" rel="noopener noreferrer"&gt;https://github.com/nolvihq/nolvi&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>python</category>
      <category>opensource</category>
      <category>einvoicing</category>
    </item>
    <item>
      <title>How I monitor my Telegram userbots with zero infra</title>
      <dc:creator>Anis Hammouche</dc:creator>
      <pubDate>Tue, 09 Jun 2026 16:58:16 +0000</pubDate>
      <link>https://dev.to/assinscreedfc/how-i-monitor-my-telegram-userbots-with-zero-infra-34i9</link>
      <guid>https://dev.to/assinscreedfc/how-i-monitor-my-telegram-userbots-with-zero-infra-34i9</guid>
      <description>&lt;p&gt;I run a few Telegram bots and userbots in production. A couple of aiogram bots, a couple of Telethon userbots doing scraping work. For months I kept hitting the same two failures.&lt;br&gt;
First: a bot dies and I don't notice. The process crashes or hangs, and I find out hours later when someone tells me it stopped answering.&lt;br&gt;
Second, and worse: a userbot account gets restricted or banned. Telegram throttles it, or kills the session, and by the time I look the damage is done.&lt;br&gt;
The obvious answer is monitoring. But every option felt wrong for the size of the problem. Prometheus plus Grafana for two bots is a lot of moving parts to babysit. Hosted observability wants a server, an agent, a dashboard. I just wanted a Telegram message when something broke.&lt;br&gt;
So I built tgwatch.&lt;/p&gt;

&lt;p&gt;What it does&lt;br&gt;
You attach it to your bot or userbot in two lines. After that, capture is automatic:&lt;/p&gt;

&lt;p&gt;alive or dead state, based on a heartbeat&lt;br&gt;
message, error, and FloodWait counters&lt;br&gt;
userbot account health: restricted, banned, or dead session&lt;/p&gt;

&lt;p&gt;When something degrades, a separate watchdog process sends you a Telegram alert.&lt;br&gt;
from tgwatch import Watch&lt;/p&gt;

&lt;p&gt;watch = Watch(&lt;br&gt;
    storage="tgwatch.db",&lt;br&gt;
    alert_bot_token="123456:abc...",   # a Telegram bot dedicated to alerts&lt;br&gt;
    alert_chat_id="@my_alerts",&lt;br&gt;
)&lt;/p&gt;

&lt;h1&gt;
  
  
  Telethon userbot
&lt;/h1&gt;

&lt;p&gt;watch.attach(telethon_client, kind="userbot", name="scraper")&lt;/p&gt;

&lt;h1&gt;
  
  
  aiogram bot
&lt;/h1&gt;

&lt;p&gt;watch.attach(dispatcher, kind="bot", name="support_bot")&lt;br&gt;
After attach, you write no more monitoring code. Your bot behaves exactly as before. Exceptions are recorded, then re-raised, so nothing changes about how your code runs.&lt;/p&gt;

&lt;p&gt;The part I couldn't find elsewhere&lt;br&gt;
Knowing a process is up is easy. Knowing a userbot account is healthy is not.&lt;br&gt;
A Telethon userbot can be technically running while its account is restricted or already banned. The process looks fine. The account is dead.&lt;br&gt;
tgwatch classifies account state from two sources.&lt;br&gt;
Passive layer: it reads the Telethon exceptions your code already throws.&lt;/p&gt;

&lt;p&gt;ExceptionState&lt;br&gt;
PeerFloodErrorrestricted&lt;br&gt;
PhoneNumberBannedError, UserDeactivatedBanErrorbanned&lt;br&gt;
AuthKeyUnregisteredErrordead session&lt;br&gt;
FloodWaitErrorfloodwait event (slowed down, not a degraded state by itself)&lt;/p&gt;

&lt;p&gt;Active layer: a periodic get_me() probe that reads the account's restricted flag.&lt;br&gt;
The two signals merge with a worst-state-wins rule: banned beats dead session beats restricted beats healthy. A "banned" signal is never overwritten by a later unconfirmed "healthy". So once tgwatch sees a ban, it stays loud.&lt;/p&gt;

&lt;p&gt;Why a separate process&lt;br&gt;
A dead process can't alert you about itself. That sounds obvious, and it's the reason the watchdog runs on its own.&lt;br&gt;
tgwatch watch --storage tgwatch.db --token 123456:abc... --chat-id @my_alerts&lt;br&gt;
The watchdog reads the shared SQLite file, detects degraded states and missed heartbeats, and sends the alert. Your bot writes state into SQLite. The watchdog reads it. If the bot dies, the watchdog is still there to notice.&lt;br&gt;
You can also check state by hand:&lt;br&gt;
tgwatch status --storage tgwatch.db&lt;br&gt;
NAME          KIND     STATUS  HEALTH      MSG   ERROR  FLOODWAIT&lt;br&gt;
scraper       userbot  up      restricted  842   3      1&lt;br&gt;
support_bot   bot      down    n/a         1503  12     0&lt;/p&gt;

&lt;p&gt;Design choices I stand by&lt;br&gt;
Zero runtime dependencies in the base install. The core uses only the standard library: sqlite3, urllib, argparse. aiogram and telethon are optional extras, imported lazily. If you only run bots you never install Telethon, and the reverse.&lt;br&gt;
One direction of dependencies. There's a core layer that knows nothing about Telegram. Adapters depend on core, never the other way. SQLite runs in WAL mode, so the bot process and the watchdog process share the file safely.&lt;br&gt;
Secrets never touch the database or the logs. Bot token, session string, phone number: all masked at the recorder, with sensitive keys redacted and a regex for bot-token shapes. I didn't want a monitoring tool to become the thing that leaks the credentials it's supposed to protect.&lt;br&gt;
Alerting fails soft. Sending an alert retries with backoff, then logs and gives up. It never raises into the bot you're monitoring. A monitoring tool that crashes your bot is worse than no monitoring.&lt;br&gt;
The whole thing has 241 tests at 96% coverage and runs fully offline.&lt;/p&gt;

&lt;p&gt;Where it's at&lt;br&gt;
tgwatch is at 0.1.0. It does what I needed, and I'm running it on my own bots now.&lt;br&gt;
pip install "tgwatch[telethon,aiogram]"&lt;br&gt;
Repo: &lt;a href="https://github.com/assinscreedFC/tgwatch" rel="noopener noreferrer"&gt;https://github.com/assinscreedFC/tgwatch&lt;/a&gt;&lt;br&gt;
What I'd like feedback on: the account-health signals. Telethon throws a lot of exceptions, and I mapped the ones that bit me. If you've seen account states I'm not catching, tell me.&lt;/p&gt;




&lt;p&gt;Built and maintained by &lt;a href="https://solidscale.tech" rel="noopener noreferrer"&gt;SolidScale&lt;/a&gt;, the applied-AI studio for European companies. I open-source the tools I build.&lt;/p&gt;

</description>
      <category>python</category>
      <category>telegram</category>
      <category>opensource</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
