Skip to content

Tickets Service

Purpose

The Tickets Service streamlines customer support by integrating with Plentymarkets' Ticket System. It automates the retrieval of customer inquiries and leverages OpenAI to generate suggested responses, reducing the manual workload for support agents.

High-Level Architecture

The service runs as a Docker container (service-tickets) exposing an API on port 8007.

  • Integrations:
  • Plentymarkets API: Source of customer tickets and messages.
  • OpenAI API: Generates draft responses based on ticket context.
  • Data Flow:
  • Sync: Cron job fetches new/updated tickets from Plenty.
  • Process: AI analyzes the message content.
  • Draft: Suggested reply is stored in the database for agent review.

Database Schema

Ticket (tickets)

Stores the local copy of Plentymarkets tickets.

  • Key Fields: plenty_ticket_id, title, status (TODO, IN_PROGRESS, etc.), customer_name.
  • Sync: Updated periodically to reflect the state in Plentymarkets.

AIResponseDB (ai_responses)

Stores generated draft responses.

  • Key Fields: ticket_id, generated_text, model_used (e.g., gpt-4o-mini).
  • Usage: Displayed to the support agent in the frontend as a "Quick Reply" option.

OpenAICredentialDB (openai_credentials)

Stores API keys for OpenAI.

  • Key Fields: encrypted_token, model.
  • Security: Tokens are encrypted at rest.

Application Structure

  • app.py: Entry point.
  • routers/:
  • tickets_router.py: Manages ticket CRUD and sync jobs.
  • openai_router.py: Handles interaction with the OpenAI API.
  • plenty_router.py: Proxy for direct Plentymarkets calls.

Key Workflows

1. Ticket Synchronization

A background Cron Job (tickets_refresh_cron) keeps the local database in sync.

graph TD Cron[Cron Job] -->|Trigger| Mgr[Plenty Manager] Mgr -->|Fetch Tickets| API[Plentymarkets API] API -->|Return JSON| Mgr Mgr -->|Upsert| DB[(Postgres)]

2. AI Response Generation

When a ticket is opened or a new message arrives, the system can trigger an AI analysis.

  1. Context Building: The service compiles the ticket history and customer details.
  2. Prompting: Sends a structured prompt to OpenAI (e.g., "You are a helpful support agent...").
  3. Storage: The completion is saved to ai_responses.
  4. Review: The agent sees the draft, edits it if necessary, and sends it via the Plentymarkets API.

Technical Details

OpenAI Integration

The service uses the openai Python client.

  • Models: Configurable via the database (default: gpt-4o-mini).
  • Context Window: Logic exists to truncate very long ticket histories to fit within the model's token limit.

Plentymarkets Rate Limits

Similar to other services, the Plenty_Manager respects API limits to prevent service interruptions during bulk syncs.