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:
107
src/telegram.ts
Normal file
107
src/telegram.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
// Telegram API 메시지 전송
|
||||
export async function sendMessage(
|
||||
token: string,
|
||||
chatId: number,
|
||||
text: string,
|
||||
options?: {
|
||||
parse_mode?: 'HTML' | 'Markdown' | 'MarkdownV2';
|
||||
reply_to_message_id?: number;
|
||||
disable_notification?: boolean;
|
||||
}
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.telegram.org/bot${token}/sendMessage`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
chat_id: chatId,
|
||||
text,
|
||||
parse_mode: options?.parse_mode || 'HTML',
|
||||
reply_to_message_id: options?.reply_to_message_id,
|
||||
disable_notification: options?.disable_notification,
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.text();
|
||||
console.error('Telegram API error:', error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Failed to send message:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Webhook 설정 (Secret Token 포함)
|
||||
export async function setWebhook(
|
||||
token: string,
|
||||
webhookUrl: string,
|
||||
secretToken: string
|
||||
): Promise<{ ok: boolean; description?: string }> {
|
||||
const response = await fetch(
|
||||
`https://api.telegram.org/bot${token}/setWebhook`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
url: webhookUrl,
|
||||
secret_token: secretToken,
|
||||
allowed_updates: ['message'],
|
||||
drop_pending_updates: true,
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Webhook 정보 조회
|
||||
export async function getWebhookInfo(
|
||||
token: string
|
||||
): Promise<unknown> {
|
||||
const response = await fetch(
|
||||
`https://api.telegram.org/bot${token}/getWebhookInfo`
|
||||
);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Webhook 삭제
|
||||
export async function deleteWebhook(
|
||||
token: string
|
||||
): Promise<{ ok: boolean }> {
|
||||
const response = await fetch(
|
||||
`https://api.telegram.org/bot${token}/deleteWebhook`,
|
||||
{ method: 'POST' }
|
||||
);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 타이핑 액션 전송
|
||||
export async function sendChatAction(
|
||||
token: string,
|
||||
chatId: number,
|
||||
action: 'typing' | 'upload_photo' | 'upload_document' = 'typing'
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.telegram.org/bot${token}/sendChatAction`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
chat_id: chatId,
|
||||
action,
|
||||
}),
|
||||
}
|
||||
);
|
||||
return response.ok;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user