Compare commits

..

1 Commits

Author SHA1 Message Date
Kenneth Lien
9f2a4feab9 telegram: add error handlers to stop silent polling death
The bot would silently stop delivering messages after the first error:
grammy's default handler calls bot.stop() on any middleware throw, and
void bot.start() / void mcp.notification() swallow rejections with no log.

- bot.catch(): log and keep polling on handler errors
- bot.start().catch(): log when polling dies (bad token, 409, network)
- mcp.notification().catch(): log when inbound delivery to Claude fails
- process-level unhandledRejection/uncaughtException as a safety net

Fixes #756 #759 #761 #777 #809, partial #788
2026-03-20 10:53:36 -07:00
6 changed files with 42 additions and 48 deletions

View File

@@ -1,20 +1,11 @@
{
"name": "discord",
"description": "Discord channel for Claude Code messaging bridge with built-in access control. Manage pairing, allowlists, and policy via /discord:access.",
"version": "0.0.2",
"description": "Discord channel for Claude Code \u2014 messaging bridge with built-in access control. Manage pairing, allowlists, and policy via /discord:access.",
"version": "0.0.1",
"keywords": [
"discord",
"messaging",
"channel",
"mcp"
],
"userConfig": {
"DISCORD_BOT_TOKEN": {
"type": "string",
"title": "Bot Token",
"description": "Bot token from the Discord Developer Portal. Stored in keychain (macOS) or ~/.claude/.credentials.json with 0600 permissions elsewhere. Never written to settings.json.",
"required": true,
"sensitive": true
}
}
]
}

View File

@@ -2,10 +2,7 @@
"mcpServers": {
"discord": {
"command": "bun",
"args": ["run", "--cwd", "${CLAUDE_PLUGIN_ROOT}", "--shell=bun", "--silent", "start"],
"env": {
"DISCORD_BOT_TOKEN": "${user_config.DISCORD_BOT_TOKEN}"
}
"args": ["run", "--cwd", "${CLAUDE_PLUGIN_ROOT}", "--shell=bun", "--silent", "start"]
}
}
}

View File

@@ -34,12 +34,10 @@ const ACCESS_FILE = join(STATE_DIR, 'access.json')
const APPROVED_DIR = join(STATE_DIR, 'approved')
const ENV_FILE = join(STATE_DIR, '.env')
// Token is injected via ${user_config.DISCORD_BOT_TOKEN} from .mcp.json —
// prompted at enable time, stored in keychain (macOS) or .credentials.json 0600
// elsewhere. The .env file below is a legacy fallback for users configured
// before H1 #3617646 — real env wins, so the injected value takes precedence.
// Load ~/.claude/channels/discord/.env into process.env. Real env wins.
// Plugin-spawned servers don't get an env block — this is where the token lives.
try {
// Defensive chmod for legacy .env files (no-op on Windows).
// Token is a credential — lock to owner. No-op on Windows (would need ACLs).
chmodSync(ENV_FILE, 0o600)
for (const line of readFileSync(ENV_FILE, 'utf8').split('\n')) {
const m = line.match(/^(\w+)=(.*)$/)
@@ -53,8 +51,8 @@ const STATIC = process.env.DISCORD_ACCESS_MODE === 'static'
if (!TOKEN) {
process.stderr.write(
`discord channel: DISCORD_BOT_TOKEN required\n` +
` re-enter via: /plugin manage → discord → Configure options\n` +
` (stored in keychain/credentials.json, not settings.json)\n`,
` set in ${ENV_FILE}\n` +
` format: DISCORD_BOT_TOKEN=MTIz...\n`,
)
process.exit(1)
}

View File

@@ -1,20 +1,11 @@
{
"name": "telegram",
"description": "Telegram channel for Claude Code messaging bridge with built-in access control. Manage pairing, allowlists, and policy via /telegram:access.",
"version": "0.0.2",
"description": "Telegram channel for Claude Code \u2014 messaging bridge with built-in access control. Manage pairing, allowlists, and policy via /telegram:access.",
"version": "0.0.1",
"keywords": [
"telegram",
"messaging",
"channel",
"mcp"
],
"userConfig": {
"TELEGRAM_BOT_TOKEN": {
"type": "string",
"title": "Bot Token",
"description": "Bot token from @BotFather — format is 123456789:AAH... Stored in keychain (macOS) or ~/.claude/.credentials.json with 0600 permissions elsewhere. Never written to settings.json.",
"required": true,
"sensitive": true
}
}
]
}

View File

@@ -2,10 +2,7 @@
"mcpServers": {
"telegram": {
"command": "bun",
"args": ["run", "--cwd", "${CLAUDE_PLUGIN_ROOT}", "--shell=bun", "--silent", "start"],
"env": {
"TELEGRAM_BOT_TOKEN": "${user_config.TELEGRAM_BOT_TOKEN}"
}
"args": ["run", "--cwd", "${CLAUDE_PLUGIN_ROOT}", "--shell=bun", "--silent", "start"]
}
}
}

