Add send latest post function with lastSentPostId remember
This commit is contained in:
parent
240aed2061
commit
de6d7f971e
1 changed files with 35 additions and 1 deletions
36
src/main.js
36
src/main.js
|
@ -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();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue