forked from hczhcz/telegram-kuso-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.wx.js
96 lines (77 loc) · 2.4 KB
/
bot.wx.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
'use strict';
const Wechat = require('wechat4u');
const qrcode = require('qrcode-terminal');
process.on('uncaughtException', (err) => {
console.error(Date());
console.error(err);
});
module.exports = () => {
const bot = new Wechat();
bot.start();
bot.on('uuid', (uuid) => {
qrcode.generate(
'https://login.weixin.qq.com/l/' + uuid,
{
small: true,
},
(qr) => {
console.warn(qr);
}
);
});
bot.onText = (re, event) => {
bot.on('message', (msg) => {
if (
!msg.isSendBySelf
&& msg.MsgType === bot.CONF.MSGTYPE_TEXT
) {
// mock
msg.message_id = 0;
const tgUser = (user) => {
return {
username: bot.contacts[user].getDisplayName(),
first_name: bot.contacts[user].getDisplayName(),
id: user,
};
};
const tgGroup = (user) => {
return {
username: bot.contacts[user].getDisplayName(),
first_name: bot.contacts[user].getDisplayName(),
id: user,
};
};
if (msg.FromUserName.slice(0, 2) === '@@') {
const content = msg.OriginalContent.split(':<br/>');
msg.from = tgUser(content[0]);
msg.chat = tgGroup(msg.FromUserName);
msg.text = content[1];
} else {
msg.from = tgUser(msg.FromUserName);
msg.chat = tgUser(msg.FromUserName);
msg.text = msg.Content;
}
const match = msg.text.match(re);
if (match) {
event(msg, match);
}
}
});
};
bot.sendMessage = (user, text, options) => {
// TODO: callback query
return bot.sendText(text, user);
};
bot.getChatAdministrators = (chat) => {
// notice: temporary solution
// TODO
return Promise.resolve([]);
};
bot.on('login', () => {
console.warn('login');
});
bot.on('logout', () => {
console.warn('logout');
});
return bot;
};