Skip to content

How It Works

End-to-end flow from Discord message to accounting result.

Document Processing

  1. Message arrives on Discord #invoices (forwarded email receipt, invoice PDF, or text with photo)
  2. OpenClaw extracts merchant, amount, date, VAT from the document using the LLM
  3. OpenClaw calls the API with the extracted data (e.g., POST /receipt/attach)
  4. API does the work: checks Folio for matching transaction, attaches receipt, or registers in Fiken
  5. API returns result to OpenClaw (success, match details, or error)
  6. OpenClaw replies in a Discord thread on the original message

Queries

  1. User asks a question in Discord (e.g., "Hva er saldo?")
  2. OpenClaw determines the intent using the LLM
  3. OpenClaw calls the appropriate API endpoint (e.g., GET /folio/balance)
  4. API queries Folio/Fiken and returns data
  5. OpenClaw formats and posts the answer in Discord

Learned Patterns

  • API stores merchant-to-account/VAT mappings in Postgres
  • OpenClaw reads patterns via GET /patterns to provide context to the LLM
  • When a new merchant appears, OpenClaw asks the user, then calls POST /patterns/suggest
  • The suggest endpoint creates a PR to the gitops repo (not a direct DB write)
  • After PR merge, the pattern is available via GET /patterns

Proactive Checks

A cron job (k8s CronJob) calls the API's check endpoints daily:

  • GET /check/missing-receipts -- Folio transactions without attachments
  • GET /check/due-invoices -- purchases approaching due date
  • GET /check/overdue-invoices -- past due, not yet paid

Results are sent to OpenClaw, which posts reminders in Discord.

Configuration Changes

OpenClaw never writes config directly. For changes to patterns or company rules:

  1. OpenClaw creates a PR to the gitops repo via GitHub API
  2. Other agents or humans review the PR
  3. On merge, the new config is deployed via ArgoCD

Discord Thread Model

Every document gets its own thread. If clarification is needed, the conversation continues in the thread.

#invoices channel:
  [Email forward: Adobe invoice PDF]
    └── Thread:
        Bot: "Adobe 199 kr, 22. mars --> konto 6540 (programvare, 25% MVA)
              Lagt til pa Folio-transaksjon #evt-abc123"

  [Email forward: Unknown supplier invoice]
    └── Thread:
        Bot: "Ny leverandor: CloudFlare. Hvilken konto?
              Foreslar 6540 (skytjenester, 25% MVA)"
        You: "Ja"
        Bot: "Husker dette til neste gang. Registrert i Fiken,
              forfaller 15. april."