View File

@@ -27,12 +27,10 @@ const ACCESS_FILE = join(STATE_DIR, 'access.json')
const APPROVED_DIR = join(STATE_DIR, 'approved')
const ENV_FILE = join(STATE_DIR, '.env')
// Token is injected via ${user_config.TELEGRAM_BOT_TOKEN} from .mcp.json —
// prompted at enable time, stored in keychain (macOS) or .credentials.json 0600
// elsewhere. The .env file below is a legacy fallback for users configured
// before H1 #3617646 — real env wins, so the injected value takes precedence.
// Load ~/.claude/channels/telegram/.env into process.env. Real env wins.
// Plugin-spawned servers don't get an env block — this is where the token lives.
try {
// Defensive chmod for legacy .env files (no-op on Windows).
// Token is a credential — lock to owner. No-op on Windows (would need ACLs).
chmodSync(ENV_FILE, 0o600)
for (const line of readFileSync(ENV_FILE, 'utf8').split('\n')) {
const m = line.match(/^(\w+)=(.*)$/)
@@ -46,13 +44,22 @@ const STATIC = process.env.TELEGRAM_ACCESS_MODE === 'static'
if (!TOKEN) {
process.stderr.write(
`telegram channel: TELEGRAM_BOT_TOKEN required\n` +
` re-enter via: /plugin manage → telegram → Configure options\n` +
` (stored in keychain/credentials.json, not settings.json)\n`,
` set in ${ENV_FILE}\n` +
` format: TELEGRAM_BOT_TOKEN=123456789:AAH...\n`,
)
process.exit(1)
}
const INBOX_DIR = join(STATE_DIR, 'inbox')
// Last-resort safety net — without these the process dies silently on any
// unhandled promise rejection. With them it logs and keeps serving tools.
process.on('unhandledRejection', err => {
process.stderr.write(`telegram channel: unhandled rejection: ${err}\n`)
})
process.on('uncaughtException', err => {
process.stderr.write(`telegram channel: uncaught exception: ${err}\n`)
})
const bot = new Bot(TOKEN)
let botUsername = ''
@@ -579,7 +586,7 @@ async function handleInbound(
// image_path goes in meta only — an in-content "[image attached — read: PATH]"
// annotation is forgeable by any allowlisted sender typing that string.
void mcp.notification({
mcp.notification({
method: 'notifications/claude/channel',
params: {
content: text,
@@ -592,12 +599,25 @@ async function handleInbound(
...(imagePath ? { image_path: imagePath } : {}),
},
},
}).catch(err => {
process.stderr.write(`telegram channel: failed to deliver inbound to Claude: ${err}\n`)
})
}
void bot.start({
// Without this, any throw in a message handler stops polling permanently
// (grammy's default error handler calls bot.stop() and rethrows).
bot.catch(err => {
process.stderr.write(`telegram channel: handler error (polling continues): ${err.error}\n`)
})
bot.start({
onStart: info => {
botUsername = info.username
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
},
}).catch(err => {
// bot.start() only rejects if polling can't begin or dies unrecoverably —
// bad token, 409 conflict, network gone. Log it so the user isn't left
// wondering why messages stopped arriving.
process.stderr.write(`telegram channel: polling stopped: ${err}\n`)
})