-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
199 lines (159 loc) · 5.5 KB
/
server.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
'use strict';
const fs = require('fs');
const express = require('express');
const https = require('https');
const Telegraf = require('telegraf');
const Extra = require('telegraf/extra')
const session = require('telegraf/session')
const Markup = require('telegraf/markup')
const { reply } = Telegraf
const configObj = require('../config.json');
const environment = process.env.NODE_ENV || 'development';
const defaultConfig = configObj['development'];
const environmentConfig = configObj[environment];
const finalConfig = Object.assign(defaultConfig, environmentConfig);
//const Config = config.development;
// as a best practice best
// all global variables should be referenced via global. syntax
// and their names should always begin with g
global.gConfig = finalConfig;
// log global.gConfig
console.log(`global.gConfig: ${JSON.stringify(global.gConfig, undefined, global.gConfig.json_indentation)}`);
const util = require('./helpers/util');
const dataService = require('./helpers/dataService');
dataService.loadUsers();
const bot = new Telegraf(process.env.BOT_TOKEN || global.gConfig.bot_token )
util.setBot(bot);
// // Register session middleware
bot.use(session())
// Register logger middleware
bot.use((ctx, next) => {
const start = new Date()
return next().then(() => {
const ms = new Date() - start
console.log('response time %sms', ms)
})
})
bot.catch((err) => {
console.error('Error', err)
})
bot.command('start', ctx => {
util.logMsg(ctx);
dataService.registerUser(ctx);
//dataService.setCounter(ctx.chat.id, '0', 0);
var m = "Hello, I'm your AGP bot, please ask admin to add you to group";
ctx.reply(m);
util.logOutMsg(ctx, m);
setTimeout(() => {
ctx.reply(0);
util.logOutMsg(ctx, 0)
}, 50); //workaround to send this message definitely as second message
});
//bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply('👍'))
bot.hears('hi', (ctx) => ctx.reply('Hey there!'))
bot.hears('log', (ctx) => {
ctx.reply('See log!');
util.logMsg(ctx);
})
bot.launch()
// Constants
const PORT = global.gConfig.port;
const HOST = global.gConfig.host;
const SSL_KEY = global.gConfig.ssl_key;
const SSL_CERT = global.gConfig.ssl_cert;
const SSL_PASSPHRASE = global.gConfig.ssl_passphrase;
// App
const app = express();
// remove header X-Powered-By:express
app.disable('x-powered-by');
// or
// app.use(function (req, res, next) {
// res.removeHeader("X-Powered-By");
// next();
// });
// create an error with .status. we
// can then use the property in our
// custom error handler (Connect repects this prop as well)
function error(status, msg) {
var err = new Error(msg);
err.status = status;
return err;
}
// here we validate the API key,
// by mounting this middleware to /api
// meaning only paths prefixed with "/api"
// will cause this middleware to be invoked
app.use('/api', function(req, res, next){
var key = req.query['api-key'];
// key isn't present
if (!key) return next(error(400, 'api key required'));
// key is invalid
if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));
// all good, store req.key for route access
req.key = key;
next();
});
// map of valid api keys, typically mapped to
// account info with some sort of database like redis.
// api keys do _not_ serve as authentication, merely to
// track API usage or help prevent malicious behavior etc.
var apiKeys = ['agppass'];
// we now can assume the api key is valid,
// and simply expose the data
var users = [
{ name: 'tobi' }
, { name: 'loki' }
, { name: 'jane' }
];
// example: http://localhost:3000/api/users/?api-key=agppass
app.get('/api/users', function(req, res, next){
res.send(users);
});
// example: http://localhost:3000/api/users/?api-key=agppass
app.get('/api/sendtoadmin', function(req, res, next){
var resp ={'is_error':0, error: 'message', data: {} };
// var msg = req.query.msg.toString() | 'Default message';
var msg = !req.query.msg ? 'Default message' : req.query.msg;
util.sendToAdmin(`sendtoadmin: ${msg.toString()}`);
Object.assign(resp.data, {'message':msg});
res.json(resp);
});
// middleware with an arity of 4 are considered
// error handling middleware. When you next(err)
// it will be passed through the defined middleware
// in order, but ONLY those with an arity of 4, ignoring
// regular middleware.
app.use(function(err, req, res, next){
// whatever you want here, feel free to populate
// properties on `err` to treat it differently in here.
res.status(err.status || 500);
res.send({ 'is_error':1, error: err.message });
});
// our custom JSON 404 middleware. Since it's placed last
// it will be the last middleware called, if all others
// invoke next() and do not respond.
app.use(function(req, res){
res.status(404);
util.sendToAdmin(`err authorize: ${req.ip}`);
res.send({ 'is_error':0, error: "404.Authorize and use TLS" });
});
process.on('unhandledRejection', (e) => {
console.log(e);
// sendToAdmin(`Unhandled Rejection! ${e.message}`);
});
process.on('uncaughtException', (e) => {
console.log(e);
// sendToAdmin(`Uncaught Exception! ${e.message}`);
});
// https://hackernoon.com/set-up-ssl-in-nodejs-and-express-using-openssl-f2529eab5bb
// we will pass our 'app' to 'https' server
https.createServer({
key: fs.readFileSync(SSL_KEY, 'utf8'),
cert: fs.readFileSync(SSL_CERT, 'utf8'),
passphrase: SSL_PASSPHRASE
}, app)
.listen(PORT, HOST);
// app.listen(PORT, HOST);
console.log(`-> Running bot on https://${HOST}:${PORT}`);