Skip to content
← Public packages

@kody/slack

Read Slack conversations and safely send messages as the authorizing user through the saved slack OAuth integration.

src/get-history.ts

33 lines · 1.0 KB · TypeScript
import { nextCursor, resolveIntegrationName, 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'),
		},
		resolveIntegrationName(input.integration),
	)

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