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