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
216 lines (173 loc) · 5.21 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const Storage = require("./db");
const Utils = require("./utils");
const config = require("./config");
const DB = new Storage();
const TELEGRAM_API_KEY = process.env.TELEGRAM_API_KEY;
const TELEGRAM_URL = `https://api.telegram.org/bot${TELEGRAM_API_KEY}/sendMessage`;
const app = express();
const socket = require("express-ws")(app);
const metascraper = require("metascraper")([
require("metascraper-author")(),
require("metascraper-description")(),
require("metascraper-image")(),
require("metascraper-title")(),
require("metascraper-url")()
]);
app.use(express.static("public"));
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 extractMetadata = text => {
let urls = Utils.extractURLS(text);
let tags = Utils.extractTags(text);
let cleanText = Utils.removeFromText(text, Utils.prependHashtags(tags), urls);
let quote = Utils.extractQuote(cleanText);
let comment = Utils.removeFromText(cleanText, [quote]);
text = quote || cleanText;
return { text, urls, tags, comment, quote };
};
const handleText = message => {
let metadata = extractMetadata(message.text);
if (metadata.text === "/start") {
reply(
message.chat.id,
config.TEXTS.start.replace("{u}", message.from.first_name)
);
} else if (metadata.text === "/help") {
reply(message.chat.id, config.TEXTS.commands);
} else if (metadata.text === "/ping") {
socket.getWss().clients.forEach(client => {
client.send(JSON.stringify({ ping: true }));
});
} else if (metadata.urls && metadata.urls.length) {
storeURLS(message, metadata);
} else if (metadata.quote) {
// storeQuote(message, metadata)
}
};
const storeQuote = (message, data) => {
data.user = message.from.username;
data.room = message.chat.title;
data.type = "quote";
Storage.store(data, onStorageResponse);
if (message.chat.type === "private") {
reply(message.chat.id, data);
}
};
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 comment = Utils.removeFromText(text, Utils.prependHashtags(tags), urls);
let type = "image";
let text = "";
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 = (message, data) => {
let username = message.from.username;
let room = message.chat.title;
let type = "website";
data.urls.forEach(url => {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = `http://${url}`;
}
axios
.get(url)
.then(async response => {
const html = response.data;
const metadata = await metascraper({ html, url });
const author = metadata.author;
const title = metadata.title;
const description = metadata.description;
DB.createLink({ author, title, description, url, username });
socket.getWss().clients.forEach(client => {
client.send(
JSON.stringify({ author, url, title, description, url, username })
);
});
})
.catch(e => {
console.log(e);
});
});
};
const onGetLinks = (request, response) => {
DB.getLinks()
.then(links => {
response.json({ links });
})
.catch(e => {
response.json({ links: [] });
});
};
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) {
handleText(message);
}
res.end("ok");
};
const onRoot = (request, response) => {
response.sendFile(__dirname + "/views/index.html");
};
const onReset = (request, response) => {
if (request.query.secret === process.env.SECRET) {
DB.recreateDatabase().then(() => {
response.json({ ok: true });
});
} else {
response.json({ ok: false });
}
};
app.get("/", onRoot);
app.get("/reset", onReset);
app.get("/api/links", onGetLinks);
app.post("/message", onMessage);
app.ws("/", (ws, req) => {
ws.on("message", message => {
console.log("Message", JSON.parse(message));
});
});
const listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});