-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapi.js
146 lines (122 loc) · 3.81 KB
/
api.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
var express = require('express');
//var cookieParser = require('cookie-parser');
var http = require('http');
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//app.use(cookieParser());
app.use((req, res, next) => {
res.append('Access-Control-Allow-Origin', ['*']);
res.append('Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
next();
});
var save_function = null;
var exit_function = null;
var bot_send_function = null;
var bot_stats_function = null;
var bot_getUser_function = null;
app.get('/', function (req, res) {
save_function();
return res.json( {ok:"ok"} );
});
app.get('/shutdown', function (req, res) {
res.json( {shutdown:"ok"} );
exit_function({exit:true}, null);
});
app.get('/stats', function (req, res) {
return res.json( {stats:bot_stats_function()});
});
app.post('/send', function (req, res) {
if(req.body && req.body.text && req.body.channel){
//bot_send_function(req.body.channel, req.body.text);
bot_send_function.sendMessage(req.body.channel, req.body.text,{
parse_mode:"MarkdownV2"
}).then(status =>{
if(status){
return res.json({text_sent: "ok"});
}else{
return res.json({error: "could not send text"});
}
}).catch(err =>{
res.status(500);
return res.json({error: err});
});
}else{
return res.json({error: "could not send text"});
}
});
app.post('/getUser', function (req, res) {
if(req.body && req.body.chatId && req.body.userId){
let userSend = {};
bot_send_function.getChatMember(req.body.chatId, req.body.userId).then(user =>{
userSend = user;
return bot_send_function.getChat(req.body.chatId);
}).then(chat =>{
return res.json({chat: chat, user: userSend});
})
.catch(err =>{
res.status(500);
return res.json({error: err});
});
}else{
return res.json({error: "could not find user"});
}
});
var port = process.env.PORT || '19876';
app.set('port', port);
function onErrorServer(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListeningServer() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}
var set_save_function = (save_functionn) =>{
save_function = save_functionn;
};
var set_exit_function = (exit_functionn) =>{
exit_function = exit_functionn;
};
var set_bot_send_function = (send_function) =>{
bot_send_function = send_function;
};
var set_stats_function = (stats_function) =>{
bot_stats_function = stats_function;
};
var set_getUser_function = (getUser_function) =>{
bot_getUser_function = getUser_function;
};
var server = http.createServer(app);
server.listen(port);
server.on('error', onErrorServer);
server.on('listening', onListeningServer);
module.exports = {
server: server,
set_save_function: set_save_function,
set_exit_function: set_exit_function,
set_bot_send_function: set_bot_send_function,
set_stats_function: set_stats_function,
set_getUser_function: set_getUser_function
};