-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (84 loc) · 2.72 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
const axios = require('axios');
axios.defaults.headers.post['Content-Type'] = 'application/json';
class QyMsg {
constructor(options, isProduction) {
if (typeof options == 'string') {
let key = options
options = { keys: { defaultGroup: key } }
}
this.keys = options.keys || {};
this.onlyOneKey = Object.keys(this.keys).length == 1;
this.keys.defaultGroup = this.keys[Object.keys(this.keys)[0]];
this.baseURL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=';
this.log = options.log;
this.isProduction = isProduction;
axios.interceptors.request.use(function (config) {
if (options.log) console.log('config->', config)
return config;
}, function (error) {
return Promise.reject(error);
});
console.log('QyMsg注册信息:', this.keys);
}
async push(groupName, content, users = []) {
if (this.onlyOneKey) {
users = content;
content = groupName;
groupName = 'defaultGroup';
} else if (arguments.length == 1 || (arguments.length == 2 && Array.isArray(content))) {
throw new Error('缺少groupName参数');
}
if (!this.keys[groupName]) throw new Error(`缺少${groupName}的key`);
if (!this.isProduction) {
console.log(`[qymsg] 非生产模式:`, content)
} else {
await axios.post(this.baseURL + this.keys[groupName], {
msgtype: 'text',
text: {
content,
mentioned_mobile_list: users
}
})
}
}
async pushToAll(groupName, content) {
if (this.onlyOneKey) {
content = groupName;
groupName = 'defaultGroup';
} else if (arguments.length == 1) {
throw new Error('缺少groupName参数');
}
if (!this.keys[groupName]) throw new Error(`缺少${groupName}的key`);
if (!this.isProduction) {
console.log(`[qymsg] 非生产模式:`, content)
} else {
await axios.post(this.baseURL + this.keys[groupName], {
msgtype: 'text',
text: {
content,
mentioned_mobile_list: ['@all']
}
})
}
}
async markdown(groupName, content) {
if (this.onlyOneKey) {
content = groupName;
groupName = 'defaultGroup';
} else if (arguments.length == 1) {
throw new Error('缺少groupName参数');
}
if (!this.keys[groupName]) throw new Error(`缺少${groupName}的key`);
if (!this.isProduction) {
console.log(`[qymsg] 非生产模式:`, content)
} else {
await axios.post(this.baseURL + this.keys[groupName], {
msgtype: 'markdown',
markdown: {
content
}
})
}
}
}
module.exports = QyMsg;