> ## Documentation Index
> Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Slackbot Agent

> Build AI-powered Slack bots with PraisonAI

# Slackbot Agent

Build intelligent Slack bots that can respond to messages, handle mentions, and execute tasks.

## Quick Start

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { Agent, createSlackBot } from 'praisonai-ts';

// Create an agent
const agent = new Agent({
  name: 'SlackAssistant',
  instructions: 'You are a helpful Slack assistant.',
});

// Create Slack bot
const bot = createSlackBot({
  botToken: process.env.SLACK_BOT_TOKEN!,
  signingSecret: process.env.SLACK_SIGNING_SECRET!,
});

// Handle messages
bot.onMessage(async (message) => {
  const response = await agent.chat(message.text);
  return { text: response, threadTs: message.ts };
});

// Start the bot
bot.listen(3000);
```

## Configuration

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createSlackBot } from 'praisonai-ts';

const bot = createSlackBot({
  // Required
  botToken: process.env.SLACK_BOT_TOKEN!,
  
  // For webhook mode (recommended for production)
  signingSecret: process.env.SLACK_SIGNING_SECRET!,
  
  // For Socket Mode (development)
  appToken: process.env.SLACK_APP_TOKEN,
  socketMode: true,
});
```

## Event Handlers

### Message Handler

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
bot.onMessage(async (message) => {
  // message.channel - Channel ID
  // message.user - User ID
  // message.text - Message text
  // message.ts - Timestamp
  // message.threadTs - Thread timestamp (if in thread)
  
  const response = await agent.chat(message.text);
  
  return {
    text: response,
    threadTs: message.ts, // Reply in thread
  };
});
```

### App Mention Handler

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
bot.onAppMention(async (message) => {
  // Triggered when bot is @mentioned
  const response = await agent.chat(message.text);
  return { text: response };
});
```

### Reaction Handler

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
bot.onReactionAdded(async (event) => {
  if (event.reaction === 'eyes') {
    // Process message when 👀 reaction is added
    console.log('Processing message:', event.item.ts);
  }
});
```

## Rich Responses

### With Blocks

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
bot.onMessage(async (message) => {
  return {
    text: 'Here are the results:',
    blocks: [
      {
        type: 'section',
        text: {
          type: 'mrkdwn',
          text: '*Results*\n• Item 1\n• Item 2',
        },
      },
      {
        type: 'actions',
        elements: [
          {
            type: 'button',
            text: { type: 'plain_text', text: 'View More' },
            action_id: 'view_more',
          },
        ],
      },
    ],
  };
});
```

## Socket Mode (Development)

For local development without exposing a public URL:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const bot = createSlackBot({
  botToken: process.env.SLACK_BOT_TOKEN!,
  appToken: process.env.SLACK_APP_TOKEN!,
  socketMode: true,
});

// No need to call listen() - socket mode connects automatically
await bot.start();
```

## Webhook Mode (Production)

For production deployments:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const bot = createSlackBot({
  botToken: process.env.SLACK_BOT_TOKEN!,
  signingSecret: process.env.SLACK_SIGNING_SECRET!,
});

// Start HTTP server
bot.listen(3000);
```

## With Tools

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const agent = new Agent({
  name: 'SlackAssistant',
  instructions: 'You help with tasks in Slack.',
  tools: [
    {
      name: 'search_docs',
      description: 'Search documentation',
      execute: async ({ query }) => {
        // Search implementation
        return { results: ['doc1', 'doc2'] };
      },
    },
  ],
});

bot.onMessage(async (message) => {
  const response = await agent.chat(message.text);
  return { text: response };
});
```

## Environment Variables

| Variable               | Required | Description          |
| ---------------------- | -------- | -------------------- |
| `SLACK_BOT_TOKEN`      | Yes      | Bot token (xoxb-...) |
| `SLACK_SIGNING_SECRET` | Webhook  | Signing secret       |
| `SLACK_APP_TOKEN`      | Socket   | App token (xapp-...) |
| `OPENAI_API_KEY`       | Yes      | For the agent        |

## Slack App Setup

1. Create a Slack App at [https://api.slack.com/apps](https://api.slack.com/apps)
2. Enable **Socket Mode** or configure **Event Subscriptions**
3. Add bot scopes: `chat:write`, `app_mentions:read`, `channels:history`
4. Install to workspace
5. Copy tokens to environment variables

## Best Practices

1. **Use threads** - Reply in threads to keep channels clean
2. **Handle errors gracefully** - Return friendly error messages
3. **Rate limit** - Respect Slack's rate limits
4. **Log interactions** - Track usage for debugging

## Related

* [Agent](/docs/js/agent) - Core agent documentation
* [Tools](/docs/js/tools) - Adding tools to agents
