This repository was archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (101 loc) · 3.88 KB
/
server.js
File metadata and controls
115 lines (101 loc) · 3.88 KB
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
var express = require('express'),
app = express(),
fs = require('fs'),
http = require('http'),
request = require('request'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser');
var server = http.createServer(app).listen(16000);
var BOT_AUTH = "";
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/webhook', function(req, res){
res.send("Thanks!");
if (req.body.message.text != undefined) {
var TelegramChatID = req.body.message.chat.id;
var message = req.body.message.text.split(" ");
var command = message[0];
var sentence = req.body.message.text.replace("/convert ", "").replace("/convert@LatexBot ", "").replace("/help ", "").replace("/help@LatexBot ", "").replace("/start ", "").replace("/convertDoc ", "").replace("/convertDoc@LatexBot ", "");
if (command == "/help" || command == "/help@LatexBot" || command == "/start") {
reply(TelegramChatID, "NEW: Now you can generate LaTeX Documents with _/convertDoc LaTeX_!");
reply(TelegramChatID, "Send me the LaTeX and receive the image!\nIn groups send: _/convert LaTeX_");
reply(TelegramChatID, "The LatexBot Source Code is available at [GitHub](https://github.com/luigifreitas/LatexBot). Created by @luigifreitas :D");
} else {
if (sentence) {
if (sentence.match(/\input{(.*?)\}/g)){
// Blacklist Commands
reply(TelegramChatID, "Command not available.");
return;
}
if (command == "/convert" || command == "/convert@LatexBot") {
renderImage(TelegramChatID, sentence);
} else if (command == "/convertDoc" || command == "/convertDoc@LatexBot") {
renderDoc(TelegramChatID, sentence);
} else if (req.body.message.chat.title == undefined) {
renderImage(TelegramChatID, sentence);
}
} else {
reply(TelegramChatID, "Cannot find any sentence.");
}
}
}
});
app.get('/status', function(req, res){
res.send("✅ LatexBot is up!")
});
function renderImage(TelegramChatID, Sentence) {
var path = __dirname + '/latex/' + tokenGenerator(10) + '.png';
var dest = fs.createWriteStream(path);
var render = require("mathmode")(Sentence).pipe(dest);
render.on('finish', function () {
replyImage(TelegramChatID, path);
});
}
function renderDoc(TelegramChatID, Sentence) {
var path = __dirname + '/latex/' + tokenGenerator(10) + '.pdf';
var dest = fs.createWriteStream(path);
var render = require("latex")(Sentence).pipe(dest)
render.on('finish', function () {
replyFile(TelegramChatID, path);
});
}
function replyImage(TelegramChatID, Path) {
var formData = {
chat_id: TelegramChatID,
photo: fs.createReadStream(Path)
};
request.post({url:'https://api.telegram.org/' + BOT_AUTH + '/sendPhoto', formData: formData}, function(err,httpResponse,body){
var response = JSON.parse(body);
if (!response.ok) {
replyFile(TelegramChatID, Path);
}
});
}
function replyFile(TelegramChatID, Path) {
var formData = {
chat_id: TelegramChatID,
document: fs.createReadStream(Path)
};
request.post({url:'https://api.telegram.org/' + BOT_AUTH + '/sendDocument', formData: formData}, function(err,httpResponse,body){
var response = JSON.parse(body);
if (!response.ok) {
reply(TelegramChatID, "Sorry, something happend with the image. Check the LaTeX syntax.");
}
});
}
function reply(TelegramChatID, Sentence) {
var formData = {
chat_id: TelegramChatID,
parse_mode: "Markdown",
text: Sentence
};
request.post({url:'https://api.telegram.org/' + BOT_AUTH + '/sendMessage', formData: formData});
}
function tokenGenerator(num){
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for( var i=0; i < num; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}