-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
63 lines (57 loc) · 2.07 KB
/
bot.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
/*
http://stackoverflow.com/questions/2561702/how-to-fit-the-paper-size-to-the-content-in-latex
http://tex.stackexchange.com/questions/184537/bad-math-environment-delimiter-in-standalone-class
http://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution
http://stackoverflow.com/questions/3482901/is-it-possible-to-compile-a-latex-document-via-node-js
*/
require('dotenv').config();
var login = require("facebook-chat-api");
var fs = require('fs');
var Mustache = require('mustache');
var sys = require('sys');
var spawn = require('child_process').spawn;
var async = require('async');
login({email: process.env.FB_EMAIL, password: process.env.FB_PASSWORD}, function callback (err, api) {
if(err) return console.error(err);
var q = async.queue(function (task, cb) {
fs.writeFile("temp-files/temp.tex", task.latexCode, function(err) {
if(err) {
return console.log(err);
}
});
var pdflatex = spawn('pdflatex', ['-output-directory', 'temp-files/', 'temp-files/temp.tex']);
pdflatex.on('exit', function(code) {
console.log('pdflatex exited with code ' + code);
var convert = spawn('convert', ['-density', '300', '-quality', '100', 'temp-files/temp.pdf', 'temp-files/equation.jpg']);
convert.on('exit', function(code) {
console.log('convert exited with code ' + code);
var msgToSend = { body: task.equationText,
attachment: fs.createReadStream("temp-files/equation.jpg")};
api.sendMessage(msgToSend, task.threadID);
cb();
});
});
});
api.listen(function callback(err, messageRecd) {
if(messageRecd.body)
{
var matches = messageRecd.body.match(/^\$(.*)\$$/);
if(matches)
{
var view = {
equationText: matches[1]
};
fs.readFile("latex.mst", "utf-8", function(err, template) {
if(err) {
return console.log(err);
}
var latexCode = Mustache.render(template, view);
console.log(latexCode);
q.push({ threadID: messageRecd.threadID,
equationText: view.equationText,
latexCode: latexCode});
});
}
}
});
});