Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added deployed script for send scheduled notification #3034

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions send-scheduled-notifications/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const sdk = require("node-appwrite");
const axios = require("axios");

const client = new sdk.Client();
client.setEndpoint(process.env.PUBLIC_API_URL).setProject(process.env.APPWRITE_PROJECT_ID).setKey(process.env.PUBLIC_APPWRITE_API_KEY);

const database = new sdk.Databases(client, "event");

function cleanAndTrimContent(html) {
const plainText = html.replace(/<[^>]*>/g, "");
return plainText.substring(0, 100);
}

async function sendNotification(userId, content) {
const ONESIGNAL_APP_ID = process.env.ONESIGNAL_APP_ID;
const ONESIGNAL_API_KEY = process.env.ONESIGNAL_API_KEY;

try {
const response = await axios.post(
"https://onesignal.com/api/v1/notifications",
{
app_id: ONESIGNAL_APP_ID,
include_external_user_ids: [userId],
contents: { en: content },
},
{
headers: {
Authorization: `Basic ${ONESIGNAL_API_KEY}`,
"Content-Type": "application/json",
},
}
);
return response.data;
} catch (error) {
console.error("Error sending notification:", error);
throw error;
}
}

async function processNotifications() {
try {
const now = new Date().toISOString();

const notifications = await database.listDocuments("66dabdc8001707f3431d", [sdk.Query.equal("Confirm", true), sdk.Query.lessThanEqual("PushDate", now)]);

for (const notification of notifications.documents) {
const recipientUserIds = notification.RecipientUserId;
const trimmedContent = cleanAndTrimContent(notification.Content);

for (const userId of recipientUserIds) {
await sendNotification(userId, trimmedContent);
}

console.log(`Notifications sent for: ${notification.Title}`);
}
} catch (error) {
console.error("Error processing notifications:", error);
}
}

processNotifications();
9 changes: 9 additions & 0 deletions send-scheduled-notifications/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "send-scheduled-notifications",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"axios": "^0.21.1",
"node-appwrite": "^6.0.0"
}
}
Loading