Files
hapi/server/src/telegram/renderer.ts
T
weishu bb9c5b66d3 refactor(telegram): remove interactive features and simplify to notifications only
Remove all interactive features from the Telegram bot that are now handled by the
Telegram Mini App. The bot now serves a single purpose: sending notifications for
permission requests and ready events.

Changes:
- bot.ts: Remove /help command and message handler, simplify /start to show Mini App link
- callbacks.ts: Remove all callback handlers except APPROVE and DENY for permissions
- sessionView.ts: Remove detail views and settings, keep only notification formatting
- renderer.ts: Remove session/machine list rendering, keep utility functions

This reduces the codebase by ~1000 lines (58% reduction) while preserving notification
functionality that the Mini App cannot provide.
2025-12-24 14:23:01 +08:00

72 lines
1.9 KiB
TypeScript

/**
* Utilities for Telegram Bot
*
* Helper functions for rendering and callback data handling.
* Simplified to only include utilities needed for notifications.
*/
import type { Session } from '../sync/syncEngine'
// Telegram limits
const MAX_MESSAGE_LENGTH = 4096
const MAX_CALLBACK_DATA = 64
/**
* Truncate text to fit within a limit
*/
export function truncate(text: string, maxLen: number = MAX_MESSAGE_LENGTH - 100): string {
if (text.length <= maxLen) return text
return text.slice(0, maxLen - 3) + '...'
}
/**
* Get session name (project name or directory name)
*/
export function getSessionName(session: Session): string {
if (session.metadata?.name) return session.metadata.name
if (session.metadata?.path) {
const parts = session.metadata.path.split('/')
return parts[parts.length - 1] || session.metadata.path
}
return 'Unknown'
}
/**
* Create callback data with size limit
* Format: action:sessionIdPrefix:extraData
*/
export function createCallbackData(action: string, sessionId: string, extra?: string): string {
// Use 8-char prefix for session ID to save space
const sessionPrefix = sessionId.slice(0, 8)
let data = `${action}:${sessionPrefix}`
if (extra) {
// Ensure we don't exceed 64 bytes
const remaining = MAX_CALLBACK_DATA - data.length - 1
if (remaining > 0) {
data += `:${extra.slice(0, remaining)}`
}
}
return data.slice(0, MAX_CALLBACK_DATA)
}
/**
* Parse callback data
*/
export function parseCallbackData(data: string): { action: string; sessionPrefix: string; extra?: string } {
const parts = data.split(':')
return {
action: parts[0] || '',
sessionPrefix: parts[1] || '',
extra: parts[2]
}
}
/**
* Find session by ID prefix
*/
export function findSessionByPrefix(sessions: Session[], prefix: string): Session | undefined {
return sessions.find(s => s.id.startsWith(prefix))
}