medusa-notification-provider
v0.0.9
Published
Medusa v2 notification plugin: FCM, WhatsApp, and Telegram order notifications.
Maintainers
Readme
medusa-notification-provider
Medusa v2 notification plugin: FCM push, WhatsApp Cloud API, and global Telegram order notifications.
Plugin Overview
medusa-notification-provider provides:
- FCM provider (
id: "fcm") — push via Firebase Cloud Messaging - WhatsApp provider (
id: "whatsapp") — session text via Meta Cloud API (type: "text") - Telegram order notifications — singleton channel config, admin settings UI, workflow + BullMQ queue for
order.placed/order.fulfillment_created/order.canceled
Medusa Version
Built for Medusa v2 (@medusajs/framework / @medusajs/medusa 2.12.4).
Installation & Setup
1) Install
npm install medusa-notification-provider firebase-admin bullmq ioredis zod2) Register in medusa-config.ts
import { defineConfig } from "@medusajs/framework/utils"
export default defineConfig({
plugins: [
{
resolve: "medusa-notification-provider",
options: {},
},
],
modules: [
{
resolve: "@medusajs/medusa/notification",
options: {
providers: [
{
resolve: "medusa-notification-provider/providers/fcm",
id: "fcm",
options: {
channels: ["push"],
projectId: "<firebase-project-id>",
clientEmail: "<service-account-email>",
privateKey: "<service-account-private-key>",
},
},
{
resolve: "medusa-notification-provider/providers/whatsapp",
id: "whatsapp",
options: {
channels: ["sms"],
enabled: true,
accessToken: "<meta-access-token>",
wabaId: "<whatsapp-business-account-id>",
phoneNumberId: "<phone-number-id>",
graphAPIVersion: "v21.0",
},
},
],
},
},
{
resolve: "medusa-notification-provider/modules/telegram-notification",
},
],
})3) Environment variables (Telegram)
| Variable | Required | Purpose |
|---|---|---|
| TELEGRAM_BOT_TOKEN | Yes (for send/test) | Bot token from BotFather. Never logged or stored in DB. |
| REDIS_URL | Yes (for order queue) | Redis for BullMQ. Enqueue failures are caught and logged as failed without blocking order flows. |
4) Database migrations
Module migrations ship with the plugin (telegram_config, telegram_notification_log).
- Plugin local Medusa app (when changing schema):
npx medusa db:generate telegram-notification - Host app:
npx medusa db:migrate(auto-discovers registered module migrations)
Telegram Order Notifications
Behavior
- Admin configures one global Telegram channel ID and clicks Send Test Message.
- On success,
verified = true. Real order notifications only send when verified. - Subscribers for
order.placed,order.fulfillment_created, andorder.canceledrunsend-telegram-notificationworkflow. - Workflow loads the order via
query.graph, applies idempotency + verification gates, formats HTML, enqueues to BullMQ. - In-process worker (MVP) sends with global limiter 30 msg/sec and per-chat throttle 1 msg/sec. Messages are delayed, never dropped, on rate limit.
- If
REDIS_URLis unset (common in local), messages are sent directly (no queue) so order events still notify.
Idempotency
- App check: skip if a
pendingorsentlog exists for(order_id, event_type). - DB backstop: partial unique index on
(order_id, event_type)where status ispendingorsent. - Unique violation on pending insert is treated as already handled (skip), not as failure.
Admin UI
Sidebar route Telegram Notifications (src/admin/routes/telegram-notifications/page.tsx):
- Channel ID input + format hint + help for finding
-100…IDs - Save / Send Test Message (button disabled ~3s after each test click)
- Verified badge
Admin API
| Method | Path | Description |
|---|---|---|
| GET | /admin/telegram-settings | Singleton config (no bot token) |
| POST | /admin/telegram-settings | Save channel_id; resets verified when changed |
| POST | /admin/telegram-settings/verify | Direct Telegram send (bypasses queue, no log row); sets verified on success; returns Telegram error text on failure |
Channel ID validation (Zod + UI): ^(-100\d{10,13}|@[a-zA-Z0-9_]{5,32})$
Worker placement (MVP)
BullMQ worker runs in-process with the Medusa API server (lazy-init on first enqueue). Server restarts drop in-flight throttle timing; queue processing is tied to API uptime. A dedicated worker process is out of scope for v1.
FCM / WhatsApp Providers
Unchanged. Configure via Notification module provider options as in the install example above.
FCM options
| Option | Required | Description |
|---|---|---|
| projectId | Yes | Firebase project ID |
| clientEmail | Yes | Service account email |
| privateKey | Yes | Service account private key (\\n normalized) |
WhatsApp options
| Option | Required | Default | Description |
|---|---|---|---|
| enabled | No | true | Enable/disable sends |
| accessToken | Yes | - | Meta Graph access token |
| wabaId | Yes | - | WhatsApp Business Account ID |
| phoneNumberId | Yes | - | Sender phone number ID |
| graphAPIVersion | No | v21.0 | Graph API version |
OTP / SMS via the Medusa notification module uses channel: "sms" and content.text. Map that channel to this provider. The client always sends a Cloud API session text payload (type: "text"). Recipients must be E.164 (+917567273055); Graph to is sent as digits only (917567273055).
Session text is delivered only inside Meta's 24-hour customer care window (the user must have messaged the business number first). Outside that window Graph returns (#131047) Re-engagement message.
Health routes
GET /admin/plugin— admin healthGET /store/plugin— store health
Testing
npm testCovers channel ID validation, HTML escaping, message formatting, verification/idempotency gates, enqueue failure when Redis is missing, and WhatsApp session-text payload / Graph error parsing.
Troubleshooting
Telegram: bot is not a member / chat not found
Add the bot as Channel Admin with Post Messages, then use the correct -100… ID from Telegram Web.
Telegram: channel saved but no order messages
Send a successful Test Message first (verified must be true). Changing channel_id resets verification.
Telegram: REDIS_URL is not configured
Order notifications need Redis for the queue. Test messages bypass the queue and only need TELEGRAM_BOT_TOKEN.
FCM / WhatsApp
See provider-specific errors in server logs; ensure credentials and recipient format (+ for WhatsApp phones) are correct.
OTP / SMS via Medusa notification module sends content.text. The WhatsApp provider reads content.text first, then data.body / data.title.
Issue: WhatsApp OTP marked success but not delivered
Problem:
Notification row is success with a wamid, but the user never sees the OTP. They often check SMS, while the provider sends WhatsApp.
Root Cause:
Graph API to was sent as +91…. Meta can return HTTP 200 + message id, then fail delivery. Session type: "text" also only delivers inside the 24-hour window.
Solution:
toWhatsAppRecipient strips the number to country-code digits before send. Graph errors now include error_data.details. Requests without a message id are treated as failure.
Prevention:
- Keep
channels: ["sms"]on the WhatsApp provider - Pass E.164 phones (
+917567273055) - Ask the recipient to check WhatsApp (including Message requests / Business chats)
- For OTP to users who have never messaged the business, an approved authentication template is required (not session text)
