forked from RocketChat/Rocket.Chat.Omnichannel.Load
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agents.js
93 lines (74 loc) · 3 KB
/
agents.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
import { request } from "undici";
import ShortUniqueId from 'short-unique-id';
import { parseHost } from "./utils.js";
import buzzphrase from "buzzphrase";
const uid = new ShortUniqueId({ length: 10 });
const host = parseHost(process.env.HOST);
const departmentId = process.env.DEPARTMENT || process.argv[4];
export async function getCurrentAgentList(onlyAvailable = true) {
if (departmentId) {
const response = await request(`${ host }api/v1/livechat/department/${ departmentId }`);
const { agents } = response.body.json();
return agents;
}
const response = await request(`${ host }api/v1/livechat/users/agent?onlyAvailable=${ onlyAvailable }`);
const { agents } = response.body.json();
return agents;
}
export async function checkAgents(adminUsername, adminPassword) {
if (!adminUsername || !adminPassword) {
throw new Error('Admin username and password are required when creating agents');
}
return createAgentUser(adminUsername, adminPassword, departmentId);
}
async function createAgentUser(adminUsername, adminPassword, departmentId) {
const userData = uid();
const user = await request(`${ host }api/v1/users.create`, { method: 'POST', headers: {
'content-type': 'application/json',
'X-User-Id': adminUsername,
'X-Auth-Token': adminPassword,
}, body: JSON.stringify({
email: `${uid()}@rocket.chat`,
joinDefaultChannels: false,
username: userData,
password: 'test',
roles: ['livechat-agent', 'user', 'livechat-manager'],
verified: true,
name: 'testerio',
}) });
const result = await user.body.json();
if (!result.user) {
console.error(result);
throw new Error('Error creating agent user');
}
if (departmentId) {
await request(`${ host }api/v1/livechat/department/${ departmentId }/agents`, { method: 'POST', headers: {
'content-type': 'application/json',
'X-User-Id': adminUsername,
'X-Auth-Token': adminPassword,
}, body: JSON.stringify({ upsert: [{ username: result.user.username, agentId: result.user._id, count: 0, order: 0 }] }) });
}
return {
username: userData,
password: 'test',
};
}
export async function sendAgentMessage(room, agent) {
const result = await request(`${ host }api/v1/method.call/sendMessage`, { method: 'POST', headers: {
'content-type': 'application/json',
'X-User-Id': agent.userId,
'X-Auth-Token': agent.authToken,
}, body: JSON.stringify({ message: JSON.stringify({
method: 'sendMessage',
params: [{ rid: room._id, msg: buzzphrase.get({ iterations: 2 }) }],
id: 'id',
msg: 'method',
})}) });
const response = await result.body.json();
console.log(`\tAgent ${ agent.userId } sent message to room ${ room._id }. Success: ${ !response.error }`);
}
export async function answerRoom(room, agent) {
const response = await request(`${ host }api/v1/login`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ username: agent.username, password: agent.password }) });
const { authToken, userId } = (await response.body.json()).data;
return sendAgentMessage(room, { authToken, userId });
}