-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompactor.js
More file actions
138 lines (114 loc) · 4.71 KB
/
compactor.js
File metadata and controls
138 lines (114 loc) · 4.71 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
process.env["NTBA_FIX_319"] = 1;
process.env["NTBA_FIX_350"] = 1;
process.on('uncaughtException', function (error) {
console.log("\x1b[31m", "Exception: ", error, "\x1b[0m");
});
process.on('unhandledRejection', function (error, p) {
console.log("\x1b[31m","Error: ", error.message, "\x1b[0m");
});
var config = require('./config.js');
var TelegramBot = require('node-telegram-bot-api');
var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var bodyParser = require('body-parser');
console.log('Connecting bot...');
var token = config.compactortoken;
var bot = new TelegramBot(token);
var app = express();
var path = "/compactor/bot" + token;
var port = 25005;
bot.setWebHook('https://fenixweb.net:8443' + path);
app.listen(port);
app.use(bodyParser.json());
app.post(path, function(req, res) {
bot.processUpdate(req.body);
res.sendStatus(200);
});
console.log('Starting bot...');
process.on('SIGINT', function() {
console.log("Shutting down bot...");
process.exit();
});
process.on('SIGTERM', function() {
console.log("Shutting down bot...");
process.exit();
});
var html = {
parse_mode: "HTML"
};
var mergeMessages = [];
bot.on('message', function (message) {
if (message.chat.id < 0) {
if (message.text != undefined)
if (message.text.startsWith("/"))
console.log(getNow("it") + " - " + message.from.username + ": " + message.text);
if ((message.from.is_bot == 0) && (message.text != undefined) && (message.text.indexOf("http") == -1)){
if ((message.reply_to_message == undefined) && (!message.text.startsWith("/")) && (message.forward_date == undefined)){
if ((mergeMessages[message.chat.id] != undefined) && (mergeMessages[message.chat.id] != "")){
if (mergeMessages[message.chat.id].split(";")[0] == message.from.id){
bot.deleteMessage(message.chat.id, mergeMessages[message.chat.id].split(";")[1]);
bot.deleteMessage(message.chat.id, message.message_id);
var newText = mergeMessages[message.chat.id].split(";")[2] + "\n" + cleanForMerge(message.text);
bot.sendMessage(message.chat.id, "@" + message.from.username + " <i>scrive</i>:\n" + newText, html).then(function (data) {
mergeMessages[message.chat.id] = message.from.id + ";" + data.message_id + ";" + newText;
});
} else
mergeMessages[message.chat.id] = message.from.id + ";" + message.message_id + ";" + cleanForMerge(message.text);
} else
mergeMessages[message.chat.id] = message.from.id + ";" + message.message_id + ";" + cleanForMerge(message.text);
} else
mergeMessages[message.chat.id] = ""; // skip replies, commands and forwards
} else
mergeMessages[message.chat.id] = ""; // skip bots
}
});
bot.on('edited_message', function (message) {
if (message.chat.id < 0) {
if ((mergeMessages[message.chat.id] != undefined) && (mergeMessages[message.chat.id] != "")){
if (mergeMessages[message.chat.id].split(";")[0] == message.from.id){
var lastIdx = mergeMessages[message.chat.id].lastIndexOf(";");
mergeMessages[message.chat.id] = mergeMessages[message.chat.id].substr(0, lastIdx+1);
mergeMessages[message.chat.id] += cleanForMerge(message.text);
}
}
}
});
bot.onText(/^\/start/i, function (message) {
bot.sendMessage(message.chat.id, "<b>Welcome to The Compactor!</b>\n\nAdd this bot to groups to compact consequential messages into one to avoid flood.\n\n<b>Important: You must set the bot as admin!</b>", html);
});
bot.onText(/^\/check/i, function (message) {
if (message.chat.id < 0) {
bot.sendMessage(message.chat.id, "If you can see the command you have written, The Compactor is not an Admin and will not work in this group.", html);
bot.deleteMessage(message.chat.id, message.message_id);
} else
bot.sendMessage(message.chat.id, "You can use this command only in a group");
});
// Functions
function cleanForMerge(text) {
return text.replaceAll(/<[^>]*>/, "");
}
function addZero(i) {
if (i < 10)
i = "0" + i;
return i;
}
function getNow(lang, obj) {
var d = new Date();
obj = typeof obj !== 'undefined' ? obj : false;
var datetime;
if (lang == "it") {
datetime = addZero(d.getDate()) + "/" + addZero(d.getMonth() + 1) + "/" + d.getFullYear() + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
} else if (lang == "en") {
datetime = d.getFullYear() + "-" + addZero(d.getMonth() + 1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
} else
datetime = "Language not specified";
if (obj == true)
datetime = new Date(datetime);
return datetime;
}
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};