feat: 텔레그램 미니앱 연동

- sendMessageWithKeyboard 함수 추가
- /start 명령어에 미니앱 버튼 추가
- 메뉴 버튼으로 서비스 페이지 열기 지원

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-01-14 13:57:21 +09:00
parent 4477551ae2
commit 72b0ae0133
2 changed files with 58 additions and 1 deletions

View File

@@ -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');

View File

@@ -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<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_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,