-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
303 lines (250 loc) · 8.52 KB
/
main.ts
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const fs = require("fs");
// load configuration of homeserver, etc.
const config = JSON.parse(fs.readFileSync("env.json"));
// get the esp-udp stuff
import * as espudp from "./lib-esp32-udp/esp-udp";
// get the matrix-bot-sdk stuff
import { MatrixClient, SimpleFsStorageProvider } from "matrix-bot-sdk";
// matrix bot init
const storage = new SimpleFsStorageProvider(config.storage);
const bot = new MatrixClient(config.homeserverUrl, config.accessToken, storage);
const botChar = "$";
let lastRoomId: string = config.defaultRoomId;
let listenContinousFlag: boolean = false;
let heartbeat_last: string = "never";
const helpText = `Available commands:
${botChar}temperature
${botChar}pressure
${botChar}all
${botChar}listen <subcommand>
${botChar}heartbeat <subcommand>
${botChar}help
${botChar}bot
${botChar}whoami
${botChar}hello
${botChar}panic
${botChar}echo <message>
${botChar}admin <message>
For help on the commands just type the commands without further arguments`;
const helpListen = `Available subcommands:
on
off
interval <number>`;
const helpHeartbeat = `Available subcommands:
on
off
interval <number>
last`
// turn off continuous listening
export function listenOff() {
listenContinousFlag = false;
}
// turn on continuous listening
export function listenOn() {
listenContinousFlag = true;
}
// Send a message `body` to `roomId` of type `mstype`
// msgtype is either "text" or "notice"
function bot_send(roomId: string, msgtype: string, body: string) {
let content: object = {};
// specify the message type via variable
switch (msgtype) {
case "text":
content = {
"msgtype": "m.text",
"body": body
};
break;
case "notice":
default:
content = {
"msgtype": "m.notice",
"body": body
};
break;
}
bot.sendMessage(roomId, content);
console.log(`<< ${roomId}: ${body}`);
}
// Send the `msg` to different rooms debending on `level`
// level is "info", "error" or "debug"
function bot_reply(level: string, msg: string) {
let roomId: string[] = [config.defaultRoomId];
switch (level) {
case "response":
// try sending to the last room that asked
if (lastRoomId != "") {
roomId = [lastRoomId];
} else {
console.log("Empty room id, send it to default room.");
}
roomId.forEach(id => {
bot_reply_code(id, msg);
});
break;
case "measurement":
if (listenContinousFlag) {
roomId = [lastRoomId];
}
else {
roomId = [];
}
roomId.forEach(id => {
bot_reply_code(id, msg);
});
break;
case "heartbeat":
heartbeat_last = msg;
msg = `Got heartbeat ${heartbeat_last}`;
roomId.forEach(id => {
bot_send(id, "notice", msg);
});
break;
case "error":
// send to the last room and the default room
if (lastRoomId != "") {
roomId.push(lastRoomId);
}
roomId.forEach(id => {
bot_send(id, "notice", msg);
});
break;
case "debug":
default:
// send to the default room
roomId.forEach(id => {
bot_send(id, "notice", msg);
});
break;
}
}
// unused function
// format the message as source code
function bot_reply_code(id: string, msg: string) {
bot.sendMessage(id, {
"msgtype": "m.notice",
"body": msg,
"format": "org.matrix.custom.html",
"formatted_body": `<code>${msg}</code>`
});
console.log(`<< ${id}: ${msg}`);
}
// function handle for incoming matrix messages
// event is the default matrix event object
function matrix_message_handle(roomId: string, event: object) {
// ignore emptly events
if (!event["content"]) return;
// ignore non-text events
if (event["content"]["msgtype"] !== "m.text") return;
// get message info
const sender = event["sender"];
const body = event["content"]["body"];
// only listen on messages starting with the bot char
if (body.startsWith(botChar)) {
// remember room id
lastRoomId = roomId;
console.log(`>> ${roomId}: ${sender} ${body}`);
// split message into words, omitting the bot character
let words: string[] = body.substring(1).toLowerCase().split(" ");
let keyWord: string = words[0];
// replace parts of the words with the full word
if ("temperature".startsWith(keyWord)) {
keyWord = "temperature";
} else if ("pressure".startsWith(keyWord)) {
keyWord = "pressure";
} else if ("listen".startsWith(keyWord)) {
keyWord = "listen";
} else if ("heartbeat".startsWith(keyWord)) {
keyWord = "heartbeat";
}
switch (keyWord) {
case "temperature":
espudp.get("temperature");
break;
case "pressure":
espudp.get("pressure");
break;
case "all":
espudp.get("all");
break;
case "heartbeat":
case "hb":
switch (words[1]) {
case "on":
espudp.set("heartbeat", "on");
break;
case "off":
espudp.set("heartbeat", "off");
break;
case "interval":
espudp.set("heartbeat_interval", parseInt(words[2]));
break;
case "last":
bot_send(roomId, "notice", `Last heartbeat: ${heartbeat_last}`);
break;
default:
bot_send(roomId, "notice", helpHeartbeat);
break;
}
break;
case "listen":
switch (words[1]) {
case "on":
listenOn();
bot_send(roomId, "notice", "Start listening for periodic measurements.");
break;
case "off":
listenOff();
bot_send(roomId, "notice", "Stop listening for periodic measurements.");
break;
case "interval":
espudp.set("listen_interval", parseInt(words[2]));
break;
default:
bot_send(roomId, "notice", helpListen);
break;
}
break;
case "bot":
bot_send(roomId, "notice", "I am a bot.");
break;
case "whoami":
bot_send(roomId, "notice", sender);
break
case "hello":
bot_send(roomId, "notice", `Hello ${sender.slice(1, sender.indexOf(':'))}!`);
break
case "echo":
bot_send(roomId, "notice", body.substring("!echo".length).trim());
break;
case "admin":
bot_send(config.defaultRoomId, "notice", `${sender}: ${body.substring("!admin".length).trim()}`);
bot_send(lastRoomId, "notice", "Your message was send to the admin.");
break;
case "help":
// standard help
bot_send(roomId, "notice", helpText);
break;
case "panic":
bot_send(roomId, "text", "🧻");
break;
case "easteregg":
bot_send(roomId, "text", "🥚");
break;
default:
bot_send(roomId, "notice", `How can I help you?\nYou can write '${botChar}help' for help.`);
break;
}
}
}
espudp.loggingDisable()
// espudp.loggingEnable()
// start the udp stuff and specify the callback for received udp messages
espudp.start(config.ipAddress, config.port, config.key, bot_reply);
// specify the handler function for matrix messages
bot.on("room.message", matrix_message_handle);
// start the bot and send a message to the default room
bot.start().then(() => {
console.log("Bot started!");
bot_send(config.defaultRoomId, "notice", "Bot started!");
});