-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
137 lines (113 loc) · 3.36 KB
/
app.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
var http = require('http');
var config = require('./config.json');
var fs = require('fs');
var login = require('facebook-chat-api');
var mathmode = require('mathmode');
var mjAPI = require("./node_modules/mathjax-node/lib/mj-single");
var async = require('async');
var results = [];
var options = {
packages: ["amsmath", "amssymb"]
};
var port = process.env.PORT || 5000;
http.createServer(function(req, res) {
console.log("ping");
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end("");
}).listen(process.env.PORT || 5000);
//only use this if you are on free tier on heroku to keep bot from idling
setInterval(function() {
http.get((process.env.HEROKU_INSTANCE || config.heroku_instance), function(res) {
console.log("pong");
});
}, 300000);
//end
function isValidLatex(inputString) {
return (inputString.length !== 1 &&
inputString.slice(0, 1) === '$' &&
inputString.slice(-1) === '$');
}
function extractLatex(inputString) {
var length = inputString.length;
return inputString.slice(1, length - 1);
}
function populateResults(inputString) {
var resultArray = inputString.split("");
var curr;
var inMiddle = false;
for (var i = 0; i < resultArray.length; i++) {
if (resultArray[i] === '$') {
if (!inMiddle) {
curr = "";
} else {
results.push(curr);
}
inMiddle = !inMiddle; //flip switch
} else {
if (inMiddle) {
curr += resultArray[i];
}
}
}
}
login({
email: process.env.FB_USERNAME || config.username,
password: process.env.FB_PASSWORD || config.password
}, function callback(err, api) {
if (err) return console.error(err);
api.setOptions({
listenEvents: true
});
var interpret = api.listen(function(err, event) {
if (err) return console.error(err);
if (event.type === "message") {
populateResults(event.body);
console.log(results);
mjAPI.start();
mjAPI.config({
MathJax: {
SVG: {
font: "TeX"
}
},
extensions: ""
});
for (var i = 0; i < results.length; i++) {
mjAPI.typeset({
math: results[i],
format: "inline-TeX", // "inline-TeX", "MathML"
png: true, // svg:true,
dpi: 800,
ex: 50,
width: 100
}, function(data) {
//console.log(data);
if (!data.errors) {
var base64Data = data.png.replace(/^data:image\/png;base64,/, "");
var filename = 'file' + i + '.png';
fs.writeFile(filename, base64Data, 'base64', function(err) {
if (err) {
throw err;
} else {
var msg = {
attachment: fs.createReadStream(filename)
};
console.log('writing image');
api.sendMessage(msg, event.threadID);
}
fs.unlink(filename, function(err) {
console.log("deleting temp file: " + filename);
});
});
}
});
} // end of for loop
results = [];
api.markAsRead(event.threadID, function(err) {
if (err) console.log(err);
});
}
});
});