Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.5 KB
Raw Blame
import { HogFunctionTemplate } from '~/cdp/types'

import { hogApiErrorMessageFn } from '../../hog-helpers'

export const template: HogFunctionTemplate = {
    free: true,
    status: 'hidden',
    type: 'destination',
    id: 'template-posthog-update-ticket',
    name: 'Update conversation ticket',
    description:
        'Update the status, priority, SLA, assignee, or tags of a conversation ticket. Tags are additive by default.',
    icon_url: '/static/posthog-icon.svg',
    category: ['Custom'],
    code_language: 'hog',
    code: `
${hogApiErrorMessageFn}

if (empty(inputs.ticket_id)) {
  throw Error('Ticket ID is required')
}

let updates := {}

if (not empty(inputs.status) and inputs.status != '') {
  updates.status := inputs.status
}

if (not empty(inputs.priority) and inputs.priority != '') {
  updates.priority := inputs.priority
}

if (inputs.sla_amount == 'clear') {
  updates.sla_due_at := null
} else if (not empty(inputs.sla_amount)) {
  let amount := toFloat(inputs.sla_amount)

  if (amount == null or amount <= 0) {
    throw Error(f'Invalid SLA amount: {inputs.sla_amount}. Must be a positive number or "clear".')
  }
  let unit := inputs.sla_unit ?? 'hour'

  if (empty(inputs.sla_business_hours)) {
    let deadline := dateAdd(unit, amount, now())
    updates.sla_due_at := formatDateTime(deadline, '%Y-%m-%dT%H:%i:%SZ')
  } else {
    updates.sla_amount := amount
    updates.sla_unit := unit
    updates.sla_business_hours := inputs.sla_business_hours
  }
}

if (not empty(inputs.assignee)) {
  updates.assignee := inputs.assignee
}

if (not empty(inputs.tags)) {
  updates.tags := inputs.tags
  updates.tags_mode := (not empty(inputs.tags_mode)) ? inputs.tags_mode : 'add'
}

let response := postHogUpdateTicket({
  'ticket_id': inputs.ticket_id,
  'updates': updates
})

if (response.status >= 400) {
  throw Error(f'Failed to update ticket ({response.status}): {apiErrorMessage(response)}')
}

return response.body
`,
    inputs_schema: [
        {
            key: 'ticket_id',
            type: 'string',
            label: 'Ticket ID',
            secret: false,
            required: true,
            default: '{event.properties.ticket_id}',
            description: 'The UUID of the ticket to update. Available from trigger event properties.',
        },
        {
            key: 'status',
            type: 'choice',
            label: 'Status',
            secret: false,
            required: false,
            choices: [
                { label: 'New', value: 'new' },
                { label: 'Open', value: 'open' },
                { label: 'Pending', value: 'pending' },
                { label: 'On hold', value: 'on_hold' },
                { label: 'Resolved', value: 'resolved' },
            ],
            description: 'New status for the ticket. Leave empty to keep current.',
        },
        {
            key: 'priority',
            type: 'choice',
            label: 'Priority',
            secret: false,
            required: false,
            choices: [
                { label: 'Low', value: 'low' },
                { label: 'Medium', value: 'medium' },
                { label: 'High', value: 'high' },
                { label: 'Critical', value: 'critical' },
            ],
            description: 'New priority for the ticket. Leave empty to keep current.',
        },
        {
            key: 'sla_amount',
            type: 'string',
            label: 'SLA deadline',
            secret: false,
            required: false,
            description: 'Duration from now until SLA expires. Set to "clear" to remove the SLA.',
        },
        {
            key: 'sla_unit',
            type: 'choice',
            label: 'SLA unit',
            secret: false,
            required: false,
            default: 'hour',
            choices: [
                { label: 'Minute(s)', value: 'minute' },
                { label: 'Hour(s)', value: 'hour' },
                { label: 'Day(s)', value: 'day' },
            ],
            description: 'Time unit for the SLA deadline.',
        },
        {
            key: 'sla_business_hours',
            type: 'posthog_business_hours',
            label: 'Working hours',
            secret: false,
            required: false,
            description:
                'Days, time of day, and timezone the SLA clock runs during. Defaults to all days / any time (calendar hours).',
        },
        {
            key: 'assignee',
            type: 'posthog_assignee',
            label: 'Assignee',
            secret: false,
            required: false,
            description: 'Assign ticket to a user or role. Leave empty to keep current assignment.',
        },
        {
            key: 'tags',
            type: 'posthog_ticket_tags',
            label: 'Tags',
            secret: false,
            required: false,
            description:
                'Tags to apply to the ticket. Each tag supports variable templating, e.g. {event.properties.region}. Leave empty to keep current tags.',
        },
        {
            key: 'tags_mode',
            type: 'choice',
            label: 'Tag mode',
            secret: false,
            required: false,
            default: 'add',
            choices: [
                { label: 'Add to existing tags', value: 'add' },
                { label: 'Replace all tags', value: 'set' },
                { label: 'Remove these tags', value: 'remove' },
            ],
            description:
                'How the tags above are applied. Add (default) is safe when multiple workflows tag the same ticket.',
        },
    ],
}