|
|
@@ -23,39 +23,54 @@ export async function handleScheduledEvent(
|
|
|
// Function to handle new event notifications
|
|
|
const handleNewEventNotifications = async () => {
|
|
|
try {
|
|
|
- // Get latest event ID from storage
|
|
|
- const storedLatestEventId = await env.COSMOE_STORAGE.get('latestEventId');
|
|
|
- let latestEventId = storedLatestEventId ? parseInt(storedLatestEventId) : 0;
|
|
|
-
|
|
|
+ // Get notified event IDs from storage
|
|
|
+ const storedNotifiedEventIds = await env.COSMOE_STORAGE.get('notifiedEventIds');
|
|
|
+ let notifiedEventIds: number[] = [];
|
|
|
+
|
|
|
+ if (storedNotifiedEventIds) {
|
|
|
+ // Use notifiedEventIds if exists
|
|
|
+ notifiedEventIds = JSON.parse(storedNotifiedEventIds);
|
|
|
+ } else {
|
|
|
+ // Compatibility: first deployment, initialize from latestEventId
|
|
|
+ const storedLatestEventId = await env.COSMOE_STORAGE.get('latestEventId');
|
|
|
+ const latestEventId = storedLatestEventId ? parseInt(storedLatestEventId) : 0;
|
|
|
+ if (latestEventId > 0) {
|
|
|
+ // Initialize notifiedEventIds with IDs from 1 to latestEventId
|
|
|
+ notifiedEventIds = Array.from({ length: latestEventId }, (_, i) => i + 1);
|
|
|
+ await env.COSMOE_STORAGE.put('notifiedEventIds', JSON.stringify(notifiedEventIds));
|
|
|
+ console.log(`Initialized notifiedEventIds with ${notifiedEventIds.length} IDs (1-${latestEventId})`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// Fetch all events
|
|
|
const eventsResult = await cosmoeClient.getEvents();
|
|
|
-
|
|
|
+
|
|
|
if (eventsResult.code === 200 && eventsResult.data.length > 0) {
|
|
|
- // Filter new events (those with ID greater than latestEventId)
|
|
|
+ // Filter new events (those not in notifiedEventIds)
|
|
|
const newEvents = eventsResult.data.filter((event: any) => {
|
|
|
// Convert event ID to number for comparison
|
|
|
const eventId = typeof event.id === 'string' ? parseInt(event.id) : event.id;
|
|
|
- return eventId > latestEventId;
|
|
|
+ return !notifiedEventIds.includes(eventId);
|
|
|
});
|
|
|
console.log('New events: ', newEvents.length);
|
|
|
-
|
|
|
+
|
|
|
if (newEvents.length > 0) {
|
|
|
// Get all registered user IDs from COSMOE_CREDENTIALS
|
|
|
const keys = await env.COSMOE_CREDENTIALS.list();
|
|
|
-
|
|
|
+
|
|
|
for (const newEvent of newEvents) {
|
|
|
// Send notification to each registered user
|
|
|
for (const key of keys.keys) {
|
|
|
try {
|
|
|
const userId = parseInt(key.name);
|
|
|
console.log(`Sending notification to user ${key.name}...`);
|
|
|
-
|
|
|
+
|
|
|
// Create a message for the new event
|
|
|
const message = `🎉 新活动上线: *${newEvent.name}*\n日期: ${newEvent.event_date}\n\n查看详情: /event\\_${newEvent.id}`;
|
|
|
-
|
|
|
+
|
|
|
// Send message to user via Telegram API
|
|
|
const sendResult = await bot.api.sendMessage(userId, message, { parse_mode: 'Markdown' });
|
|
|
-
|
|
|
+
|
|
|
// Check if message was sent successfully by verifying the result
|
|
|
if (sendResult && sendResult.message_id) {
|
|
|
console.log(`Successfully sent notification to user ${key.name}, message_id: ${sendResult.message_id}`);
|
|
|
@@ -66,16 +81,15 @@ export async function handleScheduledEvent(
|
|
|
console.error(`Error sending notification to user ${key.name}:`, error);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // Add event ID to notified list
|
|
|
+ const eventId = typeof newEvent.id === 'string' ? parseInt(newEvent.id) : newEvent.id;
|
|
|
+ notifiedEventIds.push(eventId);
|
|
|
}
|
|
|
-
|
|
|
- // Update the latest event ID to the highest new event ID
|
|
|
- const maxNewEventId = Math.max(...newEvents.map((event: any) =>
|
|
|
- typeof event.id === 'string' ? parseInt(event.id) : event.id
|
|
|
- ));
|
|
|
-
|
|
|
- if (maxNewEventId > latestEventId) {
|
|
|
- await env.COSMOE_STORAGE.put('latestEventId', maxNewEventId.toString());
|
|
|
- }
|
|
|
+
|
|
|
+ // Update notifiedEventIds in storage
|
|
|
+ await env.COSMOE_STORAGE.put('notifiedEventIds', JSON.stringify(notifiedEventIds));
|
|
|
+ console.log(`Updated notifiedEventIds, total: ${notifiedEventIds.length} events`);
|
|
|
}
|
|
|
}
|
|
|
} catch (error) {
|