-
Notifications
You must be signed in to change notification settings - Fork 359
/
server.js
98 lines (84 loc) · 3.12 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
const request = require('request');
const compression = require('compression');
const cors = require('cors');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(express.static('dist', {index: 'demo.html', maxage: '4h'}));
app.use(bodyParser.json());
// handle admin Telegram messages
app.post('/hook', function(req, res){
try {
const message = req.body.message || req.body.channel_post;
const chatId = message.chat.id;
const name = message.chat.first_name || message.chat.title || "admin";
const text = message.text || "";
const reply = message.reply_to_message;
if (text.startsWith("/start")) {
console.log("/start chatId " + chatId);
sendTelegramMessage(chatId,
"*Welcome to Intergram* \n" +
"Your unique chat id is `" + chatId + "`\n" +
"Use it to link between the embedded chat and this telegram chat",
"Markdown");
} else if (reply) {
let replyText = reply.text || "";
let userId = replyText.split(':')[0];
io.to(userId).emit(chatId + "-" + userId, {name, text, from: 'admin'});
} else if (text){
io.emit(chatId, {name, text, from: 'admin'});
}
} catch (e) {
console.error("hook error", e, req.body);
}
res.statusCode = 200;
res.end();
});
// handle chat visitors websocket messages
io.on('connection', function(socket){
socket.on('register', function(registerMsg){
let userId = registerMsg.userId;
let chatId = registerMsg.chatId;
let messageReceived = false;
socket.join(userId);
console.log("useId " + userId + " connected to chatId " + chatId);
socket.on('message', function(msg) {
messageReceived = true;
io.to(userId).emit(chatId + "-" + userId, msg);
let visitorName = msg.visitorName ? "[" + msg.visitorName + "]: " : "";
sendTelegramMessage(chatId, userId + ":" + visitorName + " " + msg.text);
});
socket.on('disconnect', function(){
if (messageReceived) {
sendTelegramMessage(chatId, userId + " has left");
}
});
});
});
function sendTelegramMessage(chatId, text, parseMode) {
request
.post('https://api.telegram.org/bot' + process.env.TELEGRAM_TOKEN + '/sendMessage')
.form({
"chat_id": chatId,
"text": text,
"parse_mode": parseMode
});
}
app.post('/usage-start', cors(), function(req, res) {
console.log('usage from', req.query.host);
res.statusCode = 200;
res.end();
});
// left here until the cache expires
app.post('/usage-end', cors(), function(req, res) {
res.statusCode = 200;
res.end();
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening on port:' + (process.env.PORT || 3000));
});
app.get("/.well-known/acme-challenge/:content", (req, res) => {
res.send(process.env.CERTBOT_RESPONSE);
});