-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
55 lines (49 loc) · 1.51 KB
/
index.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
const http = require('http');
const express = require('express');
const SocketIO = require('socket.io');
const bodyParser = require('body-parser');
const fs = require('fs');
const yaml = require('js-yaml');
const YveBot = require('../../lib/core');
const app = express();
const server = http.Server(app);
const io = SocketIO(server);
const example = yaml.load(fs.readFileSync(__dirname + '/../chat.yaml', 'utf8'));
const bot = new YveBot(example);
bot
.on('typing', sid => io.to(sid).emit('typing'))
.on('typed', sid => io.to(sid).emit('typed'))
.on('talk', (message, data, sid) => {
io.to(sid).emit('talk', {
message,
data,
});
})
.on('storeChanged', (data, sid) => io.to(sid).emit('storeChanged', data))
.on('error', (err, sid) => {
console.error(sid, err);
io.to(sid).emit('error', err.message);
})
.on('end', (_, sid) => {
if (io.sockets.sockets[sid]) {
io.sockets.sockets[sid].disconnect();
}
});
io.on('connection', chat => {
chat
.on('join', () => {
chat.emit('connected', chat.id);
chat.join(chat.id);
bot.session(chat.id).start();
})
.on('reply', ({ sid, store, message }) => {
bot.session(sid, { store }).hear(message);
});
});
app.use(bodyParser.json());
app.use('/', express.static(__dirname));
app.use('/chat.css', express.static(__dirname + '/../web/chat.css'));
app.use('/web.js', express.static(__dirname + '/../../lib/web.js'));
server.listen(3000, () => {
console.log('Yve server example listening on port 3000');
});