mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-08-28 14:14:09 +00:00
support bot, simplex-chat-nodejs: fix bot command parsing (#6964)
ciBotCommand's regex was unanchored, so a customer message like "follow/read blog posts?" parsed as a /read command and the Grok handler silently dropped the message. Anchor the regex with ^ so a command requires `/` at the start of the (trimmed) message. In the support bot, filter customer command parsing by the registered keyword set: any unknown `/word` from a customer (e.g. /help) is now routed as plain text instead of being silently dropped by Grok. This also makes /grok when Grok is disabled behave consistently as text, removing the previous ad-hoc workaround.
This commit is contained in:
@@ -78,7 +78,7 @@ export interface BotCommand {
|
||||
export function ciBotCommand(chatItem: T.ChatItem): BotCommand | undefined {
|
||||
const msg = ciContentText(chatItem)?.trim()
|
||||
if (msg) {
|
||||
const r = msg.match(/\/([^\s]+)(.*)/)
|
||||
const r = msg.match(/^\/([^\s]+)(.*)/)
|
||||
if (r && r.length >= 3) {
|
||||
return {keyword: r[1], params: r[2].trim()}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import {T} from "@simplex-chat/types"
|
||||
import {ciBotCommand} from "../src/util"
|
||||
|
||||
function rcvText(text: string): T.ChatItem {
|
||||
return {content: {type: "rcvMsgContent", msgContent: {type: "text", text}}} as T.ChatItem
|
||||
}
|
||||
|
||||
describe("ciBotCommand", () => {
|
||||
it("parses a command at the start of the message", () => {
|
||||
expect(ciBotCommand(rcvText("/grok hello"))).toEqual({keyword: "grok", params: "hello"})
|
||||
})
|
||||
|
||||
it("returns undefined for a slash in the middle of a word", () => {
|
||||
expect(ciBotCommand(rcvText("What follow/read blog posts?"))).toBeUndefined()
|
||||
})
|
||||
|
||||
it("returns undefined for a slash after a space", () => {
|
||||
expect(ciBotCommand(rcvText("see /home for details"))).toBeUndefined()
|
||||
})
|
||||
|
||||
it("strips leading whitespace before matching", () => {
|
||||
expect(ciBotCommand(rcvText(" /grok ask this"))).toEqual({keyword: "grok", params: "ask this"})
|
||||
})
|
||||
|
||||
it("returns command with empty params when only the keyword is present", () => {
|
||||
expect(ciBotCommand(rcvText("/team"))).toEqual({keyword: "team", params: ""})
|
||||
})
|
||||
|
||||
it("returns undefined for plain text without slash", () => {
|
||||
expect(ciBotCommand(rcvText("hello there"))).toBeUndefined()
|
||||
})
|
||||
|
||||
it("returns undefined for non-text chat item content", () => {
|
||||
const ci = {content: {type: "rcvDeleted"}} as T.ChatItem
|
||||
expect(ciBotCommand(ci)).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user