-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcommand_bot.js
More file actions
82 lines (64 loc) · 2.38 KB
/
command_bot.js
File metadata and controls
82 lines (64 loc) · 2.38 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
}
// handle commands
if(message.text === "/ping"){
await connection.sendTextMessage(contact.publicKey, "PONG! 🏓", Constants.TxtTypes.Plain);
return;
}
// handle commands
if(message.text === "/date"){
await connection.sendTextMessage(contact.publicKey, (new Date()).toISOString(), Constants.TxtTypes.Plain);
return;
}
// help menu
const response = [
"🤖 Echo Bot Help",
"/help - show help menu",
"/ping - replies with pong",
"/date - replies with current date",
].join("\n");
// fallback to send the help menu
await connection.sendTextMessage(contact.publicKey, response, 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();