-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
63 lines (51 loc) · 1.75 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import 'dotenv/config'
import TelegramBot from "node-telegram-bot-api";
import Parser from 'rss-parser';
import { JSONPreset } from 'lowdb/node';
const defaultData = { links: [] }
const db = await JSONPreset('db.json', defaultData)
const parser = new Parser();
const upworkURL = `${process.env.UPWORK_URL}?`
+ `q=${process.env.UPWORK_QUERY}&` // Query
+ `sort=recency&verified_payment_only=1&paging=0%3B10&api_params=1&` // Filters
+ `securityToken=${process.env.UPWORK_SEQURITY_TOKEN}&userUid=${process.env.UPWORK_USER_UID}&orgUid=${process.env.UPWORK_ORG_UID}`; // Auth
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, { polling: true });
bot.setMyCommands([
{ command: '/start', description: 'Hello message' },
{ command: '/clear', description: 'Clear the database' },
])
bot.onText(/\/start/, (msg) => {
bot.sendMessage(msg.chat.id, 'Hello, I\'m Upwork RSS Bot');
});
bot.onText(/\/clear/, () => {
db.data.links = [];
db.write()
})
const checkForNewLinks = async () => {
try {
const feed = await parser.parseURL(upworkURL);
feed.items.forEach((item) => {
if (db.data.links.includes(item.link)) return;
console.log('Written:', item.link);
db.data.links.push(item.link);
bot.sendMessage(
process.env.TELEGRAM_CHAT_ID,
`<b>${item.title}</b>`
+ '\n\n'
+ `${item['content']
.replaceAll('<br />', '\n')
.replaceAll('\n\n', '\n')
.replaceAll('•', '•')
.replaceAll('"', '"')
.replaceAll(' ', ' ')
.replace(/\ {5,10}/g, ' ')}`,
{ parse_mode: 'HTML' }
);
});
db.write();
} catch (error) {
console.log('Error:', error);
}
}
checkForNewLinks();
setInterval(() => checkForNewLinks(), 30000);