Skip to content

Instantly share code, notes, and snippets.

@mariosaputra
Created March 3, 2024 08:56
Show Gist options
  • Save mariosaputra/1c7a40b3dd858bcfed6c620d4c529539 to your computer and use it in GitHub Desktop.
Save mariosaputra/1c7a40b3dd858bcfed6c620d4c529539 to your computer and use it in GitHub Desktop.
RevenueCat Webhook
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const app = express();
const port = 3000;
const TELEGRAM_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const CHAT_ID = process.env.TELEGRAM_CHANNEL_ID;
const TELEGRAM_API_URL = `https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`;
app.use(express.json());
app.post("/webhook/revenuecat", async (req, res) => {
const eventData = req.body;
let message = `Received RevenueCat Webhook Event:\nType: ${eventData.event.type}\nProduct ID: ${eventData.event.product_id}\nPrice: ${eventData.event.price}\nCurrency: ${eventData.event.currency}`;
try {
const response = await axios.post(TELEGRAM_API_URL, {
chat_id: CHAT_ID,
text: message,
});
if (response.status === 200) {
console.log("Message sent successfully to Telegram");
res.send("Message sent successfully to Telegram");
} else {
console.error("Failed to send message to Telegram");
res.status(500).send("Failed to send message to Telegram");
}
} catch (error) {
console.error("Error sending message to Telegram:", error);
res.status(500).send("Error sending message to Telegram");
}
});
app.listen(port, () => {
console.log(`Webhook server listening at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment