2018-09-06 09:37:37 +00:00
|
|
|
//const transit = require('transit-js');
|
|
|
|
|
2018-09-16 23:47:52 +00:00
|
|
|
const MatrixWhisperBridge = require('./app/MatrixWhisperBridge');
|
2018-09-06 09:37:37 +00:00
|
|
|
const { decodeStatusPayload } = require('./utils/statusUtils');
|
|
|
|
|
|
|
|
const CHANNEL = 'noman-test';
|
|
|
|
|
2018-09-16 23:47:52 +00:00
|
|
|
const bridge = new MatrixWhisperBridge();
|
2018-09-06 09:37:37 +00:00
|
|
|
|
|
|
|
bridge.init().then(([whisperUtils]) => {
|
|
|
|
whisperUtils.getPublicKey().then(publicKey => {
|
|
|
|
whisperUtils.send(CHANNEL, 'Bot is alive');
|
|
|
|
|
|
|
|
whisperUtils.listen(CHANNEL).then(
|
|
|
|
subscription => {
|
|
|
|
subscription.on('data', ({ payload, sig, timestamp, topic }) => {
|
|
|
|
const [, [message]] = decodeStatusPayload(payload);
|
2018-09-06 09:59:41 +00:00
|
|
|
const isMe = publicKey === sig;
|
|
|
|
const name = isMe ? 'Bot' : sig.slice(0, 10);
|
2018-09-06 09:37:37 +00:00
|
|
|
|
|
|
|
console.log('From:', name, 'at', timestamp);
|
|
|
|
console.log('Topic:', topic);
|
|
|
|
console.log('Message:', message, '\n');
|
|
|
|
|
2018-09-06 09:59:41 +00:00
|
|
|
if (!isMe) {
|
2018-09-06 09:37:37 +00:00
|
|
|
whisperUtils.send(CHANNEL, `${message} back`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
error => console.error(error));
|
|
|
|
});
|