add helper to fetch all waiting messages, and add command bot example

This commit is contained in:
liamcottle 2025-03-04 17:36:22 +13:00
parent 166762dc31
commit a03023b771
3 changed files with 102 additions and 30 deletions

View file

@ -0,0 +1,77 @@
import TCPConnection from "../src/connection/tcp_connection.js";
import Constants from "../src/constants.js";
// create tcp connection
const connection = new TCPConnection("10.1.0.226", 5000);
// wait until connected
connection.on("connected", async () => {
// we are now connected
console.log(`Connected to: [${connection.host}:${connection.port}]`);
// 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();

View file

@ -1,5 +1,4 @@
import TCPConnection from "../src/connection/tcp_connection.js";
import BufferUtils from "../src/buffer_utils.js";
import Constants from "../src/constants.js";
// create tcp connection
@ -18,51 +17,26 @@ connection.on("connected", async () => {
// listen for new messages
connection.on(Constants.PushCodes.MsgWaiting, async () => {
await syncMessages();
});
async function syncMessages() {
try {
while(true){
// sync messages until no more returned
const message = await connection.syncNextMessage();
if(!message){
break;
}
// handle received message
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 findContactByPubKeyPrefix(pubKeyPrefix) {
// get contacts
const contacts = await connection.getContacts();
// find first contact matching pub key prefix
return contacts.find((contact) => {
const contactPubKeyPrefix = contact.publicKey.subarray(0, pubKeyPrefix.length);
return BufferUtils.areBuffersEqual(pubKeyPrefix, contactPubKeyPrefix);
});
}
});
async function onContactMessageReceived(message) {
console.log("Received contact message", message);
// find first contact matching pub key prefix
const contact = await findContactByPubKeyPrefix(message.pubKeyPrefix);
const contact = await connection.findContactByPublicKeyPrefix(message.pubKeyPrefix);
if(!contact){
console.log("Did not find contact for received message");
return;

View file

@ -780,6 +780,27 @@ class Connection extends EventEmitter {
});
}
async getWaitingMessages() {
const waitingMessages = [];
while(true){
// get next message, otherwise stop if nothing is returned
const message = await this.syncNextMessage();
if(!message){
break;
}
// add to waiting messages list
waitingMessages.push(message);
}
return waitingMessages;
}
getDeviceTime() {
return new Promise(async (resolve, reject) => {
try {