add bale token
This commit is contained in:
+18
-17
@@ -1,6 +1,7 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
const TELEGRAM_API = 'https://api.telegram.org'
|
||||
/** API بله سازگار با API تلگرام — مستندات: https://dev.bale.ai */
|
||||
const BALE_API = 'https://tapi.bale.ai'
|
||||
|
||||
export interface ConsultingRequestBody {
|
||||
fullName: string
|
||||
@@ -10,7 +11,7 @@ export interface ConsultingRequestBody {
|
||||
description?: string
|
||||
}
|
||||
|
||||
function escapeForTelegram(text: string): string {
|
||||
function escapeHtml(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
@@ -18,12 +19,15 @@ function escapeForTelegram(text: string): string {
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const token = process.env.TELEGRAM_BOT_TOKEN
|
||||
const chatId = process.env.TELEGRAM_CHAT_ID
|
||||
const token = process.env.BALE_BOT_TOKEN
|
||||
const chatId = process.env.BALE_CHAT_ID
|
||||
|
||||
if (!token || !chatId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'تلگرام پیکربندی نشده است. TELEGRAM_BOT_TOKEN و TELEGRAM_CHAT_ID را در .env.local تنظیم کنید.' },
|
||||
{
|
||||
error:
|
||||
'بله پیکربندی نشده است. BALE_BOT_TOKEN و BALE_CHAT_ID را در .env.local تنظیم کنید.',
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
@@ -43,19 +47,19 @@ export async function POST(request: Request) {
|
||||
const lines = [
|
||||
'📩 <b>درخواست جدید مشاوره سازمانی</b>',
|
||||
'',
|
||||
`<b>نام و نام خانوادگی:</b> ${escapeForTelegram(fullName.trim())}`,
|
||||
`<b>نام سازمان:</b> ${escapeForTelegram(organization.trim())}`,
|
||||
`<b>شماره تماس:</b> ${escapeForTelegram(phone.trim())}`,
|
||||
`<b>حوزه مشاوره:</b> ${escapeForTelegram(area.trim())}`,
|
||||
`<b>نام و نام خانوادگی:</b> ${escapeHtml(fullName.trim())}`,
|
||||
`<b>نام سازمان:</b> ${escapeHtml(organization.trim())}`,
|
||||
`<b>شماره تماس:</b> ${escapeHtml(phone.trim())}`,
|
||||
`<b>حوزه مشاوره:</b> ${escapeHtml(area.trim())}`,
|
||||
]
|
||||
if (description?.trim()) {
|
||||
lines.push('', `<b>توضیحات:</b>`, escapeForTelegram(description.trim()))
|
||||
lines.push('', '<b>توضیحات:</b>', escapeHtml(description.trim()))
|
||||
}
|
||||
|
||||
const text = lines.join('\n')
|
||||
|
||||
try {
|
||||
const res = await fetch(`${TELEGRAM_API}/bot${token}/sendMessage`, {
|
||||
const res = await fetch(`${BALE_API}/bot${token}/sendMessage`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -68,15 +72,12 @@ export async function POST(request: Request) {
|
||||
const data = await res.json().catch(() => ({}))
|
||||
if (!res.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: data.description || 'ارسال به تلگرام ناموفق بود' },
|
||||
{ error: data.description || 'ارسال به بله ناموفق بود' },
|
||||
{ status: 502 }
|
||||
)
|
||||
}
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (err) {
|
||||
return NextResponse.json(
|
||||
{ error: 'خطا در ارتباط با تلگرام' },
|
||||
{ status: 502 }
|
||||
)
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'خطا در ارتباط با بله' }, { status: 502 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user