Notification
Learn how to set up and use notifications in MkSaaS
MkSaaS supports sending notifications when a user successfully completes a payment. This allows your team to receive real-time purchase alerts in your preferred tools. Currently, we support Discord and Feishu. More notification channels (such as Slack, or Telegram) will be supported in the future. The modular design makes it easy to integrate additional services as needed.
Setup
To enable notifications, you need to configure the following environment variables in your .env
file:
DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..."
FEISHU_WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/..."
How to Obtain Webhook URLs
Discord
- Go to your Discord server and open the channel where you want to receive notifications.
- Click the gear icon to open Channel Settings.
- Select Integrations > Webhooks > New Webhook.
- Set a name and (optionally) an avatar for the webhook.
- Copy the Webhook URL and paste it into your
.env
file asDISCORD_WEBHOOK_URL
.
For more details, see the Discord Webhooks Guide.
Feishu
- Go to your Feishu group chat.
- Click the group name > Group Settings > Bot Management.
- Add a new Custom Bot and enable Webhooks.
- Copy the generated Webhook URL and paste it into your
.env
file asFEISHU_WEBHOOK_URL
.
For more details, see the Feishu Webhook Documentation.
If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.
Environment Setup
Set up environment variables
Notification System Structure
The notification system in MkSaaS is modular and easy to extend:
discord.ts
: Sends purchase notifications to Discord using the configured webhook.feishu.ts
: Sends purchase notifications to Feishu using the configured webhook.notification.ts
: Central entry point to send notifications to all enabled channels.
Core Features
- Real-time notifications for successful payments
- Supports Discord and Feishu out of the box
- Easy to add more notification channels in the future
- Robust error handling to avoid interrupting payment flow
Usage
To send a notification after a successful payment, use the following utility:
import { sendNotification } from '@/notification';
// Example usage after payment success
await sendNotification(sessionId, customerId, userName, amount);
This will automatically send messages to both Discord and Feishu if their respective webhook URLs are configured.
Message Format
The notification messages include the following information:
- Username: The customer's username
- Amount: The purchase amount in dollars
- Customer ID: Stripe customer ID for reference
- Session ID: Stripe checkout session ID for tracking
- Timestamp: When the purchase occurred
Discord Message
Discord notifications are sent as rich embeds with a green color and structured fields for easy reading.
Feishu Message
Feishu notifications are sent as plain text messages with all the purchase details formatted clearly.
Customization & Extensibility
The notification system is designed to be extensible. To add a new notification channel:
- Create a new file in the
src/notification
directory (e.g.,slack.ts
). - Implement a function to send messages to the new channel:
export async function sendMessageToSlack(
sessionId: string,
customerId: string,
userName: string,
amount: number
): Promise<void> {
try {
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
if (!webhookUrl) {
console.warn('SLACK_WEBHOOK_URL is not set, skipping Slack notification');
return;
}
// Your Slack message implementation
// ...
} catch (error) {
console.error('<< Failed to send Slack notification:', error);
}
}
- Import and call this function in
notification.ts
:
import { sendMessageToSlack } from './slack';
export async function sendNotification(
sessionId: string,
customerId: string,
userName: string,
amount: number
): Promise<void> {
// Existing Discord and Feishu calls
await sendMessageToDiscord(sessionId, customerId, userName, amount);
await sendMessageToFeishu(sessionId, customerId, userName, amount);
// Add new Slack notification
await sendMessageToSlack(sessionId, customerId, userName, amount);
}
Best Practices
- Keep Webhook URLs Secret: Never expose your webhook URLs publicly or commit them to version control.
- Monitor Delivery: Check your notification channels to ensure messages are delivered as expected.
- Error Handling: The system logs errors but does not interrupt the payment flow if a notification fails.
- Rate Limiting: Be aware of webhook rate limits for each service to avoid being blocked.
- Testing: Test your webhooks in a development environment before deploying to production.
- Extensibility: Follow the existing pattern when adding new notification channels.
Next Steps
Now that you understand how to work with notifications in MkSaaS, explore these related integrations: