|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import fs from 'fs' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +export async function POST(request: NextRequest) { |
| 6 | + try { |
| 7 | + const body = await request.json() |
| 8 | + |
| 9 | + // Telegram callback query |
| 10 | + if (body.callback_query) { |
| 11 | + const callbackData = body.callback_query.data |
| 12 | + const messageText = body.callback_query.message.text |
| 13 | + |
| 14 | + // Parse tool data from message |
| 15 | + const toolData = parseToolFromMessage(messageText) |
| 16 | + |
| 17 | + if (callbackData.startsWith('approve_')) { |
| 18 | + // Add to pending tools |
| 19 | + await addPendingTool(toolData) |
| 20 | + |
| 21 | + // Reply to Telegram |
| 22 | + await sendTelegramMessage( |
| 23 | + body.callback_query.message.chat.id, |
| 24 | + '✅ Araç onaylandı ve önizleme olarak eklendi!' |
| 25 | + ) |
| 26 | + } else if (callbackData.startsWith('reject_')) { |
| 27 | + await sendTelegramMessage( |
| 28 | + body.callback_query.message.chat.id, |
| 29 | + '❌ Araç reddedildi.' |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return NextResponse.json({ ok: true }) |
| 35 | + } catch (error) { |
| 36 | + console.error('Webhook error:', error) |
| 37 | + return NextResponse.json({ error: 'Webhook error' }, { status: 500 }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function parseToolFromMessage(text: string) { |
| 42 | + const lines = text.split('\n') |
| 43 | + const data: any = {} |
| 44 | + |
| 45 | + lines.forEach(line => { |
| 46 | + if (line.includes('*İsim:*')) data.name = line.split('*İsim:*')[1].trim() |
| 47 | + if (line.includes('*Kategori:*')) data.category = line.split('*Kategori:*')[1].trim().toLowerCase() |
| 48 | + if (line.includes('*Açıklama:*')) data.description = line.split('*Açıklama:*')[1].trim() |
| 49 | + if (line.includes('*Dil:*')) data.language = line.split('*Dil:*')[1].trim().toLowerCase() |
| 50 | + if (line.includes('*Kullanım:*')) data.usage = line.split('*Kullanım:*')[1].trim() |
| 51 | + }) |
| 52 | + |
| 53 | + // Generate ID |
| 54 | + data.id = data.name?.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '') || 'tool-' + Date.now() |
| 55 | + data.fileName = `${data.id}.${data.language === 'python' ? 'py' : 'bat'}` |
| 56 | + data.features = ['Önizleme aracı', 'Topluluk katkısı'] |
| 57 | + data.code = '# Kod yakında eklenecek' |
| 58 | + data.example = '# Örnek çıktı yakında eklenecek' |
| 59 | + data.isPending = true |
| 60 | + |
| 61 | + return data |
| 62 | +} |
| 63 | + |
| 64 | +async function addPendingTool(toolData: any) { |
| 65 | + const filePath = path.join(process.cwd(), 'lib', 'pending-tools.json') |
| 66 | + let pendingTools = [] |
| 67 | + |
| 68 | + try { |
| 69 | + const fileContent = fs.readFileSync(filePath, 'utf-8') |
| 70 | + pendingTools = JSON.parse(fileContent) |
| 71 | + } catch (error) { |
| 72 | + // File doesn't exist or is empty |
| 73 | + } |
| 74 | + |
| 75 | + pendingTools.push(toolData) |
| 76 | + fs.writeFileSync(filePath, JSON.stringify(pendingTools, null, 2)) |
| 77 | +} |
| 78 | + |
| 79 | +async function sendTelegramMessage(chatId: string, text: string) { |
| 80 | + const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN |
| 81 | + |
| 82 | + await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`, { |
| 83 | + method: 'POST', |
| 84 | + headers: { 'Content-Type': 'application/json' }, |
| 85 | + body: JSON.stringify({ chat_id: chatId, text }), |
| 86 | + }) |
| 87 | +} |
0 commit comments