Skip to content

feat(keywords): add manychat-style match conditions for contact keywords - #1617

Merged
sung17 merged 1 commit into
mainfrom
feat/keyword-match-conditions
Nov 2, 2026
Merged

sung17 merged 1 commit into
mainfrom
feat/keyword-match-conditions

Conversation

@sung17

@sung17 sung17 commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Contact (inbound) keywords can combine message conditions like ManyChat: conditions are ANDed, the values inside one condition are ORed. 14 match types: is, similar to, contains, contains whole word, begins with, ends with, doesn't contain, matching regex, contains phone/email/link/image/video, is thumbs up. Text matching ignores case and Vietnamese accents ("tu van" = "tư vấn").
  • Page (outbound) keywords are unchanged (exact match).
  • Every operation is available to agents through the public API (conditions on keywords.create/keywords.update), per AGENTS.md invariant 23.

Changes

  • Database: AutomatedResponse.conditions jsonb (nullable, no default). Migration 20261102052103_keyword_conditions adds the column (IF NOT EXISTS) and turns every existing inbound rule into one contains condition. The flat keywords column keeps the trigger words for search, findByInboundKeyword and older clients. Excluded words and regex patterns are not copied into it.
  • Matcher (packages/automated-response/src/keyword-match.ts): one function per match type. Regex uses re2js, which runs in linear time, so a pattern like (a+)+$ cannot stall the worker. "Similar to" allows one edit per five characters. Phone/email reuse extractContactInfo; link detection and Levenshtein moved to packages/utils and are now shared with comment hiding and flow-config.
  • Schema split: re2js (~260 KB) lives in @chatbotx.io/database/partials/keyword-condition-schema, so client components importing the partials barrel do not ship it.
  • Image/video-only messages: they now reach Keywords (channels and webchat) through resolveIncomingTextRouting + enqueueMessage. They are queued only when an inbound rule matches, so the AI agent / Global Router never answer a bare photo. findManyByIds now returns each message's attachment file types.
  • Service (automatedResponseService): resolves keywords/conditions in one place for the UI, public API and template install. It rejects both at once, rejects conditions on outbound rules, and applies the same limits to plain keywords. Unchanged saves write no audit entry. Templates export/install conditions.
  • UI: ManyChat-style condition editor (match type select, keyword pills, "and" separators, "+ Message condition") for the Contact tab; the list shows "Contains (a)(b) and Begins with (c)". Translations in all 21 locales.
  • Docs: CLI README keywords section (also fixes the wrong inbound|comment type list); new MCP eval case.
  • Fix (pre-existing on main): creating a text-reply keyword sent flowId: "", which failed as an invalid bigint.

Notes for reviewers

  • After the migration, existing inbound rules match ignoring accents, which is broader than before. This was a deliberate product decision.
  • Keyword rules are still evaluated oldest-first, first match wins. A loose old rule (e.g. "contains w") can catch messages before a newer rule ("contains link"). Manual ordering is out of scope.
  • "Is thumbs up" only matches a 👍 emoji message. The Messenger Like button arrives as an image, so it matches "contains image" instead.
  • Before deploy, check select max(cardinality(keywords)) from "AutomatedResponse" where type='inbound' on production. A rule with more than 50 keywords cannot be re-saved from the form until the limit is raised.

Test plan

  • pnpm lint (includes i18n check and schema drift check)
  • check-types: database, utils, flow-config, automated-response, business, worker, builder
  • Tests: automated-response, database, business, utils, flow-config, worker, builder, chatbotx-mcp
  • Apply migration on staging and confirm existing inbound keywords still trigger
  • Manual: create a rule with [contains "muốn", contains whole word "ok", doesn't contain "test"] and check replies via webchat
  • Manual: rule "contains image" replies to a photo-only message; with no matching rule the photo gets no AI reply
  • Manual: API POST /v1/keywords with conditions, then GET returns them

🤖 Generated with Claude Code

Contact (inbound) keywords can now combine message conditions (AND) with
several values each (OR): is, similar to, contains, contains whole word,
begins/ends with, doesn't contain, regex (RE2), phone, email, link, image,
video and thumbs up. Matching ignores case and Vietnamese accents.

- AutomatedResponse.conditions jsonb; migration turns existing inbound rules
  into one "contains" condition. Page (outbound) rules keep exact keywords.
- Image/video-only messages reach Keywords and are only queued when a rule
  matches, so the AI never answers a bare photo.
- Public API create/update accept `conditions`; CLI/MCP docs updated.
- Fix: creating a text-reply keyword sent flowId "" and failed as bigint.
@github-actions github-actions Bot added the feature New feature or request label Nov 2, 2026
@sung17
sung17 merged commit cc29903 into main Nov 2, 2026
15 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
…rds (#1617)

Contact (inbound) keywords can now combine message conditions (AND) with
several values each (OR): is, similar to, contains, contains whole word,
begins/ends with, doesn't contain, regex (RE2), phone, email, link, image,
video and thumbs up. Matching ignores case and Vietnamese accents.

- AutomatedResponse.conditions jsonb; migration turns existing inbound rules
  into one "contains" condition. Page (outbound) rules keep exact keywords.
- Image/video-only messages reach Keywords and are only queued when a rule
  matches, so the AI never answers a bare photo.
- Public API create/update accept `conditions`; CLI/MCP docs updated.
- Fix: creating a text-reply keyword sent flowId "" and failed as bigint.

Co-authored-by: Deathgiver <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant