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 slackSetup
Get your Slack Bot Token from the Slack API dashboard.
Required scopes:
channels:readchannels:writechat:writeusers:read
Add to your configuration:
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
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 channelsall slack users- List all usersslack channel by id- Get channel detailsslack channel by name- Find channel by nameslack channel members- List channel membersslack user by id- Get user detailsslack user by email- Find user by email
Mutations
post slack message- Send a messageupdate slack message- Edit a messagedelete slack message- Delete a messagecreate slack channel- Create a new channelarchive slack channel- Archive a channelinvite users to slack channel- Add users to channelremove users from slack channel- Remove users from channel
Configuration Options
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
// }>