added to send message to telegram bot
This commit is contained in:
@@ -3,11 +3,11 @@
|
||||
import { useState } from "react";
|
||||
import { motion } from "framer-motion";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { sendContactMessage } from "@/lib/api";
|
||||
import { Phone, MessageSquare, MapPin } from "lucide-react";
|
||||
import Image from "next/image";
|
||||
import { useLanguage } from "@/context/language-context";
|
||||
import { useProductStore } from "@/lib/productZustand";
|
||||
import axios from "axios";
|
||||
|
||||
export function ContactForm() {
|
||||
const { t } = useLanguage();
|
||||
@@ -45,18 +45,26 @@ export function ContactForm() {
|
||||
setLoading(true);
|
||||
setMessage(null);
|
||||
|
||||
try {
|
||||
const result = await sendContactMessage({
|
||||
...formData,
|
||||
lang: locale,
|
||||
});
|
||||
const text = `
|
||||
📩 New Contact Message
|
||||
|
||||
if (result.success) {
|
||||
setMessage({ type: "success", text: t.contact.success });
|
||||
setFormData({ name: "", phone: "", message: "", productName: "" });
|
||||
} else {
|
||||
setMessage({ type: "error", text: t.contact.error });
|
||||
}
|
||||
👤 Name: ${formData.name}
|
||||
📞 Phone: ${formData.phone}
|
||||
📦 Product: ${formData.productName || "—"}
|
||||
|
||||
💬 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 {
|
||||
setMessage({ type: "error", text: t.contact.error });
|
||||
} finally {
|
||||
@@ -163,7 +171,7 @@ export function ContactForm() {
|
||||
value={formData.name}
|
||||
onChange={handleChange}
|
||||
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>
|
||||
|
||||
@@ -179,7 +187,7 @@ export function ContactForm() {
|
||||
onChange={handleChange}
|
||||
placeholder={t.contact.phonePlaceholder}
|
||||
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>
|
||||
|
||||
@@ -194,7 +202,7 @@ export function ContactForm() {
|
||||
onChange={handleChange}
|
||||
placeholder={t.contact.messagePlaceholder}
|
||||
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>
|
||||
|
||||
@@ -209,7 +217,7 @@ export function ContactForm() {
|
||||
value={productName ? productName : formData.productName}
|
||||
onChange={handleChange}
|
||||
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>
|
||||
|
||||
|
||||
36
lib/api.ts
36
lib/api.ts
@@ -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" };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user