agentsautomation , 5 min read

My chat bridge kept reading my messages before I did

I built a self-hosted bridge so agents could read and answer my Zalo chats. Driving the real web app meant every read marked a chat as read and every send could double up. Five weeks in, sends moved to HTTP, and 'delivery unknown' became a real answer.

Most of my messages in Vietnam arrive on Zalo, including a few group chats where people ask me for help with things I run. I wanted my agents to read those chats, summarise what came in, and draft or send replies. Zalo has no API for a personal account. So on September 3, just before midnight, I started building one for myself: a small server in a container on the home cluster that holds my Zalo session and gives agents REST, a live event stream and MCP tools.

The repo has 84 commits. 57 of them landed on September 4.

Two ways in

The first version used zca-js, an unofficial JavaScript client for Zalo's web API. Log in with a QR code, get a session, call functions. It lasted nine hours. The QR login timed out after 70 seconds without ever reporting the scan, requests from inside the container timed out over IPv6 until Node was told to prefer IPv4, and a fresh web login doesn't download old conversations at all. An empty inbox wasn't much use to an agent.

At 08:46 on the 4th the project switched to running the real Zalo web app in a headless Firefox and reading it from the inside. Zalo's web client is a React app, so messages live in the component tree. The bridge walks React's internal Fiber structure to pull them out, takes about 2 ms to read a conversation, and listens to the page's WebSocket and HTTP traffic for anything new. Sends were typed into the page's own message box.

Finding where React kept each piece of data took 18 commits labelled debug: that day, ten of them in nine minutes while I hunted for the data behind quoted replies. Reply support shipped at 15:59.

A reader that changes what it reads

It worked, and it had a side effect I should have expected. To read a conversation in the web app, you open it. Opening a conversation marks it as read, the same as if I had looked at it myself. The bridge was doing its job, and every chat it read counted as read on my account before I had looked at it.

The fix on September 7 was to keep the browser away from conversations whenever it wasn't doing something specific. After every operation it parks on the chat with myself, Zalo's notes-to-self tab, and the background scanner that went looking for unread chats was removed. New messages still arrive, because the bridge picks them up from the network traffic without opening anything.

On September 16 I tried to remove the problem at the root. The browser already held a logged-in session, so the bridge handed that session to the zca-js HTTP client and read history over Zalo's API, which marks nothing as read. Twenty-three minutes later that path was mostly dead. Group history came back 404, and there was no endpoint for direct chats at all. So automatic reads now serve the stored copy and never open the browser, and there's a circuit breaker that backs off from 60 seconds to 15 minutes when the HTTP side fails.

Stored data that went bad

The same evening turned up a few problems that no schema check would have caught.

  • Zalo serves photos and files from signed CDN links that expire. I measured it: all six stored links older than 24 hours returned 404, and all four from that day returned 200. Every image the bridge had saved was going to break. Media now gets downloaded once, when the message arrives, and served from the bridge's own disk.
  • 14 rows in the database had the text "sendBubbleMessage" as their message body. That's the name of a function in Zalo's client, picked up from an action handler and stored as if someone had typed it.
  • Missed calls were showing up as shared contact cards, because both arrive with the same message type and the bridge guessed wrong when the details were missing.
  • Stickers came through as a blank attachment until the bridge learned their raw type number. Zalo's raw type numbers don't match the ones zca-js uses.

When you can't know if a message was sent

On October 12 I added an MCP tool to send images. Its first live send failed with "no picker found." The code clicked the photo button from inside the page and only then started waiting for the file picker, so the picker event fired before anything was listening. A click made from page script also doesn't count as a user action, which Firefox requires before it opens a file picker. The fix was to start listening first and click through Playwright.

37 minutes later came the bigger change, and the picker bug was the argument for it. Sending through the page breaks when Zalo changes its markup. It opens the conversation and marks it read. And it can only guess whether the message actually went out. So sends moved to HTTP first, through the same adopted session, with the browser as a fallback.

The rule for that fallback is the part I'd reuse anywhere. Falling back after a failed send is only safe if the first attempt provably posted nothing. Otherwise the fallback can send the message twice. So the bridge only falls back for errors that prove nothing went out:

export function isRetrySafeHttpError(err) {
  if (!err || err.name !== 'ZcaApiError') return false;
  if (err.code != null) return true;
  const message = err.message || '';
  const status = /status code (\d{3})/.exec(message);
  if (status) return status[1].startsWith('4');
  return PRE_REQUEST_ERRORS.some(re => re.test(message));
}

Zalo replied with an error code, the server returned a 4xx, or the client refused the input before sending anything. Those are safe. A timeout, a 5xx, or a 200 whose body the client couldn't parse all mean the message may well be sitting in the chat already. For those the bridge answers delivery: 'unknown' with retrySafe: false, and the agent has to look before it tries again.

Two smaller rules came with it. Identical text sends that are in flight at the same time share one request, so an agent that fires twice in a hurry produces one message. And a sent message is stored under Zalo's own message id, so when the same message comes back through the live traffic it merges with the stored one instead of appearing twice. Duplicates had come up before. On September 4 both Zalo's temporary local copy of a sent message and the confirmed copy got saved, and fixes on September 7 and 20 dealt with double submissions.

The repo's agent notes now start with a rule marked as one agents must follow: use Zalo's HTTP API when one exists, and drive the page only as a fallback. The fallback stays, because Zalo has already turned off some HTTP endpoints, group history among them. It took five weeks of side effects to write that rule down.

More writing

All posts