slack-agent-tool(Python)

Loading...

Create agent tools for Slack

This notebook creates agent tools that interact with your Slack workspace. These tools can be used to retrieve information, send messages, or perform actions in your Slack workspace.

Requirements

Before you begin, ensure that you have a token to authenticate to your Slack workspace. If you use a bearer token for an app, all actions will be performed as the app itself. However, if you use a user token, actions will be performed as the authenticated user. Choose the token type that best fits your use case.

See Slack documentation for more information on how to get started.

Setup

Install the databricks-sdk.

Update the following parameters with information for your catalog and provide a name for a new connection.

  • uc_catalog: Specifies the catalog where the external function will be created.
  • uc_schema: Specifies the schema in the catalog where the external function will be created.
  • connection_name: Specifies the connection name.
3

4

5

Create a Unity Catalog Connection

Create a Unity Catalog Connection by providing the host URL and token for authentication. To learn more about supported authentication methods, see Databricks documentation (AWS | Azure).

To securely manage your authentication details, we recommend using Databricks Secrets. Replace the placeholders <secret-scope> and <secret-key> with your specific secret scope and secret key.

If you prefer, you can also create a connection through the Catalog Explorer UI. See Databricks documentation (AWS | Azure).

7

Test the connection

Test the connection works by listing all channels for your Slack team.

9

Create a Unity Catalog function

Now that you have a working connection, create a Unity Catalog function that uses this connection.

Update the function name (currently slack_list_conversations) and comment as you see fit.

11

Call the function to make sure it works.

13

Build a Unity Catalog function that sends messages to a channel

In this section, you create a function that allows users to send messages to a channel.

According to the Slack API documentation, sending a message requires a channel ID. However, typically users only know the channel name, not the ID. To bridge this gap, define multiple functions that work together to achieve the following:

  1. Retrieve a channel ID from a channel name

    • This function looks up the channel ID based on the provided channel name.
  2. Send a message using a channel ID

    • This function directly posts a message to a Slack channel when given its ID.
  3. Combine both functions to allow sending messages using a channel name

    • This function first retrieves the channel ID using the channel name, then sends the message.

Remember: When defining these functions, it's important to use clear names and well-structured comments. This ensures that an agent can accurately determine which function to call based on the task at hand. Try updating the names and comments on any of the following functions to experiment.

15

16

Define the function to send a message.

18

Note that running the following cell will send a message to your Slack workspace!

Replace the channel ID with your result from get_channel_id.

20

Combine the functions

You can modify the following function to have additional processing on the text to be sent, or to remove it entirely if you are sending your message as an authenticated user.

22

Send a message to the Slack channel by specifying the channel-name and sender-identifier

24

Now that you've created a few Slack tools to get started, see Slack documentation to help you create more tools for your use case!

Add the tool to an AI agent

Now that you have a Unity Catalog function, you can add it to an AI agent. See Databricks documentation (AWS | Azure).

Create a tool in agent code (optional)

As an alternative, you can also define this tool directly in agent code. To learn more about the differences, see Databricks documentation (AWS | Azure)

Rewrite the Unity Catalog function to send a Slack message to the provided channel (specified from ID) in code. You can replace the ID with a channel ID from your Slack workspace, or you can try to modify this code to send a slack message using a provided channel name like you did above in the Unity Catalog function.

28

29