This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
114 lines (85 loc) · 2.7 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const axios = require('axios')
const Utils = require('./utils')
const Storage = require('./storage')
const TELEGRAM_API_KEY = process.env.TELEGRAM_API_KEY
const TELEGRAM_URL = `https://api.telegram.org/bot${TELEGRAM_API_KEY}/sendMessage`
app.use(express.static('public'))
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/index.html')
})
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true
})
)
const showError = (error) => {
console.error(error)
}
const reply = (chat_id, text) => {
return axios.post(TELEGRAM_URL, { chat_id, text })
}
const onStorageResponse = (response) => {
console.log('Saved!', response)
}
const handleURLS = (message) => {
let text = message.text
let urls = Utils.extractURLS(text)
if (!urls) {
return
}
let tags = Utils.extractTags(text)
let user = message.from.username
let room = message.chat.title
let comment = Utils.extractDescription(text, Utils.prependHashtags(tags), urls)
storeURLS(room, user, comment, urls, tags)
}
const handleAvatar = (message) => {
handlePhoto(message, message.new_chat_photo)
}
const handlePhoto = (message, photo = undefined) => {
let text = message.caption
let tags = Utils.extractTags(text)
let urls = Utils.extractURLS(text)
photo = !photo ? message.photo : photo
let file_id = photo[photo.length - 1].file_id
let url = `https://api.telegram.org/bot${TELEGRAM_API_KEY}/getFile?file_id=${file_id}`
const onGetResponse = (response) => {
let user = message.from.username
let room = message.chat.title
let type = 'image'
let text = 'test'
let comment = Utils.extractDescription(text, Utils.prependHashtags(tags), urls)
let url = `https://api.telegram.org/file/bot${TELEGRAM_API_KEY}/${response.data.result.file_path}`
Storage.store({ url, text, type, comment, tags, room, user }, onStorageResponse)
}
axios.get(url).then(onGetResponse)
}
const storeURLS = (room, user, comment, urls, tags) => {
let type = 'website'
let text = ''
urls.forEach((url) => {
Storage.store({ url, text, type, comment, tags, room, user }, onStorageResponse)
})
}
const onMessage = (req, res) => {
const { message } = req.body
if (!message) {
return res.end()
}
if (message.photo) {
handlePhoto(message)
} else if (message.new_chat_photo) {
handleAvatar(message)
} else if (message.text) {
handleURLS(message)
}
res.end('ok')
}
app.post('/message', onMessage)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port)
})