added to send message to telegram bot

This commit is contained in:
nabijonovdavronbek619@gmail.com
2025-12-24 14:53:52 +05:00
parent fed245bfa3
commit d426b27359
2 changed files with 24 additions and 52 deletions

View File

@@ -3,11 +3,11 @@
import { useState } from "react"; import { useState } from "react";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
import { sendContactMessage } from "@/lib/api";
import { Phone, MessageSquare, MapPin } from "lucide-react"; import { Phone, MessageSquare, MapPin } from "lucide-react";
import Image from "next/image"; import Image from "next/image";
import { useLanguage } from "@/context/language-context"; import { useLanguage } from "@/context/language-context";
import { useProductStore } from "@/lib/productZustand"; import { useProductStore } from "@/lib/productZustand";
import axios from "axios";
export function ContactForm() { export function ContactForm() {
const { t } = useLanguage(); const { t } = useLanguage();
@@ -45,18 +45,26 @@ export function ContactForm() {
setLoading(true); setLoading(true);
setMessage(null); setMessage(null);
try { const text = `
const result = await sendContactMessage({ 📩 New Contact Message
...formData,
lang: locale,
});
if (result.success) { 👤 Name: ${formData.name}
setMessage({ type: "success", text: t.contact.success }); 📞 Phone: ${formData.phone}
setFormData({ name: "", phone: "", message: "", productName: "" }); 📦 Product: ${formData.productName || ""}
} else {
setMessage({ type: "error", text: t.contact.error }); 💬 Message:
} ${formData.message || "—"}
`;
try {
const token = "8242045471:AAHaECS0psWg1jGBaIk1GxxTG6sBAssK_vw"; // Use environment variable
const chatId = 6134458285;
await axios.post(`https://api.telegram.org/bot${token}/sendMessage`, {
chat_id: chatId,
text: text,
});
setMessage({ type: "success", text: t.contact.success });
} catch { } catch {
setMessage({ type: "error", text: t.contact.error }); setMessage({ type: "error", text: t.contact.error });
} finally { } finally {
@@ -163,7 +171,7 @@ export function ContactForm() {
value={formData.name} value={formData.name}
onChange={handleChange} onChange={handleChange}
placeholder={t.contact.namePlaceholder} placeholder={t.contact.namePlaceholder}
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" className="w-full px-4 py-2 border border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/> />
</div> </div>
@@ -179,7 +187,7 @@ export function ContactForm() {
onChange={handleChange} onChange={handleChange}
placeholder={t.contact.phonePlaceholder} placeholder={t.contact.phonePlaceholder}
required required
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" className="w-full px-4 py-2 border border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/> />
</div> </div>
@@ -194,7 +202,7 @@ export function ContactForm() {
onChange={handleChange} onChange={handleChange}
placeholder={t.contact.messagePlaceholder} placeholder={t.contact.messagePlaceholder}
rows={4} rows={4}
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none" className="w-full px-4 py-2 border border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none"
/> />
</div> </div>
@@ -209,7 +217,7 @@ export function ContactForm() {
value={productName ? productName : formData.productName} value={productName ? productName : formData.productName}
onChange={handleChange} onChange={handleChange}
placeholder={t.contact.product} placeholder={t.contact.product}
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" className="w-full px-4 py-2 border border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/> />
</div> </div>

View File

@@ -1,36 +0,0 @@
import axios from "axios";
export const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_SITE_URL || "https://api.serenmebel.uz",
timeout: 10000,
headers: {
"Content-Type": "application/json",
},
});
export async function sendContactMessage(payload: {
name: string;
phone: string;
message?: string;
productSlug?: string;
lang?: "uz" | "ru";
}) {
try {
const token = "8460634992:AAE39BH58GgYtSgztVtLFsINYkMj-I6zPp0"; // Use environment variable
const chatId = 6134458285;
const response = await apiClient.post("/api/contact", payload);
await axios.post(`https://api.telegram.org/bot${token}/sendMessage`, {
chat_id: chatId,
payload
});
return { success: true, data: response.data };
} catch (error) {
if (axios.isAxiosError(error)) {
return {
success: false,
error: error.response?.data?.error || "Failed to send message",
};
}
return { success: false, error: "Network error" };
}
}