feat: support send message to feishu

This commit is contained in:
javayhu 2025-06-14 02:09:40 +08:00
parent a7a5a8a6a4
commit e610fe7335
5 changed files with 88 additions and 4 deletions

View File

@ -127,9 +127,15 @@ NEXT_PUBLIC_DATAFAST_ANALYTICS_DOMAIN=""
# -----------------------------------------------------------------------------
# Discord
# -----------------------------------------------------------------------------
DISCORD_WEBHOOK_URL=""
NEXT_PUBLIC_DISCORD_WIDGET_SERVER_ID=""
NEXT_PUBLIC_DISCORD_WIDGET_CHANNEL_ID=""
# -----------------------------------------------------------------------------
# Feishu
# -----------------------------------------------------------------------------
FEISHU_WEBHOOK_URL=""
# -----------------------------------------------------------------------------
# Affiliate
# https://mksaas.com/docs/affiliate

View File

@ -67,7 +67,6 @@ export async function sendMessageToDiscord(
});
if (!response.ok) {
// throw new Error(`Discord webhook request failed with status ${response.status}`);
console.error(
`<< Failed to send Discord notification for user ${userName}:`,
response

View File

@ -0,0 +1,55 @@
/**
* Send a message to Feishu when a user makes a purchase
* @param sessionId The Stripe checkout session ID
* @param customerId The Stripe customer ID
* @param userName The username of the customer
* @param amount The purchase amount in the currency's main unit (e.g., dollars, not cents)
*/
export async function sendMessageToFeishu(
sessionId: string,
customerId: string,
userName: string,
amount: number
): Promise<void> {
try {
const webhookUrl = process.env.FEISHU_WEBHOOK_URL;
if (!webhookUrl) {
console.warn(
'FEISHU_WEBHOOK_URL is not set, skipping Feishu notification'
);
return;
}
// Format the message
const message = {
msg_type: 'text',
content: {
text: `🎉 New Purchase\nUsername: ${userName}\nAmount: $${amount.toFixed(2)}\nCustomer ID: \`${customerId}\`\nSession ID: \`${sessionId}\``,
},
};
// Send the webhook request
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(message),
});
if (!response.ok) {
console.error(
`<< Failed to send Feishu notification for user ${userName}:`,
response
);
}
console.log(
`<< Successfully sent Feishu notification for user ${userName}`
);
} catch (error) {
console.error('<< Failed to send Feishu notification:', error);
// Don't rethrow the error to avoid interrupting the payment flow
}
}

View File

@ -0,0 +1,24 @@
import { sendMessageToDiscord } from './discord';
import { sendMessageToFeishu } from './feishu';
/**
* Send a notification when a user makes a purchase
* @param sessionId The Stripe checkout session ID
* @param customerId The Stripe customer ID
* @param userName The username of the customer
* @param amount The purchase amount in the currency's main unit (e.g., dollars, not cents)
*/
export async function sendNotification(
sessionId: string,
customerId: string,
userName: string,
amount: number
): Promise<void> {
console.log('sendNotification', sessionId, customerId, userName, amount);
// Send message to Discord channel
await sendMessageToDiscord(sessionId, customerId, userName, amount);
// Send message to Feishu group
await sendMessageToFeishu(sessionId, customerId, userName, amount);
}

View File

@ -1,12 +1,12 @@
import { randomUUID } from 'crypto';
import { getDb } from '@/db';
import { payment, session, user } from '@/db/schema';
import { sendMessageToDiscord } from '@/lib/discord';
import {
findPlanByPlanId,
findPlanByPriceId,
findPriceInPlan,
} from '@/lib/price-plan';
import { sendNotification } from '@/notification/notification';
import { desc, eq } from 'drizzle-orm';
import { Stripe } from 'stripe';
import {
@ -626,9 +626,9 @@ export class StripeProvider implements PaymentProvider {
`<< Created one-time payment record for user ${userId}, price: ${priceId}`
);
// Send message to Discord channel
// Send notification
const amount = session.amount_total ? session.amount_total / 100 : 0;
await sendMessageToDiscord(session.id, customerId, userId, amount);
await sendNotification(session.id, customerId, userId, amount);
}
/**