mirror of
https://github.com/anthropics/claude-plugins-official.git
synced 2026-03-20 23:43:07 +00:00
Compare commits
1 Commits
kenneth/te
...
kenneth/te
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7cb39c269 |
@@ -15,7 +15,7 @@ import {
|
|||||||
ListToolsRequestSchema,
|
ListToolsRequestSchema,
|
||||||
CallToolRequestSchema,
|
CallToolRequestSchema,
|
||||||
} from '@modelcontextprotocol/sdk/types.js'
|
} from '@modelcontextprotocol/sdk/types.js'
|
||||||
import { Bot, GrammyError, InputFile, type Context } from 'grammy'
|
import { Bot, 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'
|
||||||
@@ -372,6 +372,11 @@ mcp.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|||||||
items: { type: 'string' },
|
items: { type: 'string' },
|
||||||
description: 'Absolute file paths to attach. Images send as photos (inline preview); other types as documents. Max 50MB each.',
|
description: 'Absolute file paths to attach. Images send as photos (inline preview); other types as documents. Max 50MB each.',
|
||||||
},
|
},
|
||||||
|
format: {
|
||||||
|
type: 'string',
|
||||||
|
enum: ['text', 'markdownv2'],
|
||||||
|
description: "Rendering mode. 'markdownv2' enables Telegram formatting (bold, italic, code, links). Caller must escape special chars per MarkdownV2 rules. Default: 'text' (plain, no escaping needed).",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
required: ['chat_id', 'text'],
|
required: ['chat_id', 'text'],
|
||||||
},
|
},
|
||||||
@@ -398,6 +403,11 @@ mcp.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|||||||
chat_id: { type: 'string' },
|
chat_id: { type: 'string' },
|
||||||
message_id: { type: 'string' },
|
message_id: { type: 'string' },
|
||||||
text: { type: 'string' },
|
text: { type: 'string' },
|
||||||
|
format: {
|
||||||
|
type: 'string',
|
||||||
|
enum: ['text', 'markdownv2'],
|
||||||
|
description: "Rendering mode. 'markdownv2' enables Telegram formatting (bold, italic, code, links). Caller must escape special chars per MarkdownV2 rules. Default: 'text' (plain, no escaping needed).",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
required: ['chat_id', 'message_id', 'text'],
|
required: ['chat_id', 'message_id', 'text'],
|
||||||
},
|
},
|
||||||
@@ -414,6 +424,8 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => {
|
|||||||
const text = args.text as string
|
const text = args.text as string
|
||||||
const reply_to = args.reply_to != null ? Number(args.reply_to) : undefined
|
const reply_to = args.reply_to != null ? Number(args.reply_to) : undefined
|
||||||
const files = (args.files as string[] | undefined) ?? []
|
const files = (args.files as string[] | undefined) ?? []
|
||||||
|
const format = (args.format as string | undefined) ?? 'text'
|
||||||
|
const parseMode = format === 'markdownv2' ? 'MarkdownV2' as const : undefined
|
||||||
|
|
||||||
assertAllowedChat(chat_id)
|
assertAllowedChat(chat_id)
|
||||||
|
|
||||||
@@ -440,6 +452,7 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => {
|
|||||||
(replyMode === 'all' || i === 0)
|
(replyMode === 'all' || i === 0)
|
||||||
const sent = await bot.api.sendMessage(chat_id, chunks[i], {
|
const sent = await bot.api.sendMessage(chat_id, chunks[i], {
|
||||||
...(shouldReplyTo ? { reply_parameters: { message_id: reply_to } } : {}),
|
...(shouldReplyTo ? { reply_parameters: { message_id: reply_to } } : {}),
|
||||||
|
...(parseMode ? { parse_mode: parseMode } : {}),
|
||||||
})
|
})
|
||||||
sentIds.push(sent.message_id)
|
sentIds.push(sent.message_id)
|
||||||
}
|
}
|
||||||
@@ -482,10 +495,13 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => {
|
|||||||
}
|
}
|
||||||
case 'edit_message': {
|
case 'edit_message': {
|
||||||
assertAllowedChat(args.chat_id as string)
|
assertAllowedChat(args.chat_id as string)
|
||||||
|
const editFormat = (args.format as string | undefined) ?? 'text'
|
||||||
|
const editParseMode = editFormat === 'markdownv2' ? 'MarkdownV2' as const : undefined
|
||||||
const edited = await bot.api.editMessageText(
|
const edited = await bot.api.editMessageText(
|
||||||
args.chat_id as string,
|
args.chat_id as string,
|
||||||
Number(args.message_id),
|
Number(args.message_id),
|
||||||
args.text as string,
|
args.text as string,
|
||||||
|
...(editParseMode ? [{ parse_mode: editParseMode }] : []),
|
||||||
)
|
)
|
||||||
const id = typeof edited === 'object' ? edited.message_id : args.message_id
|
const id = typeof edited === 'object' ? edited.message_id : args.message_id
|
||||||
return { content: [{ type: 'text', text: `edited (id: ${id})` }] }
|
return { content: [{ type: 'text', text: `edited (id: ${id})` }] }
|
||||||
@@ -593,35 +609,9 @@ async function handleInbound(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 409 Conflict = another getUpdates consumer is still active (zombie from a
|
void bot.start({
|
||||||
// previous session, or a second Claude Code instance). Retry with backoff
|
onStart: info => {
|
||||||
// until the slot frees up instead of crashing on the first rejection.
|
botUsername = info.username
|
||||||
void (async () => {
|
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
|
||||||
for (let attempt = 1; ; attempt++) {
|
},
|
||||||
try {
|
})
|
||||||
await bot.start({
|
|
||||||
onStart: info => {
|
|
||||||
botUsername = info.username
|
|
||||||
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return // bot.stop() was called — clean exit from the loop
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof GrammyError && err.error_code === 409) {
|
|
||||||
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