first project version mrDecor
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
'use server'
|
||||
|
||||
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN
|
||||
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID
|
||||
const TELEGRAM_URL = process.env.TELEGRAM_URL
|
||||
|
||||
export type ConsultationFormData = {
|
||||
name: string
|
||||
phone: string
|
||||
email?: string
|
||||
subject?: string
|
||||
message?: string
|
||||
}
|
||||
|
||||
function buildTelegramMessage(data: ConsultationFormData): string {
|
||||
const lines = [
|
||||
'📩 *درخواست مشاوره آنلاین*',
|
||||
'',
|
||||
`👤 *نام:* ${data.name}`,
|
||||
`📱 *تلفن:* ${data.phone}`,
|
||||
]
|
||||
if (data.email?.trim()) {
|
||||
lines.push(`📧 *ایمیل:* ${data.email.trim()}`)
|
||||
}
|
||||
if (data.subject?.trim()) {
|
||||
lines.push(`📌 *موضوع:* ${data.subject.trim()}`)
|
||||
}
|
||||
if (data.message?.trim()) {
|
||||
lines.push('', '*توضیحات:*', data.message.trim())
|
||||
}
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
export async function submitConsultation(
|
||||
data: ConsultationFormData
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
const name = data.name?.trim()
|
||||
const phone = data.phone?.trim().replace(/\s/g, '')
|
||||
|
||||
if (!name || name.length < 2) {
|
||||
return { success: false, error: 'نام و نام خانوادگی را وارد کنید.' }
|
||||
}
|
||||
if (!phone || phone.length < 10) {
|
||||
return { success: false, error: 'شماره تماس معتبر وارد کنید.' }
|
||||
}
|
||||
|
||||
if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) {
|
||||
console.error('TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID is not set')
|
||||
return {
|
||||
success: false,
|
||||
error: 'پیکربندی ارسال پیام انجام نشده است. لطفاً با پشتیبانی تماس بگیرید.',
|
||||
}
|
||||
}
|
||||
|
||||
const text = buildTelegramMessage({
|
||||
name,
|
||||
phone,
|
||||
email: data.email,
|
||||
subject: data.subject,
|
||||
message: data.message,
|
||||
})
|
||||
|
||||
const url = `${TELEGRAM_URL}/bot${TELEGRAM_BOT_TOKEN}/sendMessage`
|
||||
const body = {
|
||||
chat_id: TELEGRAM_CHAT_ID,
|
||||
text,
|
||||
parse_mode: 'Markdown',
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
const result = await res.json()
|
||||
|
||||
if (!res.ok || !result.ok) {
|
||||
console.error('Telegram API error:', result)
|
||||
return {
|
||||
success: false,
|
||||
error: 'ارسال پیام با خطا مواجه شد. لطفاً دوباره تلاش کنید.',
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
} catch (e) {
|
||||
console.error('Failed to send to Telegram', e)
|
||||
return {
|
||||
success: false,
|
||||
error: 'ارسال پیام با خطا مواجه شد. لطفاً دوباره تلاش کنید.',
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import React from 'react'
|
||||
import type { Metadata } from 'next'
|
||||
import { ConsultationForm } from '@/components/sections/other/consultation-form'
|
||||
import { getContentSync } from '@/lib/content'
|
||||
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const content = getContentSync()
|
||||
const meta = content.meta.pages.consultation
|
||||
return {
|
||||
title: meta.title,
|
||||
description: meta.description,
|
||||
}
|
||||
}
|
||||
|
||||
export default function ConsultationPage() {
|
||||
const content = getContentSync()
|
||||
const { consultation } = content.pages
|
||||
|
||||
return (
|
||||
<main className="min-h-screen">
|
||||
<div className="mx-auto max-w-7xl px-6 py-24 md:py-32 lg:py-40">
|
||||
<div className="space-y-10 text-center">
|
||||
<div className="space-y-4">
|
||||
<h1 className="text-heading-1 font-bold">
|
||||
{consultation.heading}
|
||||
</h1>
|
||||
<p className="text-body text-muted-foreground mx-auto max-w-2xl">
|
||||
{consultation.description}
|
||||
</p>
|
||||
</div>
|
||||
<ConsultationForm />
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user