This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
69 lines (47 loc) · 1.62 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
65
66
67
68
69
const wfWatcher = require('./webfail_watcher.js')
const download = require('download')
const fse = require('fs-extra')
const tgBot = require('node-telegram-bot-api')
// Create bot instance
const bot = new tgBot(process.env.WEBFAIL_BOT_TOKEN)
// Watch german webfail posts
const deWatcher = new wfWatcher('https://de.webfail.com/rss')
deWatcher.on('post', post => postNewPost(post, process.env.WEBFAIL_DE_CHANNEL))
// Watch english webfail posts
const enWatcher = new wfWatcher('https://en.webfail.com/rss')
enWatcher.on('post', post => postNewPost(post, process.env.WEBFAIL_EN_CHANNEL))
async function postNewPost({title, link, imgUrl, isGif, id, video, twitter}, channelId) {
const params = {
caption: `<a href="${link}">${title}</a>` + (twitter?'\nQuelle: <a href="'+twitter+'">Twitter</a>':''),
parse_mode: 'HTML'
}
if (video) {
bot.sendMessage(channelId, `📽 <a href="${video}">${title}</a>`, {parse_mode: 'HTML'})
}
// Post photo
else if (!isGif) {
bot.sendPhoto(channelId, imgUrl, params)
}
// Post gif
else {
const dir = 'tmp/webfailgif'+id
try {
// Delete eventually existing old images
try {
await fse.remove(dir+'/post2.jpg')
} catch (e2) {}
// Download, change extension and send
await download(imgUrl, dir)
await fse.move(dir+'/post2.jpg',dir+'/post2.gif')
await bot.sendDocument(channelId, dir+'/post2.gif', params)
} catch (e) {
console.log(e)
} finally {
try {
// Remove image
await fse.remove(dir+'/post2.gif')
await fse.removeDir(dir+'/')
} catch (e2) {}
}
}
}