Skip to content

fix(line): map LINE mentions to At elements so the at-bot rule works - #2478

Merged
RockChinQ merged 1 commit into
langbot-app:masterfrom
leonoxo:fix/line-mention-at-elements
Aug 27, 2026
Merged

fix(line): map LINE mentions to At elements so the at-bot rule works#2478
RockChinQ merged 1 commit into
langbot-app:masterfrom
leonoxo:fix/line-mention-at-elements

Conversation

@leonoxo

@leonoxo leonoxo commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

The LINE adapter never parsed message.mentions: the full text (including
the @BotName chip text) went into a single Plain component. As a result,
the at: true group respond rule could never match on LINE — atbot.py
looks for an At element whose target equals bot_account_id, but the
adapter produced no At elements at all.

Users whose bot name is not a configured prefix therefore had to rely on
prefix/regexp rules to get the bot to respond to a LINE group @-mention, and
there was no log of the drop (the message is silently interrupted).

This PR maps LINE mentions to At elements:

  • a mention of the bot itself (mentionee.is_self) → At(target=bot_account_id)
    — matches the existing at-bot rule directly;
  • a mention of anyone else → At(target=user_id, display=name) — the
    @name text is preserved for prefix/regexp rules (At.__str__() renders
    @{display}, and display is already @-stripped, so no double @).

The text around mentions is split by mentionee.index/length (LINE
guarantees text[index:index+length] == '@name'); malformed or out-of-range
mention positions are skipped and the remainder falls back to plain text.
No-mention messages, images, and other message types are unchanged.

Related

Same area as #2398 (LINE group session identity); complements it — that one
fixed which session a group message goes to, this one fixes whether the
group message triggers a response at all
.

Test Plan

  • New unit tests in tests/unit_tests/platform/test_line_session_identity.py:
    • bot mention → At(target=bot_account_id) (the at-bot rule case)
    • mention of another user → At(target=user_id, display=name)
    • @ALL → plain text (unchanged behaviour)
    • multiple mentions → correct split with surrounding text
    • out-of-range mention indices → graceful fallback to plain text
    • no mention → single plain component (regression guard)
  • Existing session-identity tests updated to the instance-method converter.
  • uv run pytest tests/unit_tests/ → 2803 passed
  • uv run ruff check / ruff format --check clean

The LINE adapter passed text through as a single Plain component,
ignoring the mention payload (mentions[].index/length/isSelf) that the
Line Messaging API includes in the webhook. As a result:

- At(target=bot_account_id) never appeared in the message chain, so the
  'at-bot' group respond rule silently dropped every @bot mention.
- The bot only replied when the message happened to match the prefix
  rule (e.g. starting with 'ai').

Now LINEMessageConverter reads message.message.mention and builds the
chain per mention position:

- Bot mention (isSelf) -> At(target=bot_account_id) so AtBotRule matches
  the same way as other adapters (dingtalk/lark etc.).
- Other mentions -> At(target=<line user id>, display=<mention text>).
  At.__str__ already prepends '@', so the display text carries no
  double '@' and the rendered text (prefix/regexp rules, quotes,
  session context) is byte-identical to before.
- Missing/out-of-bounds mentions are skipped defensively.

target2yiri becomes an instance method (like wechatpad/aiocqhttp) so
the converters can hold bot_account_id; LINEAdapter passes it in from
its own config.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. m: Platform 机器人管理相关 / Bots management labels Aug 27, 2026
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.42857% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/langbot/pkg/platform/sources/line.py 91.42% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@RockChinQ
RockChinQ merged commit 855ae2b into langbot-app:master Aug 27, 2026
10 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 27, 2026
@leonoxo
leonoxo deleted the fix/line-mention-at-elements branch August 27, 2026 16:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

m: Platform 机器人管理相关 / Bots management size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants