Skip to main content
In this guide, you’ll build an AI-powered Slack Bot that answers user questions right inside Slack. If you haven’t developed a plugin before, read the Plugin Development Quick Start Guide first.

Project Background

A Slack Bot plugin lets your team chat with an LLM directly in Slack, putting AI where conversations already happen. Slack is an open, real-time communication platform with a robust API, including a webhook-based event system that is straightforward to build on. This guide uses that system to create the Slack Bot plugin, illustrated in the diagram below:
Slack Bot Diagram
Two similar terms appear throughout this guide:
  • Slack Bot: A chatbot on the Slack platform—a virtual user you can interact with in real time.
  • Slack Bot plugin: A plugin in the Dify Marketplace that connects a Dify application with Slack. This guide shows you how to build it.

How It Works

  1. A user messages the Slack Bot When a user in Slack sends a message to the Bot, the Slack Bot immediately issues a webhook request to the Dify platform.
  2. Slack forwards the message to the Slack Bot plugin The Dify platform triggers the Slack Bot plugin, which relays the message to the Dify application—much like an email system delivering to a recipient’s address. You establish this connection by setting up a Slack webhook address through Slack’s API and entering it in the plugin. The plugin processes the Slack request and forwards it to the Dify application, where the LLM analyzes the input and generates a response.
  3. The plugin returns the response to Slack Once the plugin receives the reply from the Dify application, it sends the LLM’s answer back along the same route to the Slack Bot, so users get the response right where they’re chatting.

Prerequisites

To create the Slack App, go to the Slack API platform, create an app from scratch, and pick the workspace where it will be deployed.
Create a Slack App
  1. Enable Webhooks:
Enable Webhooks
  1. Install the App in Your Slack Workspace:
Install the App in Your Slack Workspace
  1. Obtain an OAuth Token for plugin development:
Obtain an OAuth Token for Future Plugin Development

1. Develop the Plugin

Before you start coding, make sure you’ve read Quick Start: Developing an Extension Plugin or have built a Dify plugin before.

1.1 Initialize the Project

Run the following command to set up your plugin development environment:
dify plugin init
Follow the prompts to provide basic project info. Select the extension template, and grant both Apps and Endpoints permissions. For additional details on reverse-invoking Dify services within a plugin, see Reverse Invocation: App.
Plugins Permission

1.2 Edit the Configuration Form

The plugin needs two pieces of information: which Dify app handles the replies, and the Slack App token that authenticates the bot’s responses. Add both fields to the plugin’s form. Modify the YAML file in the group directory (for example, group/slack.yaml). The form’s filename comes from the information you provided when creating the plugin, so adjust the path accordingly. Sample Code: slack.yaml
settings:
  - name: bot_token
    type: secret-input
    required: true
    label:
      en_US: Bot Token
      zh_Hans: Bot Token
      pt_BR: Token do Bot
      ja_JP: Bot Token
    placeholder:
      en_US: Please input your Bot Token
      zh_Hans: 请输入你的 Bot Token
      pt_BR: Por favor, insira seu Token do Bot
      ja_JP: ボットトークンを入力してください
  - name: allow_retry
    type: boolean
    required: false
    label:
      en_US: Allow Retry
      zh_Hans: 允许重试
      pt_BR: Permitir Retentativas
      ja_JP: 再試行を許可
    default: false
  - name: app
    type: app-selector
    required: true
    label:
      en_US: App
      zh_Hans: 应用
      pt_BR: App
      ja_JP: アプリ
    placeholder:
      en_US: the app you want to use to answer Slack messages
      zh_Hans: 你想要用来回答 Slack 消息的应用
      pt_BR: o app que você deseja usar para responder mensagens do Slack
      ja_JP: あなたが Slack メッセージに回答するために使用するアプリ
endpoints:
  - endpoints/slack.yaml
Two configuration fields deserve a closer look:
  - name: app
    type: app-selector
    scope: chat
  • type: Set to app-selector, which lets users forward messages to a specific Dify app when using this plugin.
  • scope: Set to chat, meaning the plugin can only interact with app types such as agent, chatbot, or chatflow.
Finally, in the endpoints/slack.yaml file, change the request method to POST so the endpoint can handle incoming Slack messages. Sample Code: endpoints/slack.yaml
path: "/"
method: "POST"
extra:
  python:
    source: "endpoints/slack.py"

2. Edit the Function Code

