This commit is contained in:
Your Name committed 2026-06-12 23:21:14 -04:00
1 parent 36f8ff0759
commit 182d0349fa
5 files changed
+70 -3

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
import { Elysia } from 'elysia';
import * as line from '@line/bot-sdk';
import { lineConfig, lineClient } from '../lib/lineClient';
import crypto from 'crypto';
export const lineRoutes = new Elysia({ prefix: '/line' })
.post('/webhook', async ({ request, body, set }) => {
// 1. Verify Signature
const signature = request.headers.get('x-line-signature');
if (!signature) {
set.status = 400;
return { error: 'Missing Signature' };
}
// get raw body string
const rawBody = await request.clone().text();
const hash = crypto
.createHmac('SHA256', lineConfig.channelSecret)
.update(rawBody)
.digest('base64');
if (hash !== signature) {
set.status = 401;
return { error: 'Invalid Signature' };
}
try {
const events: line.WebhookEvent[] = (body as any).events || [];
for (const event of events) {
if (event.type === 'message' && event.message.type === 'text') {
// Simple echo bot for now
await lineClient.replyMessage({
replyToken: event.replyToken,
messages: [{
type: 'text',
text: `คุณส่งข้อความว่า: ${event.message.text}`
}]
});
}
}
return { success: true };
} catch (err) {
console.error('[LINE Webhook] Error:', err);
set.status = 500;
return { error: 'Webhook processing failed' };
}
});