-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpp.js
62 lines (55 loc) · 2.38 KB
/
xmpp.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
const { client, xml, jid } = require("@xmpp/client");
const debug = require("@xmpp/debug");
const botHelper = require('./botHelper');
module.exports = function () {
return {
address: null,
xmpp: null,
init: function (username, password, roomname, type, master) {
this.xmpp = client({
service: "wss://chitbuzz.com:5001/ws",
domain: "chitbuzz.com",
resource: `cbbot.${(Math.floor(Math.random() * 90000) + 10000)}`,
username: username,
password: password,
});
debug(this.xmpp, true);
var _self = this;
this.xmpp.on("error", (err) => {
console.error(err);
});
this.xmpp.on("offline", () => {
console.log("offline");
});
this.xmpp.on("stanza", async (stanza) => {
if (type == 'quiz' && stanza.is('message') && stanza.getChildText('body')) {
if (stanza.getChildText('body').trim().indexOf('bot@left') === 0)
_self.leaveRoom(`${roomname}@conference.chitbuzz.com`);
}
// if (stanza.is("message")) {
// await xmpp.send(xml("presence", { type: "unavailable" }));
// await xmpp.stop();
// }
});
this.xmpp.on("online", async (address) => {
// Makes itself available
await _self.xmpp.send(xml("presence"));
_self.address = address;
if (roomname)
_self.joinRoom(`${roomname}@conference.chitbuzz.com`);
//bind Bot Helper
const bot = new botHelper(_self.xmpp, `${roomname}@conference.chitbuzz.com`, master);
});
this.xmpp.start().catch(console.error);
return this.xmpp;
},
joinRoom: async function (roomJid) {
let messageJoin = xml('presence', { to: roomJid + '/' + this.address.local, type: 'available', from: this.address.toString() })
await this.xmpp.send(messageJoin);
},
leaveRoom: async function (roomJid) {
let messageLeft = xml('presence', { to: roomJid + '/' + this.address.local, type: 'unavailable', from: this.address.toString() })
await this.xmpp.send(messageLeft);
},
}
}