Corsair
Plugins

Slack

Enable natural language queries and mutations for Slack operations

Slack

The Slack plugin enables natural language queries and mutations for Slack operations.

Installation

npm install @corsair/plugin-slack
npx corsair add slack

Setup

Get your Slack Bot Token from the Slack API dashboard.

Required scopes:

  • channels:read
  • channels:write
  • chat:write
  • users:read

Add to your configuration:

corsair.config.ts
import { defineConfig } from 'corsair'
import { slack } from '@corsair/plugin-slack'

export default defineConfig({
  database: {
    type: 'postgresql',
    url: process.env.DATABASE_URL,
  },
  plugins: [
    slack({
      token: process.env.SLACK_BOT_TOKEN,
    }),
  ],
})

Environment Variables

.env
SLACK_BOT_TOKEN="xoxb-your-bot-token"

Queries

List Channels

import { useCorsairQuery } from 'corsair/client'

const { data: channels } = useCorsairQuery('all slack channels')
//    ^? Array<{ id: string, name: string, is_private: boolean }>

List Users

const { data: users } = useCorsairQuery('all slack users')
//    ^? Array<{ id: string, name: string, real_name: string, email: string }>

Get Channel Info

const { data: channel } = useCorsairQuery('slack channel by id', {
  channelId: 'C123456',
})

List Channel Members

const { data: members } = useCorsairQuery('slack channel members', {
  channelId: 'C123456',
})

Mutations

Post Message

import { useCorsairMutation } from 'corsair/client'

const { mutate: postMessage } = useCorsairMutation('post slack message')

postMessage({
  channel: '#general',
  text: 'Hello from Corsair!',
})

Post with Blocks

postMessage({
  channel: '#general',
  blocks: [
    {
      type: 'section',
      text: {
        type: 'mrkdwn',
        text: '*New user signed up!*\nEmail: user@example.com',
      },
    },
    {
      type: 'actions',
      elements: [
        {
          type: 'button',
          text: { type: 'plain_text', text: 'View Profile' },
          url: 'https://app.example.com/users/123',
        },
      ],
    },
  ],
})

Create Channel

const { mutate: createChannel } = useCorsairMutation('create slack channel')

createChannel({
  name: 'new-project',
  is_private: false,
})

Invite Users to Channel

const { mutate: inviteUsers } = useCorsairMutation(
  'invite users to slack channel'
)

inviteUsers({
  channel: 'C123456',
  users: ['U123456', 'U789012'],
})

Update Message

const { mutate: updateMessage } = useCorsairMutation('update slack message')

updateMessage({
  channel: 'C123456',
  ts: '1234567890.123456',
  text: 'Updated message text',
})

Delete Message

const { mutate: deleteMessage } = useCorsairMutation('delete slack message')

deleteMessage({
  channel: 'C123456',
  ts: '1234567890.123456',
})

Common Patterns

Notification System

import { useCorsairMutation } from 'corsair/client'

export function useSlackNotification() {
  const { mutate: notify } = useCorsairMutation('post slack message')

  return {
    notifyNewUser: (user: User) => {
      notify({
        channel: '#signups',
        text: `New user: ${user.email}`,
      })
    },
    notifyError: (error: string) => {
      notify({
        channel: '#alerts',
        text: `🚨 Error: ${error}`,
      })
    },
  }
}

Post-Creation Notification

const { mutate: createPost } = useCorsairMutation('create post', {
  onSuccess: post => {
    // Automatically notify Slack
    notifySlack({
      channel: '#content',
      text: `New post published: ${post.title}`,
    })
  },
})

Available Endpoints

Queries

  • all slack channels - List all channels
  • all slack users - List all users
  • slack channel by id - Get channel details
  • slack channel by name - Find channel by name
  • slack channel members - List channel members
  • slack user by id - Get user details
  • slack user by email - Find user by email

Mutations

  • post slack message - Send a message
  • update slack message - Edit a message
  • delete slack message - Delete a message
  • create slack channel - Create a new channel
  • archive slack channel - Archive a channel
  • invite users to slack channel - Add users to channel
  • remove users from slack channel - Remove users from channel

Configuration Options

corsair.config.ts
slack({
  token: process.env.SLACK_BOT_TOKEN,

  // Optional: Default channel for notifications
  defaultChannel: '#general',

  // Optional: Rate limiting
  rateLimit: {
    maxRequests: 50,
    perSeconds: 60,
  },
})

Error Handling

const { mutate: postMessage, error } = useCorsairMutation('post slack message')

if (error) {
  if (error.code === 'SLACK_CHANNEL_NOT_FOUND') {
    console.error("Channel doesn't exist")
  }
  if (error.code === 'SLACK_RATE_LIMIT') {
    console.error('Rate limit exceeded')
  }
}

TypeScript Support

The plugin provides full type safety:

const { data } = useCorsairQuery('all slack channels')
//    ^? Array<{
//         id: string
//         name: string
//         is_private: boolean
//         is_archived: boolean
//         num_members: number
//       }>