-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
76 lines (44 loc) · 1.5 KB
/
bot.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
// serial setup
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const port = new SerialPort('/dev/ttyACM0')
const parser = port.pipe(new Readline({ delimiter: '\r\n' }))
// cabal setup
var Headless = require('cabal-headless')
//var key = 'cabal:// [SOME CABAL KEY ]'
var opts = {}
var cabalkey = key.replace("cabal://", "").replace("cbl://", "")
var headless = Headless(cabalkey, { temp: opts.temp || false })
headless.nick('cabalbot')
headless.connect() // connect to the cabal's swarm
var current_channel = "bots"
/*
headless.onPeerConnected(function (peer) {
console.log("peer connected")
port.write('HELP\n') //write to serial port
})
*/
headless.onMessageReceived(function (msg) {
// log our new message to the console
console.log("new message", msg)
var txt = msg.value.content.text
current_channel = msg.value.content.channel
var parts = txt.split(" ")
if (parts[0]=="@cabalbot") {
// assume message if of form "@cabalbot ON" or "@cabalbot OFF"
var modality = parts[1];
var index = txt.indexOf(" ")
console.log(index)
var command=txt.slice(index+1,txt.length)
console.log(command)
port.write(command.concat('\n')) //write to serial port
}
})
parser.on('data', function (data) {
console.log('Data:', data)
headless.post({
messageType: "chat/text",
channel: current_channel,
message: data
})
})