@weweb/backend-slack
TypeScript icon, indicating that this package has built-in type declarations

0.1.34 • Public • Published

WeWeb Slack Integration

A WeWeb backend integration for Slack's API, providing access to messaging, channels, users, and other Slack features within WeWeb backend workflows. This integration uses the official @slack/web-api package for robust and type-safe interactions with Slack's API.

Features

  • Simple integration with Slack's API
  • Send messages to channels and users
  • Create and manage channels
  • Invite users to channels
  • Upload files to Slack
  • Search messages
  • Access user and workspace information

Installation

This package is designed to work with the WeWeb Supabase Backend Builder and Deno.

import { serve } from '@weweb/backend-core';
import { createSlackIntegration } from '@weweb/backend-slack';

Usage

Basic Setup

import type { BackendConfig } from '@weweb/backend-core';
import { serve } from '@weweb/backend-core';
import Slack from '@weweb/backend-slack';

// Define your backend configuration
const config: BackendConfig = {
  workflows: [
    // Your workflows here
  ],
  integrations: [
    // Use the default Slack integration
    Slack,
    // Or add other integrations
  ],
  production: false,
};

// Start the server
const server = serve(config);

Custom Configuration

You can customize the Slack client by using the createSlackIntegration function:

import { createSlackIntegration } from '@weweb/backend-slack';

// Create a custom Slack integration
const customSlack = createSlackIntegration({
  botToken: 'xoxb-your-bot-token', // Override environment variable
  userToken: 'xoxp-your-user-token', // Optional
  apiUrl: 'https://your-custom-endpoint.com', // Optional
});

If not specified, the integration will use the following environment variables:

  • SLACK_BOT_TOKEN - Your Slack Bot User OAuth Token (xoxb-...)
  • SLACK_USER_TOKEN - Your Slack User OAuth Token (xoxp-...)
  • SLACK_API_URL - Custom API endpoint URL (optional, defaults to https://slack.com/api)

Available Methods

Post Message

Send messages to channels or users.

// Example workflow action
const config = {
  type: 'action',
  id: 'send_slack_message',
  actionId: 'slack.post_message',
  inputMapping: [
    {
      channel: 'general',
      text: 'Hello from WeWeb! 👋',
      mrkdwn: true
    }
  ]
};

Create Channel

Create a new public channel in a workspace.

// Example workflow action
const config = {
  type: 'action',
  id: 'create_slack_channel',
  actionId: 'slack.create_channel',
  inputMapping: [
    {
      name: 'project-updates',
      validate: true
    }
  ]
};

Invite to Channel

Invite users to a channel.

// Example workflow action
const config = {
  type: 'action',
  id: 'invite_users',
  actionId: 'slack.invite_to_channel',
  inputMapping: [
    {
      channel: 'project-updates',
      users: 'U0123456789,U9876543210'
    }
  ]
};

List Channels

Get a list of all channels in the workspace.

// Example workflow action
const config = {
  type: 'action',
  id: 'get_channels',
  actionId: 'slack.list_channels',
  inputMapping: [
    {
      exclude_archived: true,
      limit: 100
    }
  ]
};

List Users

Get a list of all users in the workspace.

// Example workflow action
const config = {
  type: 'action',
  id: 'get_users',
  actionId: 'slack.list_users',
  inputMapping: [
    {
      limit: 100
    }
  ]
};

Upload File

Upload a file to Slack.

// Example workflow action
const config = {
  type: 'action',
  id: 'upload_file',
  actionId: 'slack.upload_file',
  inputMapping: [
    {
      file: '$body.fileContent', // Base64 encoded file
      filename: 'report.pdf',
      title: 'Monthly Report',
      channels: 'general,reports'
    }
  ]
};

Example: Complete Notification Application

import type { BackendConfig } from '@weweb/backend-core';
import { serve } from '@weweb/backend-core';
import Slack from '@weweb/backend-slack';

const config: BackendConfig = {
  workflows: [
    {
      path: '/notify',
      methods: ['POST'],
      security: {
        accessRule: 'public',
      },
      inputsValidation: {
        body: {
          type: 'object',
          properties: {
            channel: { type: 'string' },
            title: { type: 'string' },
            message: { type: 'string' },
            importance: { type: 'string', enum: ['low', 'medium', 'high'] }
          },
          required: ['channel', 'title', 'message'],
        },
      },
      workflow: [
        {
          type: 'action',
          id: 'send_notification',
          actionId: 'slack.post_message',
          inputMapping: [
            {
              channel: '$body.channel',
              blocks: [
                {
                  type: 'header',
                  text: {
                    type: 'plain_text',
                    text: '$body.title'
                  }
                },
                {
                  type: 'section',
                  text: {
                    type: 'mrkdwn',
                    text: '$body.message'
                  }
                },
                {
                  type: 'context',
                  elements: [
                    {
                      type: 'mrkdwn',
                      text: '*Importance:* $body.importance'
                    }
                  ]
                }
              ]
            },
          ],
        },
      ],
    },
  ],
  integrations: [Slack],
  production: false,
};

console.log('Starting Slack notification server on http://localhost:8000/notify');
serve(config);

Authentication

This integration uses the official Slack Web API client and requires Slack API tokens to function:

  1. Bot Token (xoxb-...): Used for most API calls. This token is obtained by creating a Slack App, adding Bot Token Scopes, and installing the app to your workspace.

  2. User Token (xoxp-...): Required for some user-level actions like search_messages. This token is obtained by adding User Token Scopes to your Slack App.

For full details on obtaining these tokens, see Slack's API documentation.

Required Scopes

Depending on which methods you use, your Slack app will need different OAuth scopes:

Bot Token Scopes

  • chat:write - For posting messages
  • channels:read - For listing channels
  • channels:write - For creating channels
  • users:read - For listing users
  • reactions:write - For adding reactions
  • files:write - For uploading files

User Token Scopes

  • search:read - For searching messages

Make sure to add the required scopes to your Slack app based on the functionality you need.

Readme

Keywords

none

Package Sidebar

Install

npm i @weweb/backend-slack

Weekly Downloads

19

Version

0.1.34

License

none

Unpacked Size

23.4 kB

Total Files

9

Last publish

Collaborators

  • kram
  • aureliev
  • leo.coletta