mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-07-26 14:58:12 +00:00
Extract bot logic from MyMesh into MyMeshBot.h
Move tryBotReplyDM() and tryBotReplyChannel() implementations to MyMeshBot.h (included at end of MyMesh.cpp). onMessageRecv and onChannelMessageRecv now contain only a single-line call each, minimising the diff against upstream in those methods. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -515,37 +515,7 @@ void MyMesh::onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t
|
||||
markConnectionActive(from); // in case this is from a server, and we have a connection
|
||||
queueMessage(from, TXT_TYPE_PLAIN, pkt, sender_timestamp, NULL, 0, text);
|
||||
|
||||
// DM auto-reply bot
|
||||
if (_prefs.bot_enabled && _prefs.bot_target_type == 1 &&
|
||||
_prefs.bot_trigger[0] && _prefs.bot_reply[0] &&
|
||||
millis() - _bot_last_reply_ms > 10000UL) {
|
||||
// all-zero pubkey = any sender; otherwise match first 4 bytes
|
||||
bool key_ok = (_prefs.bot_dm_pubkey[0] == 0 && _prefs.bot_dm_pubkey[1] == 0 &&
|
||||
_prefs.bot_dm_pubkey[2] == 0 && _prefs.bot_dm_pubkey[3] == 0) ||
|
||||
memcmp(from.id.pub_key, _prefs.bot_dm_pubkey, 4) == 0;
|
||||
if (key_ok) {
|
||||
const char* t = text;
|
||||
const char* tr = _prefs.bot_trigger;
|
||||
int tlen = strlen(tr);
|
||||
bool matched = false;
|
||||
for (; *t && !matched; t++) {
|
||||
int i = 0;
|
||||
while (i < tlen && tolower((uint8_t)t[i]) == tolower((uint8_t)tr[i])) i++;
|
||||
if (i == tlen) matched = true;
|
||||
}
|
||||
if (matched) {
|
||||
uint32_t ts = getRTCClock()->getCurrentTime();
|
||||
char expanded[200];
|
||||
expandMsg(_prefs.bot_reply, expanded, sizeof(expanded),
|
||||
sensors.node_lat, sensors.node_lon,
|
||||
sensors.node_lat != 0.0 || sensors.node_lon != 0.0,
|
||||
ts, _prefs.tz_offset_hours);
|
||||
uint32_t expected_ack, est_timeout;
|
||||
if (sendMessage(from, ts, 0, expanded, expected_ack, est_timeout) != MSG_SEND_FAILED)
|
||||
_bot_last_reply_ms = millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
tryBotReplyDM(from, text);
|
||||
}
|
||||
|
||||
void MyMesh::onCommandDataRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
|
||||
@@ -605,35 +575,7 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe
|
||||
if (_ui) _ui->newMsg(path_len, channel_name, text, offline_queue_len, 0);
|
||||
#endif
|
||||
|
||||
// auto-reply bot
|
||||
if (_prefs.bot_enabled && _prefs.bot_trigger[0] && _prefs.bot_reply[0] &&
|
||||
channel_idx == _prefs.bot_channel_idx &&
|
||||
millis() - _bot_last_reply_ms > 10000UL) {
|
||||
// case-insensitive substring match
|
||||
const char* t = text;
|
||||
const char* tr = _prefs.bot_trigger;
|
||||
int tlen = strlen(tr);
|
||||
bool matched = false;
|
||||
for (; *t && !matched; t++) {
|
||||
int i = 0;
|
||||
while (i < tlen && tolower((uint8_t)t[i]) == tolower((uint8_t)tr[i])) i++;
|
||||
if (i == tlen) matched = true;
|
||||
}
|
||||
if (matched) {
|
||||
ChannelDetails ch;
|
||||
if (getChannel(channel_idx, ch)) {
|
||||
uint32_t ts = getRTCClock()->getCurrentTime();
|
||||
char expanded[200];
|
||||
expandMsg(_prefs.bot_reply, expanded, sizeof(expanded),
|
||||
sensors.node_lat, sensors.node_lon,
|
||||
sensors.node_lat != 0.0 || sensors.node_lon != 0.0,
|
||||
ts, _prefs.tz_offset_hours);
|
||||
int rlen = strlen(expanded);
|
||||
if (sendGroupMessage(ts, ch.channel, _prefs.node_name, expanded, rlen))
|
||||
_bot_last_reply_ms = millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
tryBotReplyChannel(channel_idx, text);
|
||||
}
|
||||
|
||||
void MyMesh::onChannelDataRecv(const mesh::GroupChannel &channel, mesh::Packet *pkt, uint16_t data_type,
|
||||
@@ -2266,3 +2208,5 @@ bool MyMesh::advert() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#include "MyMeshBot.h"
|
||||
|
||||
Reference in New Issue
Block a user