This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (198 loc) · 5.59 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const CQHttp = require('cqhttp');
const config = require('./config');
const pipelines = require('./app/gitlab/pipelines');
const mr = require('./app/gitlab/mr-service');
const PjServiceCheck = require('./app/gitlab/project-service-check');
// utils
const imgUtil = require('./utils/image');
// module
const SearchPicture = require('./app/search-picture');
const getTyphoonInfo = require('./app/typhoon.js');
const Corn = require('./corn');
const { apiRoot, accessToken, robotQQ, gitlab = {} } = config;
const devops_admin = gitlab.devops_admin || [];
const bot = new CQHttp({
apiRoot,
accessToken
});
const atLen = `t,qq=${robotQQ}]`.length;
bot.on('message', context => {
// console.log('message', context);
const { message, raw_message, message_type, group_id, user_id } = context;
console.log({ message, raw_message, message_type, group_id, user_id });
if (message_type === 'group') {
// at消息
let idx = message.indexOf(`at,qq=${robotQQ}]`);
let msg = message.substring(idx + atLen + 1) || '';
msg = msg.trim();
// console.log(msg);
if (idx === -1) {
return;
}
if (raw_message.includes('德玛西亚')) {
bot('send_msg', {
...context,
message: '人在塔在!'
});
return;
}
if (raw_message.includes('!!')) {
let idx = message.indexOf('!!');
const projectName = raw_message.substring(idx + 2) || '';
// console.log('。。。。进来了。。。。。', projectName);
if (!projectName.trim()) return;
pipelines
.queryPipelines(projectName.trim())
.then(result => {
bot('send_msg', {
...context,
message: result
});
})
.catch(err => {
bot('send_msg', {
...context,
message: err.errorMsg
});
});
return;
}
if (raw_message.includes('登录')) {
let idx = message.indexOf('登录');
let text = raw_message.substring(idx + 2) || '';
// console.log('。。。。进来了。。。。。', text);
text = text.trim();
if (!text) return;
let arr = text.split('#');
let [projectName = '', projectEnv = ''] = arr;
projectName = projectName.trim();
projectEnv = projectEnv.trim();
PjServiceCheck.triggerPipelineByProjectName(
projectName,
projectEnv,
group_id
).then(msg => {
bot('send_msg', {
...context,
message: msg
});
});
return;
}
if (raw_message.includes('合并')) {
if (!devops_admin.includes(String(user_id).trim())) {
bot('send_msg', {
...context,
message: `[CQ:at,qq=${user_id}]无权限操作`
});
return;
}
let idx = message.indexOf('合并');
let text = raw_message.substring(idx + 2) || '';
console.log('。。。。进来了。。。。。', text);
text = text.trim();
if (!text) return;
let arr = text.split('#');
let [projectName = '', sourceBranch = '', targetBranch = ''] = arr;
projectName = projectName.trim();
sourceBranch = sourceBranch.trim();
targetBranch = targetBranch.trim();
console.log(projectName, sourceBranch, targetBranch, user_id)
mr.autoMr(projectName, sourceBranch, targetBranch, user_id).then(msg => {
bot('send_msg', {
...context,
message: `[CQ:at,qq=${user_id}] ${msg}`
});
});
return;
}
msgHandler(msg, context, message_type);
return;
}
if (message_type === 'private') {
msgHandler(message, context);
return;
}
});
function msgHandler(text = '', context, messageType) {
text = text.trim();
// 图片搜索
if (text.indexOf('图 ') === 0) {
SearchPicture.getPicture(
text
.replace('图', '')
.replace('图片', '')
.trim()
)
.then(url => {
if (url) {
let imgUrl = imgUtil.getImageUrl(url);
bot('send_msg', {
...context,
message: imgUrl
});
}
})
.catch(err => {});
} else if (text.indexOf('查台风') === 0) {
// 台风查询
getTyphoonInfo().then(text => {
text = text || '当前没有台风!';
bot('send_msg', {
...context,
message: text
});
});
}
}
/* bot('send_msg', {
group_id: '807533895',
message:
'[第一部分][CQ:image,file=123.jpg]图片之后的部分,表情:[CQ:face,id=123]'
})
.then(() => {
console.log(11);
})
.catch(err => console.log(err)); */
/**
* 版本 4.x,请用 notice
*/
bot.on('notice', context => {
console.log('notice', context);
if (context.notice_type === 'group_increase') {
// 处理群成员添加事件
bot('get_group_member_info', {
group_id: context.group_id,
user_id: context.user_id
})
.then(data => {
const name = data.nickname || '新人';
bot('send_group_msg_async', {
group_id: context.group_id,
message: `欢迎 ${name} 同学入群~🌹🌹🌹`
}).catch(err => {});
})
.catch(err => {
console.log(err);
});
}
// 忽略其它事件
});
bot.on('request', context => {
console.log('request', context);
if (context.request_type === 'group') {
// 处理加群请求
if (context.message !== 'some-secret') {
return { approve: false, reason: '口令不对' };
}
return { approve: true };
}
// 忽略其它类型的请求
});
/* bot.on('error', err => {
console.log(err);
});
*/
bot.listen(8080, '0.0.0.0');
Corn(bot);
console.log('监听:8080, 0.0.0.0');