Initial commit: Telegram bot with Cloudflare Workers

- OpenAI GPT-4o-mini with Function Calling
- Cloudflare D1 for user profiles and message buffer
- Sliding window (3 summaries max) for infinite context
- Tools: weather, search, time, calculator
- Workers AI fallback support
- Webhook security with rate limiting

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-01-14 13:00:44 +09:00
commit 1e71e035e7
15 changed files with 2272 additions and 0 deletions

68
src/types.ts Normal file
View File

@@ -0,0 +1,68 @@
export interface Env {
DB: D1Database;
AI: Ai;
BOT_TOKEN: string;
WEBHOOK_SECRET: string;
SUMMARY_THRESHOLD?: string;
MAX_SUMMARIES_PER_USER?: string;
N8N_WEBHOOK_URL?: string;
OPENAI_API_KEY?: string;
}
export interface IntentAnalysis {
action: 'chat' | 'n8n';
type?: string;
confidence: number;
}
export interface N8nResponse {
reply?: string;
error?: string;
}
export interface TelegramUpdate {
update_id: number;
message?: TelegramMessage;
}
export interface TelegramMessage {
message_id: number;
from: TelegramUser;
chat: TelegramChat;
date: number;
text?: string;
}
export interface TelegramUser {
id: number;
is_bot: boolean;
first_name: string;
last_name?: string;
username?: string;
}
export interface TelegramChat {
id: number;
type: string;
}
export interface BufferedMessage {
id: number;
role: 'user' | 'bot';
message: string;
created_at: string;
}
export interface Summary {
id: number;
generation: number;
summary: string;
message_count: number;
created_at: string;
}
export interface ConversationContext {
previousSummary: Summary | null;
recentMessages: BufferedMessage[];
totalMessages: number;
}