Repository navigation
Expand file tree
/
Copy pathtest-bot-interaction.js
More file actions
61 lines (50 loc) · 1.47 KB
/
Copy pathtest-bot-interaction.js
File metadata and controls
61 lines (50 loc) · 1.47 KB
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
import { client, xml } from "@xmpp/client";
const xmpp = client({
service: "xmpp://localhost:5222",
domain: "xmpp",
username: "danja",
password: "Claudiopup",
tls: { rejectUnauthorized: false },
});
xmpp.on("error", (err) => {
console.error("XMPP Error:", err.message);
process.exit(1);
});
xmpp.on("offline", () => {
console.log("Disconnected");
process.exit(0);
});
xmpp.on("online", async (address) => {
console.log("Connected as", address.toString());
// Send presence
await xmpp.send(xml("presence"));
console.log("Sent presence");
// Join the MUC room
const roomJid = "general@conference.xmpp";
const nickname = "danja";
await xmpp.send(xml("presence", { to: `${roomJid}/${nickname}` }));
console.log(`Joined room: ${roomJid}`);
// Wait a moment then send message to bot
setTimeout(async () => {
console.log("Sending message to MistralBot...");
await xmpp.send(xml(
"message",
{ type: "groupchat", to: roomJid },
xml("body", {}, "MistralBot, what is 2+2?")
));
// Wait for response then exit
setTimeout(() => {
console.log("Test complete");
xmpp.stop();
}, 8000);
}, 2000);
});
xmpp.on("stanza", (stanza) => {
if (stanza.is("message") && stanza.getChildText("body")) {
const from = stanza.attrs.from;
const body = stanza.getChildText("body");
console.log(`Message from ${from}: ${body}`);
}
});
console.log("Starting test...");
xmpp.start().catch(console.error);