A serverless Telegram bot built on Cloudflare Workers that receives RevenueCat webhook notifications and sends formatted messages to your Telegram chat.
- 🔄 Real-time purchase notifications from RevenueCat
- 📱 Formatted Telegram messages with purchase details
- 🔐 Secure webhook signature verification
- ⚡ Serverless deployment on Cloudflare Workers
- 📊 Optional event tracking with Cloudflare KV
- 🛡️ Error handling and logging
Before you begin, make sure you have:
- Node.js (v16 or higher)
- npm or yarn
- A Cloudflare account
- A Telegram account
# Clone the repository
git clone <your-repo-url>
cd rcat-tg
# Install dependencies
npm install
- Open Telegram and search for @BotFather
- Send
/newbot
command - Follow the prompts to create your bot:
- Choose a name for your bot (e.g., "RevenueCat Notifications")
- Choose a username (must end with 'bot', e.g., "revenuecat_notifications_bot")
- BotFather will provide you with a bot token (save this for later)
- Search for @userinfobot in Telegram
- Send any message to this bot
- It will reply with your chat ID (save this for later)
- Start a conversation with your bot by searching for its username
- Send
/start
to initialize the bot - The bot should respond (this ensures it can send you messages)
npm install -g wrangler
wrangler login
This will open your browser to authenticate with Cloudflare.
The wrangler.toml
file is already configured, but you can modify it if needed:
name = "rcat-tg"
main = "src/index.js"
compatibility_date = "2024-01-01"
[[kv_namespaces]]
binding = "KV_BINDING"
id = "your-kv-namespace-id"
[observability.logs]
enabled = true
# Create a new KV namespace
wrangler kv:namespace create "RCAT_TG_KV"
Replace the KV namespace ID in your wrangler.toml
with the one from the previous step:
[[kv_namespaces]]
binding = "KV_BINDING"
id = "your-actual-kv-namespace-id"
wrangler secret put TELEGRAM_BOT_TOKEN
When prompted, enter your bot token from Step 2.
wrangler secret put TELEGRAM_CHAT_ID
When prompted, enter your chat ID from Step 2.
wrangler secret put REVENUECAT_WEBHOOK_SECRET
When prompted, enter your RevenueCat webhook secret (you'll get this in the next step).
# Deploy to get your worker URL
wrangler deploy
- Log in to your RevenueCat Dashboard
- Navigate to your project
- Go to Project Settings → Webhooks
- Click Add Webhook
- Configure the webhook:
- URL:
https://rcat-tg.your-subdomain.workers.dev/webhook/revenuecat
- Events: Select the events you want to receive (recommended: all events)
- Secret: Generate a new secret or use an existing one
- URL:
- Copy the webhook secret and set it as a wrangler secret:
wrangler secret put REVENUECAT_WEBHOOK_SECRET
wrangler deploy
-
Test Environment Variables:
curl https://rcat-tg.your-subdomain.workers.dev/test/env
-
Test Telegram Message:
curl -X POST https://rcat-tg.your-subdomain.workers.dev/test/telegram \ -H "Content-Type: application/json" \ -d '{"message": "Test message from RevenueCat bot!"}'
-
Test Health Check:
curl https://rcat-tg.your-subdomain.workers.dev/health
You can test the webhook with a sample payload:
curl -X POST https://rcat-tg.your-subdomain.workers.dev/test/webhook \
-H "Content-Type: application/json" \
-d '{
"event": {
"type": "INITIAL_PURCHASE",
"id": "test_event_123",
"app_user_id": "user123",
"product_id": "premium_monthly",
"price": 9.99,
"currency": "USD",
"store": "app_store",
"environment": "production",
"purchased_at_ms": 1640995200000
}
}'
View your worker logs in the Cloudflare dashboard or via wrangler:
wrangler tail
GET /health
Returns the health status of the worker.
POST /webhook/revenuecat
Receives RevenueCat webhook events and sends notifications to Telegram.
POST /test/telegram
Body: { "message": "Test message" }
Sends a test message to your configured Telegram chat.
GET /test/env
Returns the status of your environment variables (without exposing sensitive data).
POST /test/webhook
Body: { "event": { ... } }
For testing webhook processing without signature validation.
INITIAL_PURCHASE
- New subscription or purchaseRENEWAL
- Subscription renewalPRODUCT_CHANGE
- Subscription plan changeCANCELLATION
- Subscription cancellationUNCANCELLATION
- Subscription reactivationBILLING_ISSUE
- Payment failedSUBSCRIBER_ALIAS
- User account mergeNON_RENEWING_PURCHASE
- One-time purchaseSUBSCRIPTION_PAUSED
- Subscription pausedEXPIRATION
- Subscription expiredTRANSFER
- Subscription transferredREFUND
- Purchase refundedREFUND_REVERSED
- Refund reversedSUBSCRIPTION_EXTENDED
- Subscription extendedTRIAL_STARTED
- Trial period startedTRIAL_CANCELLED
- Trial cancelledTRIAL_CONVERTED
- Trial converted to paidINVOICE_ISSUANCE
- Invoice generatedTEMPORARY_ENTITLEMENT_GRANT
- Temporary access grantedVIRTUAL_CURRENCY_TRANSACTION
- Virtual currency transactionTEST
- Test event
# Start local development server
npm run dev
The worker will be available at http://localhost:8787
Purchase notifications include:
- Event type with emoji
- User ID
- Product information
- Price and currency
- Transaction details
- Store and environment
- Renewal information
- Expiration and purchase dates
- Country and pricing details
- Family sharing status
- Offering information
- Subscriber attributes
- Entitlements and expiration dates
- Timestamp
- Webhook signatures are verified using HMAC-SHA256
- All sensitive data are stored as Cloudflare Worker secrets
- CORS headers are configured for cross-origin requests
- Idempotency is implemented to prevent duplicate processing
-
Webhook not receiving events:
- Check that your RevenueCat webhook URL is correct and accessible
- Verify the webhook is enabled in RevenueCat dashboard
- Check worker logs:
wrangler tail
-
Telegram messages not sending:
- Verify your bot token and chat ID are correct
- Ensure you've started a conversation with your bot
- Check if the bot has permission to send messages
-
Signature validation failing:
- Ensure your webhook secret matches the one in RevenueCat
- Check that the secret is properly set:
wrangler secret list
-
KV storage not working:
- Verify KV namespace is properly configured in
wrangler.toml
- Check that the KV binding is available in your worker
- Verify KV namespace is properly configured in
-
Environment variables not available:
- Verify secrets are set:
wrangler secret list
- Check the
/test/env
endpoint for debugging
- Verify secrets are set:
# View worker logs
wrangler tail
# List secrets
wrangler secret list
# Test environment
curl https://your-worker.workers.dev/test/env
# Check KV namespace
wrangler kv:namespace list
- View analytics in the Cloudflare dashboard
- Monitor request volume and response times
- Set up alerts for errors
- Use the
/health
endpoint for uptime monitoring - Monitor webhook delivery in RevenueCat dashboard
- Check Telegram message delivery manually
- Cloudflare Workers: Free tier includes 100,000 requests/day
- KV storage: Free tier includes 100,000 read/write operations/day
- Monitor usage in Cloudflare dashboard
MIT License
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
For issues and questions:
- Check the troubleshooting section above
- Review Cloudflare Workers documentation
- Check RevenueCat webhook documentation
- Open an issue in the repository