> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Slack 工具包集成

> 使用 LangChain Python 与 Slack 工具包集成。

这将帮助您开始使用 Slack [工具包](/oss/python/integrations/tools/slack)。有关所有 `SlackToolkit` 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/python/langchain-community/agent_toolkits/slack/toolkit/SlackToolkit)。

## 设置

要使用此工具包，您需要按照 [Slack API 文档](https://api.slack.com/tutorials/tracks/getting-a-token) 中的说明获取令牌。收到 SLACK\_USER\_TOKEN 后，您可以将其作为环境变量输入如下。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if not os.getenv("SLACK_USER_TOKEN"):
    os.environ["SLACK_USER_TOKEN"] = getpass.getpass("Enter your Slack user token: ")
```

要启用单个工具的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

此工具包位于 `langchain-community` 包中。我们还需要 Slack SDK：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-community slack_sdk
```

可选地，我们可以安装 beautifulsoup4 以协助解析 HTML 消息：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU beautifulsoup4 # This is optional but is useful for parsing HTML messages
```

## 实例化

现在我们可以实例化我们的工具包：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.agent_toolkits import SlackToolkit

toolkit = SlackToolkit()
```

## 工具

查看可用工具：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tools = toolkit.get_tools()

tools
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[SlackGetChannel(client=<slack_sdk.web.client.WebClient object at 0x113caa8c0>),
 SlackGetMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa4d0>),
 SlackScheduleMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa440>),
 SlackSendMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa410>)]
```

此工具包加载了以下工具：

* [SlackGetChannel](https://reference.langchain.com/python/langchain-community/tools/slack/get_channel/SlackGetChannel)
* [SlackGetMessage](https://reference.langchain.com/python/langchain-community/tools/slack/get_message/SlackGetMessage)
* [SlackScheduleMessage](https://reference.langchain.com/python/langchain-community/tools/slack/schedule_message/SlackScheduleMessage)
* [SlackSendMessage](https://reference.langchain.com/python/langchain-community/tools/slack/send_message/SlackSendMessage)

## 在代理中使用

让我们为代理配备 Slack 工具包，并查询有关频道的信息。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent


model = ChatOpenAI(model="gpt-4.1-mini")

agent_executor = create_agent(model, tools)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
example_query = "When was the #general channel created?"

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)
for event in events:
    message = event["messages"][-1]
    if message.type != "tool":  # mask sensitive information
        event["messages"][-1].pretty_print()
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
================================ Human Message =================================

When was the #general channel created?
================================== Ai Message ==================================
Tool Calls:
  get_channelid_name_dict (call_NXDkALjoOx97uF1v0CoZTqtJ)
 Call ID: call_NXDkALjoOx97uF1v0CoZTqtJ
  Args:
================================== Ai Message ==================================

The #general channel was created on timestamp 1671043305.
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
example_query = "Send a friendly greeting to channel C072Q1LP4QM."

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)
for event in events:
    message = event["messages"][-1]
    if message.type != "tool":  # mask sensitive information
        event["messages"][-1].pretty_print()
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
================================ Human Message =================================

Send a friendly greeting to channel C072Q1LP4QM.
================================== Ai Message ==================================
Tool Calls:
  send_message (call_xQxpv4wFeAZNZgSBJRIuaizi)
 Call ID: call_xQxpv4wFeAZNZgSBJRIuaizi
  Args:
    message: Hello! Have a great day!
    channel: C072Q1LP4QM
================================== Ai Message ==================================

I have sent a friendly greeting to the channel C072Q1LP4QM.
```

***

## API 参考

有关所有 `SlackToolkit` 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/python/langchain-community/agent_toolkits/slack/toolkit/SlackToolkit)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\tools\slack.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
