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

# How to Host Slack Bots

> Learn how to create and host Slack bots on Shard Cloud with Node.js and Python examples.

## Introduction

This guide covers creating and deploying Slack bots on Shard Cloud.

## Creating a Slack App

<Steps>
  <Step title="Create App">
    Go to [Slack API - Your Apps](https://api.slack.com/apps) and click
    **"Create New App"**.
  </Step>

  <Step title="Choose Creation Method">
    Select **"From scratch"**, name your app, and select your workspace.
  </Step>

  <Step title="Get Credentials">
    In **"Basic Information"**, note your **Signing Secret**. In **"OAuth &
    Permissions"**, add scopes like `chat:write`, `app_mentions:read`,
    `commands`.
  </Step>

  <Step title="Install to Workspace">
    Click **"Install to Workspace"** and copy the **Bot Token** (starts with
    `xoxb-`).
  </Step>
</Steps>

<Warning>
  **Security**: Never expose your Signing Secret or Bot Token publicly.
</Warning>

## Creating Your Bot

<Tabs>
  <Tab title="Node.js (@slack/bolt)">
    ### Setup

    ```bash theme={null}
    npm init -y
    npm install @slack/bolt
    ```

    ### Basic Bot Code

    ```javascript index.js theme={null}
    const { App } = require('@slack/bolt');

    const app = new App({
      signingSecret: process.env.SLACK_SIGNING_SECRET,
      token: process.env.SLACK_BOT_TOKEN,
    });

    // Respond to mentions
    app.event('app_mention', async ({ event, say }) => {
      await say(`<@${event.user}> Thanks for mentioning me!`);
    });

    // Slash command
    app.command('/ping', async ({ ack, respond }) => {
      await ack();
      await respond('Pong!');
    });

    (async () => {
      await app.start(process.env.PORT || 80);
      console.log('⚡️ Slack bot is running!');
    })();
    ```

    ### Package.json

    ```json package.json theme={null}
    {
      "name": "slack-bot",
      "version": "1.0.0",
      "main": "index.js",
      "dependencies": {
        "@slack/bolt": "^3.0.0"
      }
    }
    ```
  </Tab>

  <Tab title="Python (slack_bolt)">
    ### Setup

    ```bash theme={null}
    pip install slack_bolt
    ```

    ### Basic Bot Code

    ```python main.py theme={null}
    import os
    from slack_bolt import App

    app = App(
        signing_secret=os.environ.get('SLACK_SIGNING_SECRET'),
        token=os.environ.get('SLACK_BOT_TOKEN')
    )

    @app.event('app_mention')
    def handle_mention(event, say):
        user = event.get('user')
        say(f'<@{user}> Thanks for mentioning me!')

    @app.command('/ping')
    def ping_command(ack, respond):
        ack()
        respond('Pong!')

    if __name__ == '__main__':
        app.start(port=int(os.environ.get('PORT', 80)))
    ```

    ### Requirements.txt

    ```txt requirements.txt theme={null}
    slack_bolt
    ```
  </Tab>
</Tabs>

## Environment Variables

Set these in the Shard Cloud dashboard:

* `SLACK_SIGNING_SECRET`: Your app's signing secret
* `SLACK_BOT_TOKEN`: Your bot's OAuth token

## Shard Cloud Configuration

```systemd .shardcloud theme={null}
DISPLAY_NAME=Slack Bot
DESCRIPTION=My Slack Bot
MAIN=index.js
MEMORY=512
VERSION=recommended
SUBDOMAIN=my-slack-bot
```

<Note>
  The `SUBDOMAIN` is required for Slack to send events to your bot via webhooks.
</Note>

## Configuring Slack Events

After deployment, configure your Request URLs in Slack:

1. **Event Subscriptions**: Enable and set URL to `https://my-slack-bot.shardweb.app/slack/events`
2. **Slash Commands**: Set URL to `https://my-slack-bot.shardweb.app/slack/events`

## Deploying

<Steps>
  <Step title="Prepare Your Files">
    Ensure you have: - Your bot code file - `package.json` or `requirements.txt`

    * `.shardcloud`
  </Step>

  <Step title="Set Environment Variables">
    Configure `SLACK_SIGNING_SECRET` and `SLACK_BOT_TOKEN` in the dashboard.
  </Step>

  <Step title="Create ZIP Archive">
    Compress your project folder (excluding `node_modules/`).
  </Step>

  <Step title="Upload to Shard Cloud">
    Go to [Shard Cloud Dashboard](https://shardcloud.app/dash/applications) and
    upload.
  </Step>

  <Step title="Configure Slack URLs">
    Update Event Subscriptions and Slash Command URLs in your Slack app
    settings.
  </Step>
</Steps>

## Additional Resources

* [Bolt for JavaScript](https://docs.slack.dev/tools/bolt-js/)
* [Bolt for Python](https://docs.slack.dev/tools/bolt-python/)

## Troubleshooting

<AccordionGroup>
  <Accordion title="Events not received">
    * Verify your Request URL is correct - Ensure your app is running and
      accessible - Check that the signing secret matches
  </Accordion>

  <Accordion title="URL verification failed">
    Make sure your bot is running before verifying the URL in Slack.
  </Accordion>
</AccordionGroup>
