diff --git a/src/index.ts b/src/index.ts index d221b55..c38e626 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { Env, TelegramUpdate } from './types'; import { validateWebhookRequest, checkRateLimit } from './security'; -import { sendMessage, setWebhook, getWebhookInfo, sendChatAction } from './telegram'; +import { sendMessage, sendMessageWithKeyboard, setWebhook, getWebhookInfo, sendChatAction } from './telegram'; import { addToBuffer, processAndSummarize, @@ -76,6 +76,15 @@ async function handleMessage( const [command, ...argParts] = text.split(' '); const args = argParts.join(' '); responseText = await handleCommand(env, userId, chatIdStr, command, args); + + // /start 명령어는 미니앱 버튼과 함께 전송 + if (command === '/start') { + await sendMessageWithKeyboard(env.BOT_TOKEN, chatId, responseText, [ + [{ text: '🌐 서비스 보기', web_app: { url: 'https://anvil-hosting.pages.dev' } }], + [{ text: '💬 문의하기', url: 'https://t.me/AnvilForgeBot' }], + ]); + return; + } } else { // 타이핑 표시 await sendChatAction(env.BOT_TOKEN, chatId, 'typing'); diff --git a/src/telegram.ts b/src/telegram.ts index dda626a..55db0a5 100644 --- a/src/telegram.ts +++ b/src/telegram.ts @@ -82,6 +82,54 @@ export async function deleteWebhook( return response.json(); } +// 인라인 키보드 타입 +export interface InlineKeyboardButton { + text: string; + url?: string; + callback_data?: string; + web_app?: { url: string }; +} + +// 인라인 키보드와 함께 메시지 전송 +export async function sendMessageWithKeyboard( + token: string, + chatId: number, + text: string, + keyboard: InlineKeyboardButton[][], + options?: { + parse_mode?: 'HTML' | 'Markdown' | 'MarkdownV2'; + } +): Promise { + 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_markup: { + inline_keyboard: keyboard, + }, + }), + } + ); + + 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 with keyboard:', error); + return false; + } +} + // 타이핑 액션 전송 export async function sendChatAction( token: string,