Skip to content

Commit 0e08022

Browse files
committed
feat: add Telegram webhook for auto-approval
- Created /api/telegram-webhook endpoint - Pending tools system (lib/pending-tools.json) - Auto-add approved tools as preview - Parse tool data from Telegram message - Merge static and pending tools - Webhook integration for ✅/❌ buttons
1 parent 1dc92bf commit 0e08022

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

app/api/telegram-webhook/route.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

lib/pending-tools.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

lib/tools-data.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ export interface Tool {
99
usage: string
1010
code: string
1111
example: string
12+
isPending?: boolean
1213
}
1314

14-
export const tools: Tool[] = [
15+
// Pending tools'u yükle
16+
let pendingTools: Tool[] = []
17+
try {
18+
pendingTools = require('./pending-tools.json')
19+
} catch {
20+
// Dosya yoksa boş array
21+
}
22+
23+
const staticTools: Tool[] = [
1524
// Network Tools
1625
{
1726
id: 'ip-scanner',
@@ -1486,3 +1495,6 @@ export const categories = {
14861495
color: 'purple'
14871496
}
14881497
}
1498+
1499+
// Static tools ile pending tools'u birleştir
1500+
export const tools: Tool[] = [...staticTools, ...pendingTools]

0 commit comments

Comments
 (0)