-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpServer.js
55 lines (47 loc) · 1.32 KB
/
httpServer.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
import Koa from 'koa';
import Router from '@koa/router';
import bodyParser from 'koa-bodyparser';
export async function startHttpServer(bot, messageQueue) {
const app = new Koa();
const router = new Router();
app.use(bodyParser());
router.post('/send/broadcast', async (ctx) => {
const { message, roomIds, contactIds } = ctx.request.body;
if (!message || (!roomIds && !contactIds) ||
(roomIds && !Array.isArray(roomIds)) ||
(contactIds && !Array.isArray(contactIds))) {
ctx.status = 400;
ctx.body = { error: '参数错误' };
return;
}
try {
// 发送到群
if (roomIds) {
for (const roomId of roomIds) {
await messageQueue.enqueue({
type: 'group',
text: message,
room: roomId,
});
}
}
// 发送到个人
if (contactIds) {
for (const contactId of contactIds) {
await messageQueue.enqueue({
type: 'single',
text: message,
from: contactId,
});
}
}
ctx.body = { success: true };
} catch (error) {
console.error('群发消息出错:', error);
ctx.status = 500;
ctx.body = { error: '发送失败' };
}
});
app.use(router.routes());
app.listen(process.env.PORT || 3000);
}