Learn ← All posts
Post

Publishing from chat

discord · bot · automation

The bot that publishes posts is the same code the site runs, imported rather than copied.

One Discord bot does three jobs: it can open a conversation thread, it reports machine status, and it publishes posts to this site.

Why a bot at all

Publishing from a phone should not mean opening an editor. If a sentence arrives while I'm walking, I want to type it, send, and have it online before I forget it. That is the entire requirement.

One implementation, not two

The blog logic lives next to the site. The bot adds that directory to sys.path and imports the same module the site serves from:

[bot]  --import-->  site/bloglib.py
                        |
                        +-- public/learn/<slug>/index.html
                        +-- posts.json
                        +-- the list page
                        +-- sitemap.xml

That matters more than it looks. A copy would drift the first time the page template changed, and the drift would stay invisible until a post rendered wrong. Nothing has to be kept in sync because there is only one thing.

Commands

  • /post — title, body, tags
  • /posts — the last ten posts
  • /post-delete — by slug or title
  • /status — ping, CPU, memory and disk for the machines I watch
  • /server add|remove|list — which machines those are

Publishing is limited to my user ID. The token lives in a 0600 file, never in the code. It runs as a systemd service with a restart policy, and on login it writes a small readiness file — time, guild, registered commands, post count — so "is it actually up?" is one cat away instead of a guess.

Constraints

No external paid APIs. Standard library plus a Discord library, and the only outbound socket it opens is to Discord. A bot that needs a third party's service to post a paragraph stops working the day someone else's bill comes due.