mirror of
https://github.com/anthropics/claude-plugins-official.git
synced 2026-04-19 15:12:42 +00:00
Compare commits
2 Commits
kenneth/te
...
kenneth/te
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d8042f259 | ||
|
|
1daff5f224 |
@@ -15,7 +15,7 @@ import {
|
|||||||
ListToolsRequestSchema,
|
ListToolsRequestSchema,
|
||||||
CallToolRequestSchema,
|
CallToolRequestSchema,
|
||||||
} from '@modelcontextprotocol/sdk/types.js'
|
} from '@modelcontextprotocol/sdk/types.js'
|
||||||
import { Bot, InputFile, type Context } from 'grammy'
|
import { Bot, GrammyError, InputFile, type Context } from 'grammy'
|
||||||
import type { ReactionTypeEmoji } from 'grammy/types'
|
import type { ReactionTypeEmoji } from 'grammy/types'
|
||||||
import { randomBytes } from 'crypto'
|
import { randomBytes } from 'crypto'
|
||||||
import { readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync, statSync, renameSync, realpathSync, chmodSync } from 'fs'
|
import { readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync, statSync, renameSync, realpathSync, chmodSync } from 'fs'
|
||||||
@@ -51,15 +51,6 @@ if (!TOKEN) {
|
|||||||
}
|
}
|
||||||
const INBOX_DIR = join(STATE_DIR, 'inbox')
|
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)
|
const bot = new Bot(TOKEN)
|
||||||
let botUsername = ''
|
let botUsername = ''
|
||||||
|
|
||||||
@@ -586,7 +577,7 @@ async function handleInbound(
|
|||||||
|
|
||||||
// image_path goes in meta only — an in-content "[image attached — read: PATH]"
|
// image_path goes in meta only — an in-content "[image attached — read: PATH]"
|
||||||
// annotation is forgeable by any allowlisted sender typing that string.
|
// annotation is forgeable by any allowlisted sender typing that string.
|
||||||
mcp.notification({
|
void mcp.notification({
|
||||||
method: 'notifications/claude/channel',
|
method: 'notifications/claude/channel',
|
||||||
params: {
|
params: {
|
||||||
content: text,
|
content: text,
|
||||||
@@ -599,25 +590,38 @@ async function handleInbound(
|
|||||||
...(imagePath ? { image_path: imagePath } : {}),
|
...(imagePath ? { image_path: imagePath } : {}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).catch(err => {
|
|
||||||
process.stderr.write(`telegram channel: failed to deliver inbound to Claude: ${err}\n`)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Without this, any throw in a message handler stops polling permanently
|
// 409 Conflict = another getUpdates consumer is still active (zombie from a
|
||||||
// (grammy's default error handler calls bot.stop() and rethrows).
|
// previous session, or a second Claude Code instance). Retry with backoff
|
||||||
bot.catch(err => {
|
// until the slot frees up instead of crashing on the first rejection.
|
||||||
process.stderr.write(`telegram channel: handler error (polling continues): ${err.error}\n`)
|
void (async () => {
|
||||||
})
|
for (let attempt = 1; ; attempt++) {
|
||||||
|
try {
|
||||||
bot.start({
|
await bot.start({
|
||||||
onStart: info => {
|
onStart: info => {
|
||||||
botUsername = info.username
|
botUsername = info.username
|
||||||
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
|
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
|
||||||
},
|
},
|
||||||
}).catch(err => {
|
})
|
||||||
// bot.start() only rejects if polling can't begin or dies unrecoverably —
|
return // bot.stop() was called — clean exit from the loop
|
||||||
// bad token, 409 conflict, network gone. Log it so the user isn't left
|
} catch (err) {
|
||||||
// wondering why messages stopped arriving.
|
if (err instanceof GrammyError && err.error_code === 409) {
|
||||||
process.stderr.write(`telegram channel: polling stopped: ${err}\n`)
|
const delay = Math.min(1000 * attempt, 15000)
|
||||||
})
|
const detail = attempt === 1
|
||||||
|
? ' — another instance is polling (zombie session, or a second Claude Code running?)'
|
||||||
|
: ''
|
||||||
|
process.stderr.write(
|
||||||
|
`telegram channel: 409 Conflict${detail}, retrying in ${delay / 1000}s\n`,
|
||||||
|
)
|
||||||
|
await new Promise(r => setTimeout(r, delay))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// bot.stop() mid-setup rejects with grammy's "Aborted delay" — expected, not an error.
|
||||||
|
if (err instanceof Error && err.message === 'Aborted delay') return
|
||||||
|
process.stderr.write(`telegram channel: polling failed: ${err}\n`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|||||||
Reference in New Issue
Block a user