Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/slack

src/get-history.ts

29 lines · 1000 B · TypeScript
import { nextCursor, slackRequest } from './request.ts'
import { boundedLimit, inputRecord, optionalBoolean, optionalString, requiredString } from './validation.ts'

type HistoryResponse = {
  ok: boolean
  messages?: unknown[]
  has_more?: boolean
  response_metadata?: { next_cursor?: string }
  [key: string]: unknown
}

/** Read message history from a Slack conversation available to the user. */
export default async function getHistory(params: Record<string, unknown>) {
  const input = inputRecord(params)
  const result = await slackRequest<HistoryResponse>('conversations.history', {
    channel: requiredString(input, 'channel'),
    limit: boundedLimit(input.limit),
    cursor: optionalString(input, 'cursor'),
    oldest: optionalString(input, 'oldest'),
    latest: optionalString(input, 'latest'),
    inclusive: optionalBoolean(input, 'inclusive'),
  })

  return {
    messages: result.messages ?? [],
    hasMore: result.has_more === true,
    nextCursor: nextCursor(result),
  }
}