-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send telegram message when scheduled job completes
Improve logging situation
- Loading branch information
1 parent
d4f3328
commit 573e79a
Showing
12 changed files
with
145 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
PORT=1234 | ||
DEBUG=true | ||
|
||
DEBUG=false | ||
TELEGRAM_BOT_TOKEN=abcd | ||
TELEGRAM_CHAT_ID=efgh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
{ | ||
"parser": "@babel/eslint-parser", | ||
"parserOptions": { | ||
"ecmaVersion": 2021, | ||
"sourceType": "module", | ||
"babelOptions": { | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-react" | ||
] | ||
} | ||
}, | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
}, | ||
"plugins": [ | ||
"react" | ||
], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:react/recommended" | ||
], | ||
"rules": {} | ||
} | ||
"parser": "@babel/eslint-parser", | ||
"parserOptions": { | ||
"ecmaVersion": 2021, | ||
"sourceType": "module", | ||
"babelOptions": { | ||
"presets": ["@babel/preset-env", "@babel/preset-react"] | ||
} | ||
}, | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
}, | ||
"plugins": ["react", "@typescript-eslint"], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:react/recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"rules": { | ||
"@typescript-eslint/explicit-module-boundary-types": "error", | ||
"@typescript-eslint/explicit-function-return-type": "error" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function debug(...args: any[]): void { | ||
if (process.env["DEBUG"] === "true") { | ||
console.log("$", ...args); | ||
} | ||
} | ||
|
||
export function log(...args: any[]): void { | ||
console.log(">>", ...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const sendTelegramMessage = async (message: string) => { | ||
const botToken = process.env.TELEGRAM_BOT_TOKEN; | ||
const chatId = process.env.TELEGRAM_CHAT_ID; | ||
const url = `https://api.telegram.org/bot${botToken}/sendMessage`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
chat_id: chatId, | ||
text: message, | ||
}), | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`Error sending message: ${response.statusText}, ${JSON.stringify(data)}`, | ||
); | ||
} | ||
|
||
console.log("Message sent successfully."); | ||
} catch (error) { | ||
console.error("Error sending Telegram message:", error); | ||
} | ||
}; | ||
|
||
export default sendTelegramMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { fetchAndStoreArticles } from "./util/api"; | ||
import sendTelegramMessage from "./util/sendTelegramMessage"; | ||
import { newsSources } from "./util/newsSources"; | ||
import { log } from "./util/log"; | ||
|
||
const scheduleArticleUpdate = async () => { | ||
try { | ||
const articles = await fetchAndStoreArticles(); | ||
const successMessage = generateSuccessMessage(articles); | ||
log(successMessage); | ||
if (articles.length > 0) await sendTelegramMessage(successMessage); | ||
} catch (error) { | ||
const errorMessage = `Error fetching articles: ${JSON.stringify(error, null, 2)}`; | ||
log(errorMessage); | ||
await sendTelegramMessage(errorMessage); | ||
} | ||
}; | ||
|
||
const generateSuccessMessage = (articles: any[]) => { | ||
const counts = articles.reduce( | ||
(acc: Record<string, number>, article: any) => { | ||
acc[article.source] = (acc[article.source] || 0) + 1; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
|
||
const articleCounts = newsSources | ||
.map((source) => `${source.name}: ${counts[source.name] || 0}`) | ||
.join("\n"); | ||
|
||
return `Articles fetched and stored successfully.\n\n${articleCounts}\n\nVisit: https://hyperwave.codes`; | ||
}; | ||
|
||
// Run import every 15 minutes, unless someone gets mad | ||
setInterval(scheduleArticleUpdate, 1000 * 60 * 15); | ||
|
||
scheduleArticleUpdate(); |
Oops, something went wrong.