/* eslint-disable @typescript-eslint/no-unused-vars */ "use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { useToast } from "@/components/ui/use-toast" import { Loader2, Send, ChevronDown, ChevronUp } from "lucide-react" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip" export function WebhookConfig() { const [enabled, setEnabled] = useState(false) const [url, setUrl] = useState("") const [loading, setLoading] = useState(false) const [testing, setTesting] = useState(false) const [showDocs, setShowDocs] = useState(false) const [initialLoading, setInitialLoading] = useState(true) const { toast } = useToast() useEffect(() => { fetch("/api/webhook") .then(res => res.json() as Promise<{ enabled: boolean; url: string }>) .then(data => { setEnabled(data.enabled) setUrl(data.url) }) .catch(console.error) .finally(() => setInitialLoading(false)) }, []) if (initialLoading) { return (

加载中...

) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!url) return setLoading(true) try { const res = await fetch("/api/webhook", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ url, enabled }) }) if (!res.ok) throw new Error("Failed to save") toast({ title: "保存成功", description: "Webhook 配置已更新" }) } catch (_error) { toast({ title: "保存失败", description: "请稍后重试", variant: "destructive" }) } finally { setLoading(false) } } const handleTest = async () => { if (!url) return setTesting(true) try { const res = await fetch("/api/webhook/test", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ url }) }) if (!res.ok) throw new Error("测试失败") toast({ title: "测试成功", description: "Webhook 调用成功,请检查目标服务器是否收到请求" }) } catch (_error) { toast({ title: "测试失败", description: "请检查 URL 是否正确且可访问", variant: "destructive" }) } finally { setTesting(false) } } return (
当收到新邮件时通知指定的 URL
{enabled && (
setUrl(e.target.value)} type="url" required />

发送测试消息到此 Webhook

我们会向此 URL 发送 POST 请求,包含新邮件的相关信息

{showDocs && (

当收到新邮件时,我们会向配置的 URL 发送 POST 请求,请求头包含:

                  Content-Type: application/json{'\n'}
                  X-Webhook-Event: new_message
                

请求体示例:

                  {`{
  "emailId": "email-uuid",
  "messageId": "message-uuid",
  "fromAddress": "sender@example.com",
  "subject": "邮件主题",
  "content": "邮件文本内容",
  "html": "邮件HTML内容",
  "receivedAt": "2024-01-01T12:00:00.000Z",
  "toAddress": "your-email@${window.location.host}"
}`}
                
)}
)}
) }