Skip to content

examples: add channel-bridge — bidirectional cross-channel message forwarding#15

Merged
LinekForge merged 2 commits into
LinekForge:mainfrom
jamiekya:examples/channel-bridge
May 2, 2026
Merged

examples: add channel-bridge — bidirectional cross-channel message forwarding#15
LinekForge merged 2 commits into
LinekForge:mainfrom
jamiekya:examples/channel-bridge

Conversation

@jamiekya
Copy link
Copy Markdown
Contributor

@jamiekya jamiekya commented May 2, 2026

关联 Issue

closes #14

做了什么

新增 examples/channel-bridge.ts:一个基于 Hub WebSocket API 的独立跨通道消息桥脚本。

功能:

  • 通道 A 的指定联系人发消息 → 自动加前缀转发到通道 B
  • 通道 B 回复时,以配置的触发词(如 @Alice)开头 → 去掉触发词后转发回通道 A

适用场景示例:

  • Telegram 联系人的消息实时出现在微信对话里
  • 微信里用 @Alice 开头回复 → 对方在 Telegram 收到

文件说明

文件 说明
examples/channel-bridge.ts 桥接主脚本,修改 CONFIG 配置项后直接 bun run
examples/channel-bridge.plist macOS launchd 模板,用于后台持久运行

使用方式

# 1. 修改脚本顶部的 CONFIG(填入实际的 sender_id)
# 2. 直接运行
bun run examples/channel-bridge.ts

# 3. 后台持久运行(macOS)
cp examples/channel-bridge.plist ~/Library/LaunchAgents/com.forge.channel-bridge.plist
# 修改 plist 里的脚本路径
launchctl load ~/Library/LaunchAgents/com.forge.channel-bridge.plist

测试

在 macOS 本地测试了 Telegram ↔ WeChat 双向转发,链路正常。

🤖 Generated with Claude Code

…rwarding

Adds a standalone Bun script that bridges messages between any two Hub
channels using the WebSocket API:

- Channel A contact → forwarded to Channel B with configurable prefix
- Channel B reply with trigger prefix (@name) → forwarded back to Channel A

Typical use case: Telegram contact's messages appear in WeChat in real time;
WeChat user replies with @name to send back.

Includes a launchd plist template for persistent background operation on macOS.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@LinekForge
Copy link
Copy Markdown
Owner

Hey @jamiekya — thanks for this contribution! Really cool idea. Cross-channel bridging wasn't something we had on our radar at all, so it's great to see the community pushing the boundaries of what Hub can do. 🎉

The code is clean and well-documented — nice work. A few things we'd like addressed before merging:

1. Auth support

The script doesn't include HUB_API_TOKEN handling. Users who've configured token auth (recommended in our security docs) won't be able to connect. Could you add:

  • WebSocket URL: append &token=<TOKEN> query param (same pattern as hub-channel.ts)
  • /send fetch: add Authorization: Bearer <TOKEN> header
  • Read from env: process.env.HUB_API_TOKEN or a CONFIG field

2. Proxy bypass

We recently fixed an issue (#13) where system HTTP proxies (http_proxy env var) hijack localhost requests. Adding this at the top of the script would keep it consistent:

if (!process.env.NO_PROXY) process.env.NO_PROXY = "127.0.0.1,localhost";

3. Reconnect backoff

The current fixed 5s reconnect delay works, but exponential backoff would be more robust for longer outages (e.g. Hub restart taking a while):

// Something like: 5s → 10s → 20s → cap at 60s, reset on successful connect

Nothing fancy needed — a simple multiply-and-cap in the onclose handler would do.


Happy to help if you have any questions on these. Looking forward to merging this!

— Forge (maintainer)

- Auth: read HUB_API_TOKEN from env or ~/.forge-hub/api-token file,
  pass as Bearer header on /send and ?token= on WebSocket connect
- Proxy: set NO_PROXY=127.0.0.1,localhost to bypass system HTTP proxy
- Reconnect: replace fixed 5s delay with exponential backoff
  (5s → 10s → 20s → cap 60s, resets to 5s on successful connect)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Owner

@LinekForge LinekForge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three points addressed cleanly. LGTM — merging.

Thanks for the quick turnaround @jamiekya!

@LinekForge LinekForge merged commit f4e502b into LinekForge:main May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: 内置跨通道消息转发规则(Channel Bridge)

2 participants