-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
307 lines (278 loc) · 10.5 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"use strict";
var tg = require("node-telegram-bot-api");
var st = require("node-storage");
var rq = require("request");
var fs = require("fs");
var ch = require("fs-cheerio");
process.on("uncaughtException", function(err){ console.log((err && err.stack) ? err.stack : err); });
//user data storage
var store = new st("./userdata");
require.extensions[".json"] = function(module, filename){ module.exports = fs.readFileSync(filename, "utf8"); };
var raw = require("./config.json");
var con = JSON.parse(raw);
var token = con.bot.telegram_token,
admin = con.bot.admins,
relsr = con.bot.path_to_html,
iexts = con.bot.allowed_image_types;
var perms = con.msg.no_permission,
binfo = con.msg.info_text,
about = con.msg.about_text,
ntext = con.msg.create_post_text,
ntcon = con.msg.create_post_text_confirm,
npics = con.msg.create_post_picture,
npcon = con.msg.create_post_picture_confirm,
nncon = con.msg.create_post_confirm,
ertxt = con.msg.invalid_text,
erimg = con.msg.invalid_image_url,
ertyp = con.msg.invalid_image_type,
uerrs = con.msg.unknown_error,
dtext = con.msg.delete_post,
ntfnd = con.msg.post_not_found,
dconf = con.msg.post_deleted,
cancd = con.msg.operation_canceled;
var md = con.msg.markdown;
var nptxt = null,
nppic = null;
var b = new tg(token, { polling: true });
console.log("Listening...");
function isset(_var) { return ((_var && _var != null && _var != "" ) ? true : false); }
function hasPerm(uid){ return (admin.indexOf(uid.toString()) > -1) ? true : false; }
function isURL(str) {
var pattern = new RegExp(
'^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))' +
'(\\:\\d+)?(\\/[-a-z\\d%_.~+&:]*)*(\\?[;&a-z\\d%_.,~+&:=-]*)?(\\#[-a-z\\d_]*)?$','i'
);
return pattern.test(str);
}
//converts an array to a nice text (example converts '["a","b","c"]' to 'a, b and c')
function prettifyArr(arr){
var x = arr.toString().replace(/\,/gi, ", ");
var p = x.lastIndexOf(',');
return x.substring(0, p) + " and" + x.substring(p + 1);
}
//download image to given folder
function dlImg(uri, dest, callback){
rq(uri, { encoding: null }, function(error, response, body){
fs.writeFile(dest, body, 'binary', function (err){
if (err) console.error(err);
callback(err);
});
});
}
b.on("message", (msg) => {
var txt = msg.text,
uid = msg.from.id,
key = uid.toString(),
mid = msg.chat.id;
//message send wrapper
function send(text){ b.sendMessage(mid, text, { parse_mode: (md ? "markdown" : "") }); }
//is the user in the middle of a process? (like entering a new post text)
function main(){
var userdata = store.get(key);
isset(userdata) ? followProcess(userdata) : initMsg();
}
function followProcess(data){
switch (data.toLowerCase()){
case "newpost_text": {
store.put(key, null);
if (isset(txt)){
if (txt.toLowerCase() == "!--c"){
store.put(key, null);
send(cancd);
return;
}
else nptxt = txt;
}
else {
send(ertxt + "\n\n" + ntext);
store.put(key, 'newpost_text');
break;
}
newPostPicture(uid);
break;
}
case "newpost_picture": {
store.put(key, null);
if (typeof msg.photo !== 'undefined' && msg.photo != null){
var fID = msg.photo[0].file_id;
var options = {
uri: "https:\/\/api.telegram.org\/bot" + token + "\/getFile?file_id=" + fID,
method: 'POST',
headers: { 'Content-Type': 'application/json' }
};
try {
rq(options, function(error, response, body){
var jsonraw = JSON.parse(body);
var fPath = jsonraw.result.file_path;
nppic = "https:\/\/api.telegram.org\/file\/bot" + token + "\/" + fPath;
newPostCall(nptxt, nppic, function(){ send(nncon); });
});
}
catch (err){ console.log(err); }
}
else if (isURL(txt)){
nppic = txt;
newPostCall(nptxt, nppic, function(){ send(nncon); });
}
else if (txt.toLowerCase() == "!--c"){
store.put(key, null);
send(cancd);
return;
}
//wrong input: repeat
else {
send(erimg + "\n\n" + npics);
store.put(key, 'newpost_picture');
}
break;
}
case "delete_post": {
store.put(key, null);
if (isNaN(txt) || txt <= 0){
store.put(key, null);
send(cancd);
return;
}
else {
ch.readFile(relsr).then(function($){
var count = $(".post").length;
var index = parseInt(txt) + 1;
if (txt > count) send(ntfnd);
else {
var imgpath = "./public_html/images/" + txt + "";
$("#post-" + txt).remove();
//fs.unlink(imgpath, function(err){ if (err) return console.log(err); });
send(dconf + "\n\nPost ID: " + txt);
//this will rearrange the remaining posts so there is a normal order again (1, 2, 3...)
for (index; index <= count; index++) $("#post-" + index).attr("id", "#post-" + (index - 1));
ch.writeFile(relsr, $);
}
});
}
break;
}
}
}
//Create new post call
function newPostCall(nptxt, nppic, callback){
//check if its a valid picture
var ext = nppic.split(".").pop().toLowerCase();
if (iexts.indexOf(ext) >= 0) {
ch.readFile(relsr).then(function($){
var count = $(".post").length;
count++;
var img = "./public_html/images/" + count + "." + ext;
var src = "./images/" + count + "." + ext;
dlImg(nppic, img, function(err){ if (err) send(uerrs); });
send(npcon);
$(".posts").append(
"<div class=\"post\" id=\"post-" + count + "\">\n" +
" <div class=\"post-image\">\n" +
" <img class=\"bigimg\" src=\"" + src + "\" />\n" +
" </div>\n" +
" <div class=\"post-text\">\n" +
" <p>" + nptxt + "</p>\n" +
" </div>\n" +
"</div>\n"
);
ch.writeFile(relsr, $);
});
callback();
}
else {
send(ertyp + "\n\nAllowed image types: " + prettifyArr(iexts) + "\nYour image: " + ext + "\n\n" + npics);
store.put(key, 'newpost_picture');
return;
}
}
//choice menu
function initMsg(){
//telegram buttons
b.sendMessage(mid, binfo, {
reply_markup: {
inline_keyboard: [
[{
text: "New Post",
callback_data: "new_post"
}],
[{
text: "List Posts",
callback_data: "list_post"
}],
[{
text: "Delete Post",
callback_data: "delete_post"
}],
[{
text: "Edit Posts",
callback_data: "edit_post"
}],
[
{
text: "About",
callback_data: "about_text"
},
{
text: "Source Code",
callback_data: "source_code"
}
]
]
},
//is markdown enabled in the config?
parse_mode: (md ? "markdown" : "")
}).then(function(x) {
//process input
b.on("callback_query", (callbackQuery) => {
//always check permissions; button could be old!
switch (callbackQuery.data){
case "new_post": {
hasPerm(uid) ? newPostText() : deny();
break;
}
case "list_post": {
hasPerm(uid) ? listPost() : deny();
break;
}
case "delete_post": {
hasPerm(uid) ? deletePost() : deny();
break;
}
case "edit_post": {
hasPerm(uid) ? editPost() : deny();
break;
}
case "about_text": {
send(about);
break;
}
case "source_code": {
send("https://github.com/NullDev/Static-Post-Bot");
break;
}
}
b.answerCallbackQuery(callbackQuery.id, null);
callbackQuery = null;
});
}).catch(console.error);
}
function deny(){ send(perms + "\n\nYour User ID is: " + uid) }
function newPostText(){
send(ntext);
store.put(key, 'newpost_text');
}
function newPostPicture(){
send(ntcon + "\n\n" + npics);
store.put(key, 'newpost_picture');
}
function listPost(uid){
}
function deletePost(){
send(dtext);
store.put(key, 'delete_post');
}
function editPost(uid){
}
//check user permissions and either deny or allow him to proceed
hasPerm(uid) ? main() : deny();
});