-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (52 loc) · 1.74 KB
/
index.js
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
64
require('dotenv').config();
const chokidar = require('chokidar')
const {Markup} = require('telegraf')
const sha1 = require('sha1')
const bot = require('./bot')
const fs = require('fs')
const db = require('./lowdb/index')
const dir = process.env.DIR
const {orderInfo} = require('./utils/messageHtmlTemplates')
const formatJson = require('./utils/formatJson')
const deleteLink = require('./utils/deleteLink')
const getLinkFollowers = require('./utils/getLinkFollowers')
const sendTelegramMessage = async (userId, info) => {
if (info.photos.length) {
try {
info.photos.map(photo => {
fs.readFile(photo, (err, data) => {
bot.telegram.sendPhoto(userId, {source: photo})
})
})
} catch (e) {
console.error(e)
}
}
const hashLink = sha1(info.searchUrl)
const inlineMessageRatingKeyboard = Markup.inlineKeyboard([
Markup.callbackButton('Unfollow', hashLink),
])
bot.telegram.sendMessage(userId, orderInfo(info), {
parse_mode: "HTML",
reply_markup: inlineMessageRatingKeyboard
})
}
const startWatching = () => {
const watcher = chokidar.watch(dir, {
usePolling: false,
});
watcher.on('ready', async () => {
watcher.on('add', (path) => {
fs.readFile(path, {encoding: 'utf-8'}, (err, data) => {
const info = formatJson(data)
const linkFollowers = getLinkFollowers(info.searchUrl)
if (linkFollowers.length) {
linkFollowers.map(id => {
sendTelegramMessage(id, info)
})
}
})
})
})
}
startWatching();