Modify the endpoints/slack.py file and add the following code:
import json
import traceback
from typing import Mapping
from werkzeug import Request, Response
from dify_plugin import Endpoint
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError


class SlackEndpoint(Endpoint):
    def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
        """
        Invokes the endpoint with the given request.
        """
        retry_num = r.headers.get("X-Slack-Retry-Num")
        if (not settings.get("allow_retry") and (r.headers.get("X-Slack-Retry-Reason") == "http_timeout" or ((retry_num is not None and int(retry_num) > 0)))):
            return Response(status=200, response="ok")
        data = r.get_json()

        # Handle Slack URL verification challenge
        if data.get("type") == "url_verification":
            return Response(
                response=json.dumps({"challenge": data.get("challenge")}),
                status=200,
                content_type="application/json"
            )

        if (data.get("type") == "event_callback"):
            event = data.get("event")
            if (event.get("type") == "app_mention"):
                message = event.get("text", "")
                if message.startswith("<@"):
                    message = message.split("> ", 1)[1] if "> " in message else message
                    channel = event.get("channel", "")
                    blocks = event.get("blocks", [])
                    blocks[0]["elements"][0]["elements"] = blocks[0].get("elements")[0].get("elements")[1:]
                    token = settings.get("bot_token")
                    client = WebClient(token=token)
                    try:
                        response = self.session.app.chat.invoke(
                            app_id=settings["app"]["app_id"],
                            query=message,
                            inputs={},
                            response_mode="blocking",
                        )
                        try:
                            blocks[0]["elements"][0]["elements"][0]["text"] = response.get("answer")
                            result = client.chat_postMessage(
                                channel=channel,
                                text=response.get("answer"),
                                blocks=blocks
                            )
                            return Response(
                                status=200,
                                response=json.dumps(result),
                                content_type="application/json"
                            )
                        except SlackApiError as e:
                            raise e
                    except Exception as e:
                        err = traceback.format_exc()
                        return Response(
                            status=200,
                            response="Sorry, I'm having trouble processing your request. Please try again later." + str(err),
                            content_type="text/plain",
                        )
                else:
                    return Response(status=200, response="ok")
            else:
                return Response(status=200, response="ok")
        else:
            return Response(status=200, response="ok")

3. Debug the Plugin

Go to the Dify platform and obtain the remote debugging address and key for your plugin.
Get the Remote Debugging Address and Key
Back in your plugin project, copy the .env.example file, rename it to .env, and fill in the debugging details:
INSTALL_METHOD=remote
REMOTE_INSTALL_URL=debug.dify.ai:5003
REMOTE_INSTALL_KEY=********-****-****-****-************
Start the plugin:
python -m main
You should now see the plugin installed in your Workspace on Dify’s plugin management page, where other team members can also access it.

Configure the Plugin Endpoint

On Dify’s plugin management page, locate the newly installed test plugin and create a new endpoint. Enter a name and your Bot Token, then select the app you want to connect.
Test Plugins
After saving, a POST request URL is generated:
Generated POST Request URL
Next, complete the Slack App setup:
  1. Enable Event Subscriptions
    Enable Event Subscriptions
    Paste the POST request URL you generated above.
    Paste the POST Request URL You Generated Above
  2. Grant Required Permissions
    Grant Required Permissions

4. Verify the Plugin

The plugin calls the Dify application through self.session.app.chat.invoke, passing in parameters such as app_id and query, and returns the response to the Slack Bot. Run python -m main again to restart the plugin, then check that Slack displays the Dify app’s reply:
Slack Displays the Dify App Reply

5. Package the Plugin (Optional)

Once you confirm the plugin works correctly, package it with the following command. The command produces a slack_bot.difypkg file in the current directory—your final plugin package. For detailed packaging steps, see Package as a Local File and Share.
# Replace ./slack_bot with your actual plugin project path.

dify plugin package ./slack_bot
Congratulations—you’ve developed, tested, and packaged a plugin!

6. Publish the Plugin (Optional)

You can now upload it to the Dify Marketplace repository for public release. Before publishing, ensure your plugin meets the Publishing to Dify Marketplace Guidelines. Once approved, your code is merged into the main branch, and the plugin goes live on the Dify Marketplace.

Further Reading

For a complete Dify plugin project example, visit the GitHub repository. You’ll also find additional plugins with full source code and implementation details. To explore more about plugin development, see the following: Quick Starts: Plugin Interface Docs:
Edit this page | Report an issue