This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
148 lines (131 loc) · 4.39 KB
/
preload.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
const axios = require("axios");
const { clipboard, ipcRenderer } = require("electron");
const cfg = require(`C:\\users\\${
require("os").userInfo().username
}\\.ics-placetel-config.json`);
let div = document.getElementById("Status");
let refreshTimestamp = document.getElementById("timestamp");
let version = document.getElementById("version");
let token = document.getElementById("token");
let debug = document.getElementById("debug");
let shortcut = document.getElementById("shortcut");
let nebenstelle = document.getElementById("nebenstelle");
let warning = document.getElementById("warning");
window.addEventListener("DOMContentLoaded", () => {
updateUser();
nebenstelle.textContent = cfg.placetel_data.si_id;
token.textContent = "exists";
shortcut.textContent = cfg.shortcut_key;
version.textContent = "v" + require("./package.json").version;
if (!cfg.refresh_time || cfg.refresh_time === 0)
warning.textContent =
"You have an outdated config file! Check latest changelog.";
let update_interval = cfg.refresh_time === 0 ? 10 : cfg.refresh_time;
setInterval(() => {
updateUser();
}, update_interval * 1000);
});
function updateUser() {
axios
.get("https://api.placetel.de/v2/call_center_agents", {
headers: {
Authorization: `Bearer ${cfg.placetel_data.api_token}`,
},
})
.then(function (response) {
let content = "";
for (const user of response.data) {
switch (user.status) {
case "offline":
content += `<b>${user.name}:</b> <button type="button" id="changeState" onClick="updateState(event)" class="btn btn-link"><span class="badge bg-danger" state="offline" placetel-id=${user.id} >${user.status}</span></button><br />`;
break;
case "online":
content += `<b>${user.name}:</b> <button type="button" id="changeState" onClick="updateState(event)" class="btn btn-link"><span placetel-id=${user.id} state="online" class="badge bg-success">${user.status}</span></button><br />`;
break;
default:
content += `<b>${user.name}:</b> <span class="badge bg-warning">${user.status}</span><br />`;
break;
}
div.innerHTML = content;
var today = new Date();
refreshTimestamp.innerHTML = `Last refresh: ${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}`;
}
})
.catch(function (error) {
div.textContent = error.response.data.error;
});
}
async function updateState(event) {
if (event.target.attributes["state"].value === "offline") {
var data = JSON.stringify({
status: "online",
});
} else {
var data = JSON.stringify({
status: "offline",
});
}
var config = {
method: "put",
url: `https://api.placetel.de/v2/call_center_agents/${event.target.attributes["placetel-id"].value}`,
headers: {
Authorization: `Bearer ${cfg.placetel_data.api_token}`,
"Content-Type": "application/json",
},
data: data,
};
await axios(config).catch(function (error) {
div.textContent = error.response.data.error;
});
debug.textContent = "Changed status from user";
setTimeout(() => {
updateUser();
}, 1000);
}
async function dial(number) {
var data = JSON.stringify({
sipuid: nebenstelle.textContent,
target: number,
from_name: "ics it-systems GmbH",
});
var config = {
method: "post",
url: "https://api.placetel.de/v2/calls",
headers: {
Authorization: `Bearer ${cfg.placetel_data.api_token}`,
"Content-Type": "application/json",
},
data: data,
};
axios(config)
.then(function (response) {
debug.innerHTML = "Dialed: " + number;
new Notification("Erfolgreich!", {
body: "Dialed: " + number,
});
})
.catch(function (error) {
console.log(error);
debug.innerHTML = `[${error.response.status}] ${error.response.data.error}`;
});
}
async function manualdial() {
let manualdialing = document.getElementById("manualdialing");
if (manualdialing.value) {
await dial(manualdialing.value);
manualdialing.value = "";
} else {
debug.innerHTML = "Keine Nummer vorhanden.";
}
}
ipcRenderer.on("dialkey", async () => {
const text = clipboard.readText();
if (nebenstelle.textContent != "") {
await dial(text);
} else {
debug.innerHTML = "Nebenstelle leer.";
}
});
ipcRenderer.on("warning", (text) => {
warning.textContent = text;
});