Skip to content

feat: Add a ticket API for the Assistant - #296

Draft
funana777 wants to merge 6 commits into
feat/zammad-toolingfrom
feat/zammad-assistant-read
Draft

funana777 wants to merge 6 commits into
feat/zammad-toolingfrom
feat/zammad-assistant-read

Conversation

@funana777

@funana777 funana777 commented Nov 18, 2026 •

Copy link
Copy Markdown

Adds OCS routes that let the Nextcloud Assistant, and any other client, list Zammad tickets and read a ticket with its conversation.

Base: #295 (feat/zammad-tooling). Merge #295 first. #297 (notes) builds on this PR. The tools that use these routes are in nextcloud/context_agent#335.

Why

Support teams work in Zammad and want the Assistant's help on tickets. Today the Assistant cannot read a ticket: asked about one, it calls Zammad without a token and then asks the user to paste one into the chat. The user's token already sits in this app, so the routes go through it. Every call uses the user's own token, and Zammad decides what the user may see, internal notes included.

What

  • GET /api/v1/tickets: passes the query to Zammad's ticket search unchanged (the same syntax as Zammad's search bar), with mine, limit (at most 20), sort_by and order_by. Each ticket has its state, priority, owner and customer as names, and Zammad's own contact and escalation times. mine is sent as a search condition on the ticket owner, which Zammad applies with and without Elasticsearch. Field syntax in the query needs Elasticsearch on the Zammad server; without it, Zammad matches the query as plain text. Errors: forbidden (403) when Zammad refuses access, search_failed (400) only when Zammad refuses the search itself (400 or 422, e.g. an unknown sort_by), otherwise zammad_error (502) with Zammad's status code in the message.
  • GET /api/v1/ticket?ticket=<ID or link>: one ticket with its articles as plain text, newest last. Email articles are cut at Zammad's signature marker and lose their blockquotes; notes, web form, phone and chat articles are kept whole. If they do not fit into max_chars (which the Context Agent sets to the room the model has), the oldest are left out and truncated is true. Attachments are listed, not downloaded. Links to another Zammad server are refused.
  • Lists and tickets name people the same way, from the assets Zammad returns with full=true.
  • A single ticket also has Zammad's contact and escalation times, like the list. The Assistant's note tool needs last_contact_customer_at to tell whether the customer wrote after its last note.
  • A capability (ticket_api: 1), the OpenAPI spec with its CI check, and a short README section.

Notes for review:

  • Two commits: the shared text helper with the quote cleanup, then the routes.
  • The controller calls ZammadAPIService directly. No new service, no settings.
  • The Context Chat import produces the same text as before. Using the cleanup there is a behaviour change, in feat(context_chat): Index mails without signature and quoted thread #298.
  • The cleanup is based on a check against support.nextcloud.com: Zammad stores <span class="js-signatureMarker"></span> in the HTML body of incoming mails, before the signature and the quoted thread, for blockquote and Outlook-style replies alike; preferences.signature_detection was never set. The body is cut at the marker, then blockquotes are removed with DOMDocument, as a quote can start before the marker. No language patterns: the quote header line ("On ... wrote:") before the marker stays in the text. Known limit: in some Outlook-style replies Zammad puts the marker well after Outlook's quote header, so part of the quoted mail stays. The size limit still applies, and the similar-ticket search only uses the first customer message.
  • InstanceIdTest pins the Context Chat instance ID with the same test vector as the Context Agent's tools, which compute it too.

1,889 lines added, 44 removed; without openapi.json 1,059 added, of which 93 are the OpenAPI CI workflow and 285 tests.

How to test

  1. composer run test:unit
  2. Connect a user to a Zammad server in Connected accounts.
  3. As that user, list tickets: curl -u user:pass -H 'OCS-APIRequest: true' 'https://<nextcloud>/ocs/v2.php/apps/integration_zammad/api/v1/tickets?query=state.name:new&format=json'.
  4. Read a ticket by link: .../api/v1/ticket?ticket=https://<zammad>/%23ticket/zoom/<id>&format=json. Quoted mail history is gone, internal notes are marked internal: true. With &max_chars=4000 on a long ticket, older articles are left out and truncated is true.
  5. Try a ticket you cannot see in Zammad (404) and a link to another server (400).

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Code, tests and this description were drafted with Claude Code (claude-opus-5-5) under my direction.

How this was tested before opening the PR

On a sandbox Nextcloud 35.0.1 against a fake Zammad API on zammad.example.com with realistic JSON: an HTML mail with quoted history, internal notes, a 40-article thread. Covered: field queries, mine, quote stripping, internal notes in the result, truncation with max_chars, the error responses and the capability. List and single ticket give the same owner and customer names. The routes were also used by the Context Agent in the end-to-end runs described in nextcloud/context_agent#335. 20 unit tests; psalm (stable32) shows no new errors. After review: the error mapping and the mine condition are covered by unit tests on Nextcloud 35, and the condition parameter was checked in Zammad's source (6.0.0, 7.2.3 and develop), not yet against a real Zammad.

