-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathecho_bot.js
More file actions
62 lines (47 loc) · 1.8 KB
/
echo_bot.js
File metadata and controls
62 lines (47 loc) · 1.8 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
62
import Constants from "../src/constants.js";
import TCPConnection from "../src/connection/tcp_connection.js";
import NodeJSSerialConnection from "../src/connection/nodejs_serial_connection.js";
// create connection
// const connection = new TCPConnection("10.1.0.226", 5000);
const connection = new NodeJSSerialConnection("/dev/cu.usbmodem14401");
// wait until connected
connection.on("connected", async () => {
// we are now connected
console.log("Connected");
// update clock on meshcore device
await connection.syncDeviceTime();
// send flood advert when connected
await connection.sendFloodAdvert();
});
// listen for new messages
connection.on(Constants.PushCodes.MsgWaiting, async () => {
try {
const waitingMessages = await connection.getWaitingMessages();
for(const message of waitingMessages){
if(message.contactMessage){
await onContactMessageReceived(message.contactMessage);
} else if(message.channelMessage) {
await onChannelMessageReceived(message.channelMessage);
}
}
} catch(e) {
console.log(e);
}
});
async function onContactMessageReceived(message) {
console.log("Received contact message", message);
// find first contact matching pub key prefix
const contact = await connection.findContactByPublicKeyPrefix(message.pubKeyPrefix);
if(!contact){
console.log("Did not find contact for received message");
return;
}
// send it back
await connection.sendTextMessage(contact.publicKey, message.text, Constants.TxtTypes.Plain);
}
async function onChannelMessageReceived(message) {
console.log(`Received channel message`, message);
}
// todo auto reconnect on disconnect
// connect to meshcore device
await connection.connect();