-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (127 loc) · 3.45 KB
/
index.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
const express = require("express");
class vk_event {
constructor(type, object, group_id, event_id, secret) {
this.type = type
this.object = object
this.group_id = group_id
this.event_id = event_id
this.secret = secret
}
}
class group {
constructor(id, return_str, secret_key) {
this.id = id;
this.return_str = return_str;
this.secret_key = secret_key;
}
}
function getUrl(host, port, ssl_protected = false) {
if (ssl_protected == true) {
if (port == 443) {
return `https://${host}`;
}
else return `https://${host}:${port}`;
}
else {
if (port == 80) {
return `http://${host}`;
}
else return `http://${host}:${port}`;
}
}
class VkEvent_handler {
constructor (eventFunction, check = () => {}) {
this.eventFunction = eventFunction;
this.check = check;
}
}
defaultHandler = new VkEvent_handler((group_obj, Event) => {
console.log('Needs code\n==========');
console.log(`Group: ${group_obj.id}, Event:\ntype: ${Event.type}\ngroup_id: ${Event.group_id}\nevent_id: ${Event.event_id}\nsecret: ${Event.secret}\nobject:`, Event.object);
}, () => console.log('[WARNING] You not connect vk event handler: VkEvent_handler'));
class server {
constructor(groups = [], _event = defaultHandler, host='localhost', port = 80, logger=false) {
this.host = host;
this.port = port;
this.groups = groups;
this.logger = Boolean(logger);
_event.check();
this._event = _event.eventFunction;
this.group_dict = new Object();
for (var index = 0; index < this.groups.length; ++index) {
var item = this.groups[index];
this.group_dict[item.id] = item;
}
}
log (event) {
if (this.logger) console.log(event);
}
confirmation_secret (group_obj, Event, getTimeConfiramtor = (group_obj, Event) => true) {
// console.log(123);
if (Event.secret != group_obj.secret_key) {
return "<strong>Invalid secret key</strong>";
}
else if (Event.type == 'confirmation') {
return group_obj.return_str;
}
else {
if (
!getTimeConfiramtor(group_obj, Event)
) {
// console.log(123);
return "<b>Request timeouted</b>";
}
else {
setTimeout(async () => this._event(group_obj, Event), 10);
return 'ok';
}
}
}
start(callback = () => {}, print_serverInfo = console.log) {
let app = express();
app.use(express.json());
app.route('/:group_id').post(
async (req, res) => {
try {
var group_id = Number(req.params.group_id);
group_id = String(group_id);
var POST_DATA = req.body;
if (this.group_dict[group_id] !== undefined) {
var callback_event = new vk_event(
POST_DATA.type,
POST_DATA.object,
POST_DATA.group_id,
POST_DATA.event_id,
POST_DATA.secret
);
this.log(callback_event);
res.send(this.confirmation_secret(this.group_dict[group_id], callback_event));
}
else {
res.send('group_id failed');
}
}
catch (err) {
console.log(`Raised error: ${err.message}\n======================\n${err.stack}`);
res.status(500).send(`<b>Raised error: ${err.message}</b><br/>\n${err.stack}`);
}
}
).get(async (req, res) => { res.send('<strong>Invalid secret key</strong>') });
app.listen(this.port, this.host, (err) => {
if (err) {
throw err;
}
else {
print_serverInfo(`vk callback server listen in ${getUrl(this.host, this.port)}`);
callback();
}
});
this.server = app;
}
}
module.exports = {
VkEvent: vk_event,
Group: group,
Server: server,
VkEventHandler: VkEvent_handler
};