End-to-end run on 2026-11-24 with the whole stack (#297, #298, nextcloud/context_agent#335) on a sandbox Nextcloud 35.0.1 against a fake Zammad, first with scripted tool calls, then with Claude Haiku as the model. It found the missing contact times on a single ticket, fixed in the last commit (one more unit test assertion, red without the fix).


Generated by Claude Code

Ticket text goes to language models, which need what an article says
itself, not the whole quoted mail thread. TicketText holds the text
handling:

- htmlToText and the ticket link parsing, moved unchanged from the
  Context Chat import and the link previews, where each was private.
  The import produces the same text as before.
- articleText cuts an email's HTML body at Zammad's signature marker,
  <span class="js-signatureMarker"></span>, and removes blockquote
  elements from what is left with DOMDocument. A check against
  support.nextcloud.com showed the marker stored in the body of every
  quoted customer mail, before the signature and the quoted thread,
  for blockquote and Outlook-style replies alike, and that a quote can
  start before it. Plain-text mails are kept as they are, a message
  that would come out empty keeps all its text, and bodies over 1 MB
  are only converted.

No language patterns: the quote header line before the marker stays,
and some Outlook-style replies keep part of their quote.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
Lets the Assistant, and any other client, read Zammad tickets with the
user's own token, so Zammad decides what the user may see, internal
notes included.

- GET /api/v1/tickets passes the query to Zammad's ticket search
  unchanged, with mine, limit (at most 20), sort_by and order_by. Each
  ticket has its state, priority, owner and customer as names and
  Zammad's own contact and escalation times. A refused query is
  reported as search_failed; Zammad's server errors as zammad_error.
- GET /api/v1/ticket?ticket=<ID or link> returns one ticket with its
  articles as plain text, newest last. Email articles go through the
  quote cleanup; notes, web form, phone and chat articles are kept
  whole. If the articles do not fit into max_chars, the oldest are
  left out and truncated is true. Attachments are listed, not
  downloaded. Links to another Zammad server are refused.
- Lists and tickets name people the same way, from the assets Zammad
  returns with full=true. Those assets carry no groups for an agent
  token, so tickets have no group field; group.name: in a query works.
- A capability, so clients can see that the API exists, the OpenAPI
  spec with its CI check, and a README section. InstanceIdTest pins
  the Context Chat instance ID that the Context Agent's tools compute
  too.

The controller calls ZammadAPIService directly: no settings, no
service layer, no computed ticket flags.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
@funana777
funana777 force-pushed the feat/zammad-assistant-read branch from bcd138a to 80f4fbf Compare November 19, 2026 18:58
Comment thread lib/Controller/TicketApiController.php Outdated
if (!is_int($code) || $code >= 500) {
return self::error(Http::STATUS_BAD_GATEWAY, 'zammad_error', $this->l10n->t('Zammad answered with an error.'));
}
return self::error(Http::STATUS_BAD_REQUEST, 'search_failed', $this->l10n->t('Zammad could not run this search. Check the query syntax.'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass through errors more fine granularly. e.g. 403 is now reported as "Check query syntax"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I'll separate errors for 401/403 (no access), 404, 429 (rate limit), and only real query errors as search_failed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, now that it's pushed in dcdd633: 401 returns bad_credentials (412), 403 returns forbidden (403), and only 400/422 return search_failed. 404, 429 and anything else return zammad_error (502) with Zammad's status in the message, so a rate limit shows up but has no code of its own.

Comment thread lib/Controller/TicketApiController.php Outdated
return self::error(Http::STATUS_PRECONDITION_FAILED, 'not_connected',
$this->l10n->t('Your Zammad account is not known yet. Please disconnect and connect it again in the Connected accounts settings.'));
}
$query = $query === '' ? 'owner_id:' . $zammadUserId : '(' . $query . ') AND owner_id:' . $zammadUserId;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works if ElasticSearch is installed with Zammad. Not sure how we can know that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"mine" is now sent as a search condition, which Zammad applies on both backends. The docs now say that field syntax in query needs Elasticsearch.

Comment thread lib/Controller/TicketApiController.php Resolved
Co-authored-by: Marcel Klehr <mklehr@gmx.net>
Signed-off-by: Christian Fu Mueller <fu@funana.de>
Every refused search was reported as a query syntax error, also when
Zammad answered 403 because the account may not search tickets, and
server errors and network errors read the same.

Now a search answers 403 forbidden when Zammad refuses access, 400
search_failed only when Zammad refuses the search itself (400 or 422,
e.g. an unknown sort_by), and 502 zammad_error with Zammad's status code
in the message, or that Zammad could not be reached.

The error handling of a ticket request moves into zammadError() and
invalidTicket(), which the notes route builds on.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
The mine filter added owner_id:<id> to the search query. Only
Elasticsearch understands that syntax. Without it, Zammad matches the
query as plain text against title, number and articles, so the filter
found no tickets at all, and no error said why.

The filter is now sent as a search condition on ticket.owner_id, which
Zammad applies on both search backends (Zammad 6.0 and later).

The docs of the query parameter now say that field syntax needs
Elasticsearch, and that a query Zammad cannot parse finds no tickets
instead of giving an error.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
@funana777
funana777 force-pushed the feat/zammad-assistant-read branch from d343aa8 to 1b33d1d Compare November 24, 2026 13:29
GET /api/v1/ticket left out last_contact_customer_at, last_contact_agent_at
and escalation_at, which the list route already returns. The Assistant's
note tool compares its last note with last_contact_customer_at, so without
it every note after the first one on a ticket was refused as already_noted,
even after the customer had written again. Found in a sandbox run of the
stack against a fake Zammad.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
@funana777
funana777 force-pushed the feat/zammad-assistant-read branch from 4e499cd to 7959525 Compare November 24, 2026 16:31

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants