Add send latest post function with lastSentPostId remember

This commit is contained in:
Amir Husayn Panahifar 2025-04-08 02:52:35 +03:30
parent 240aed2061
commit de6d7f971e

View file

@ -1 +1,35 @@
console.log("Hello, World!");
import { getLatestPost } from "./apis/main.js";
import { sendMessage } from "./services/messageServices.js";
import { log } from "./utils/logger.js";
let lastSentPostId = null;
const sendLatestPost = async () => {
const post = await getLatestPost();
if (post) {
if (post.id !== lastSentPostId) {
const message = `- آخرین پست از انجمن #پارچ لینوکس\n` +
`عنوان موضوع: ${post.title}\n` +
`توضیحات: ${post.excerpt}\n` +
`پیوند: ${post.url}`;
try {
await sendMessage(message, { parse_mode: 'Markdown' });
lastSentPostId = post.id;
log.info("Latest post sent successfully");
} catch (error) {
log.error("Error sending message:", error);
}
} else {
log.info("No new post to send.");
}
} else {
log.warn("No post found to send");
}
};
setInterval(sendLatestPost, 60 * 60 * 1000);
sendLatestPost();