Repository navigation
channels: LINE bot — webhook receiver mounted on the gateway (Wave 1.5a) - #76
Conversation
First two-way IM bot beyond Telegram. picoclaw's `line` channel
brought into PasClaw by adding three things at once:
1. A gateway "mount webhook" API. TGatewayServer.MountWebhook(Path,
Handler) registers an exact-match POST route that runs Handler
(procedure(AContext; ARequest; AResponse) of object) when a
client posts to it. DispatchWebhook walks the registry from
OnCommandGet just before the 404 fall-through, so the channel
reuses the same Indy listener PasClaw already runs for /v1 —
no second TCP port, no second TLS config, no second auth shape.
2. A pure-Pascal SHA-256 + HMAC-SHA-256 in
src/pkg/crypto/PasClaw.Crypto.HMAC.pas. Indy's TIdHMACSHA256
routes through OpenSSL's EVP_sha256, and the OpenSSL 3 export
shape is flaky to detect at load time (Indy's IsAvailable
returned False even with OpenSSL 3.0 fully loaded). Doing the
primitive ourselves removes the whole class of failure mode and
runs identical under FPC ({$MODE DELPHI}) and Delphi 12 — no
native deps, no compile-time hash unit picker. Includes a
constant-time string equality helper since signature compares
must not early-out on the first byte mismatch.
Verified against openssl: same hex + base64 output for the
RFC HMAC-SHA-256("key", "The quick brown fox jumps over the
lazy dog") test vector.
3. TLineBot in PasClaw.Channels.LINE alongside the existing
TLinePush. The bot holds Cfg / Provider / Registry directly
(mirrors TTelegramChannel — no callback indirection, no
abstract bridge) and exposes HandleWebhook(AContext; ARequest;
AResponse) which:
- reads the raw request body (signature is over bytes, not
the parsed JSON);
- verifies X-Line-Signature with constant-time-equal HMAC
against the channel secret;
- returns 401 with {"error":"signature"} on mismatch;
- returns 200 immediately so LINE doesn't retry on internal
errors (their retry storms duplicate replies);
- parses each event, runs RunToolLoop with the user text;
- replies via /v2/bot/message/reply using the one-shot
replyToken (free + fast inside ~30 s) and falls back to
/v2/bot/message/push when the reply token has already
expired because the tool loop ran long.
Gateway wiring:
pasclaw gateway --line # default ports
pasclaw gateway --line --line-token X --line-secret Y
$PASCLAW_LINE_TOKEN / $PASCLAW_LINE_SECRET as the env fallback.
The user-facing console banner now also prints
POST http://.../webhooks/line (LINE platform)
when --line is on, so the user can copy the URL straight into LINE
Developers Console.
Build:
Makefile UNIT_DIRS gains src/pkg/crypto; .dproj DCCReferences
gain PasClaw.Crypto.HMAC and the unit search path gets the new
..\pkg\crypto entry. README's env-var table picks up LINE_TOKEN
/ LINE_SECRET, the gateway example block grows a --line line.
Verified locally:
make builds clean (FPC 3.2.2).
hmac smoke vs openssl: hex + base64 + empty-input + 4
constant-time equality cases all pass.
pasclaw --help and pasclaw gateway plain (no --line) behave
byte-identically to pre-change.
Wave 1.5b (WhatsApp Cloud receiver) lands next on top of this same
MountWebhook API plus the HMAC helper.
https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f049f9d3d1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| EvObj := Events.ItemObject(i); | ||
| if EvObj = nil then Continue; | ||
| try | ||
| ProcessEvent(EvObj.ToJSON); |
There was a problem hiding this comment.
Acknowledge LINE webhooks before running the agent
When a LINE event triggers a slow model/tool call, this synchronous ProcessEvent call keeps the HTTP response unsent until RunToolLoop and the reply/push API call finish; the ResponseNo := 200 set above is not actually flushed until HandleWebhook returns. In those slow cases LINE treats the delivery as timed out and retries the same event, so this handler can run the agent multiple times and send duplicate replies despite the comment about avoiding retries. Consider returning 200 before the expensive work or deduplicating/enqueuing webhook events.
Useful? React with 👍 / 👎.
PR #76 review fix: ack webhook 200 before running the agent (LINE + WhatsApp)
First two-way IM bot beyond Telegram. PR #75 shipped LINE push; this PR closes the loop with a webhook receiver that runs the agent and replies. Three things landed together because all three are needed for any future webhook-based channel (WhatsApp Cloud is next).
1. Gateway "mount webhook" API
TGatewayServer.MountWebhook(Path, Handler)registers an exact-match POST route. The newDispatchWebhookis called fromOnCommandGetjust before the 404 fall-through, so the channel piggybacks on the same Indy listener PasClaw already runs for/v1— no second TCP port, no second TLS config, no second auth scheme.Handlers own their own response — signature check, parse, run the agent loop, write the reply. The dispatcher does not authenticate on their behalf.
2. Pure-Pascal SHA-256 + HMAC-SHA-256
src/pkg/crypto/PasClaw.Crypto.HMAC.pas. Indy'sTIdHMACSHA256routes through OpenSSL'sEVP_sha256and OpenSSL 3's export layout is flaky to detect at load time — Indy'sIsAvailablereturnedFalseeven withlibssl.so.3fully loaded. Doing the primitive ourselves removes the whole class of failure mode and runs identical under FPC{$MODE DELPHI}and Delphi 12. Zero native deps, zero compile-time hash unit picker.Verified against
openssl dgst -sha256 -hmac:Includes
ConstantTimeEqualfor signature compares (never=— early-out leaks one byte at a time).3.
TLineBotSits alongside the existing
TLinePushinPasClaw.Channels.LINE. Holds Cfg / Provider / Registry directly (mirrorsTTelegramChannel— no callback indirection, no abstract bridge).HandleWebhook:X-Line-SignatureviaConstantTimeEqual(HMACSHA256Base64(secret, body), header).401 {"error":"signature"}on mismatch.200immediately so LINE doesn't retry on internal errors (their retry storms duplicate replies).type=message+message.type=text, runsRunToolLoopwith the user text./v2/bot/message/replyusing the one-shotreplyToken(free + fast inside ~30 s) and falls back to/v2/bot/message/pushwhen the token expired because the tool loop took longer.Source ID picks
userId/groupId/roomIdin order — fallback push works for any chat type.CLI
Gateway banner prints
POST http://…/webhooks/line (LINE platform)when--lineis on, so the URL is copy-pasteable straight into LINE Developers Console.Build
MakefileUNIT_DIRSgainssrc/pkg/crypto.PasClaw.dprojDCC_UnitSearchPathgains..\pkg\crypto;DCCReferenceentry forPasClaw.Crypto.HMAC.pas.LINE_TOKEN/LINE_SECRET; gateway example block grows--line.Verification
make— clean build under FPC 3.2.2.openssl dgst -sha256 -hmac: RFC test vector hex + base64, empty-input vector, 4 constant-time equality cases — all 7 pass.pasclaw --helpandpasclaw gateway(without--line) byte-identical to pre-change.Wave 1.5b ahead
WhatsApp Cloud receiver lands next on top of this same
MountWebhookAPI and HMAC helper. WhatsApp adds a GET subscription-verification handler (hub.challenge echo) and usesX-Hub-Signature-256with hex-encoded HMAC +sha256=prefix; the HMAC primitive already supports both encodings.https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Generated by Claude Code