This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
201 lines (172 loc) · 5.96 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
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
200
201
'use strict';
const fs = require('fs');
const http = require('http');
const tg = require('telegraf');
const qq = require('qq-bot-rebown');
const SocksProxyAgent = require('socks-proxy-agent');
const config = require('./config');
const telegrafOpt = { telegram: {}, msgId: null, qrMsgId: null };
if (config.tg.proxy) {
telegrafOpt.telegram.agent = new SocksProxyAgent(config.tg.proxy);
}
const tgBot = new tg(config.tg.bot_token, telegrafOpt);
let qqOpt = { online: false };
if (config.qq.id && config.qq.pwd) {
Object.assign(qqOpt, {
app: { login: qq.QQ.LOGIN.PWD },
auth: { u: config.qq.id, p: config.qq.pwd }
});
}
const qqBot = new qq.QQ(qqOpt);
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url.startsWith(config.server.get_qr_path)) {
fs.createReadStream(qqBot.options.qrcodePath).pipe(res);
}
});
server.listen(config.server.port);
function notifyManager(...args) {
if (telegrafOpt.msgId) {
return tgBot.telegram.editMessageText(config.tg.manager_id, telegrafOpt.msgId, null, ...args);
}
return tgBot.telegram
.sendMessage(config.tg.manager_id, ...args)
.then(ret => {
telegrafOpt.msgId = ret.message_id;
return ret;
});
}
function sendQRImage(...args) {
return tgBot.telegram
.sendPhoto(config.tg.manager_id, ...args)
.then(ret => {
telegrafOpt.qrMsgId = ret.message_id;
return ret;
});
}
function deleteQRImage() {
if (telegrafOpt.qrMsgId) {
tgBot.telegram.deleteMessage(config.tg.manager_id, telegrafOpt.qrMsgId);
telegrafOpt.qrMsgId = null;
}
}
// Set bot username
tgBot.telegram.getMe().then((botInfo) => {
tgBot.options.username = botInfo.username;
});
tgBot.command('whoami', (ctx) => {
ctx.reply(`Your Telegram uid is \`${ctx.message.from.id}\``, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
});
tgBot.command('whereisthis', (ctx) => {
let message;
switch (ctx.message.chat.type) {
case 'group':
message = `This group uid is \`${ctx.message.chat.id}\``;
break;
case 'private':
message = `This private chat id as well as your uid is \`${ctx.message.chat.id}\``;
break;
default:
message = `Chat type: ${ctx.message.chat.type}; Chat id \`${ctx.message.chat.id}\``;
}
ctx.reply(message, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
});
/* eslint-disable no-case-declarations */
function tgMsgHandler(ctx, next, handler) {
if (!qqBot._alive) return;
const message = ctx.message || ctx.editedMessage;
switch (message.chat.type) {
case 'group':
case 'supergroup':
const { id, username } = message.chat;
config.rules.forEach(r => {
if (r.tg_chat_id == id || r.tg_chat_id == username) {
switch (r.type) {
case '2way':
if (!r.gid) r.gid = qqBot.group
.filter(g => g.name == r.qq_group_name)
.map(g => g.gid)
.pop();
const msg = {
name: message.from.username || `${message.from.first_name} ${message.from.last_name || ''}`,
content: handler(message)
};
const text = config.qq.transformMsg(msg);
// stroe last sent msg, to avoid duplicate forward
r.lastMsg = text;
qqBot.sendGroupMsg(r.gid, text);
break;
default:
break;
}
}
});
break;
default:
break;
}
}
tgBot.on(['text', 'edited_message'], (ctx, next) => tgMsgHandler(ctx, next, message => message.text));
tgBot.on('sticker', (ctx, next) => tgMsgHandler(ctx, next, message => `(Sticker) ${message.sticker.emoji}`));
tgBot.startPolling();
qqBot.on('group', msg => {
config.rules.forEach(r => {
if (r.qq_group_name === msg.groupName) {
switch (r.type) {
case '2way':
// do not forward if this msg was sent by myself just now
if (msg.content === r.lastMsg) return;
tgBot.telegram.sendMessage(
r.tg_chat_id,
config.tg.transformMsg(msg),
{ parse_mode: 'HTML' }
);
break;
case 'qq2tg':
for (let kwd of r.listen_keywords) {
if (msg.content.includes(kwd)) {
tgBot.telegram.sendMessage(
r.tg_chat_id,
config.tg.transformMsg(msg, kwd),
{ parse_mode: 'HTML' }
);
return;
}
}
break;
default:
break;
}
}
});
});
qqBot.on('login', () => {
if (qqOpt.online) return;
notifyManager('Login...');
});
qqBot.on('qr', (path, img) => {
deleteQRImage();
sendQRImage({ source: img });
});
qqBot.on('qr-expire', () => deleteQRImage());
qqBot.on('start-poll', () => {
if (qqOpt.online) return;
notifyManager(`${qqBot.selfInfo.nick} online`);
qqOpt.online = true;
deleteQRImage();
});
qqBot.on('cookie-expire', () => {
qqOpt.online = false;
// send new message when cookie expired
telegrafOpt.msgId = null;
telegrafOpt.qrMsgId = null;
})
qqBot.on('error', err => {
notifyManager(`QQ Bot error:\n<pre>${err}</pre>`, { parse_mode: 'HTML' });
})
qqBot.run();