Integrating intelligent LLM-powered applications with Slack is a very popular use-case for Griptape Cloud. This sample provides a complete implementation of an event handler for a Slack Application within a Griptape Structure, ready for you to deploy it on Griptape Cloud.
Environment Variables
OPENAI_API_KEY=<secret_ref> # Reference your own key from a Griptape Secret
GT_CLOUD_API_KEY=<secret_ref> # Reference your own key from a Griptape SecretThis sample is intended to run as a Griptape Cloud hosted Structure.
Follow the instructions in the main README of this repo to deploy this Structure to your Griptape Cloud account.
- Go to Griptape Cloud Integrations and fill out the form for creating a Slack integration.
- Skip the entry about Bot Token and Signing Secret, you will update those later.
- If you have already created your Slack Bot Structure, select it now under the "Structures" field.
- Copy the Slack App Manifest on the Integration config page.
- Go to your Slack Apps Page and Press "Create New App" -> "From a manifest".
- Paste in the Slack App Manifest, and click through until the app is created.
- Get the Signing Secret from the "Basic Information" tab from your newly created App.
- Go back to your integration page on Griptape Cloud and type in a secret name under "Slack App Secret" and hit enter.
- Paste in the value of the Signing Secret.
- Go back to your Slack App and press the "OAuth & Permissions" tab.
- Click the "Install to
workspace" button, and click through to install the App into your Slack workspace. - Copy the newly created Bot Token.
- Go back to your integration page on Griptape Cloud and type in a secret name under "Slack Bot Token" and hit enter.
- Paste in the value of the Bot Token.
- Lastly, go back to your slack app and press the "Event Subscriptions" tab.
- Hit "retry" next to the URL displayed in the field. It should have a green checkmark and say "Verified".
That's it! Now find your app in Slack and start chatting. It can be added to channels as well as messaged in private chats.
The bot will always respond with a three-dots gif to indicate it has started processing messages. It will then update this message over time, explaining its actions, and then ultimately overwrite the message with its final response.
There are a few ways to configure other behavior of the bot at runtime.
Set environment variables to include or exclude specific Slack channels that the Slackbot Structure should respond in:
The FILTER_IN_CHANNELS configuration overrides the FILTER_OUT_CHANNELS if both are provided. The following configuration will allow the bot to only respond in channels "A012BA2A9DG" and "B012BA2A9DG".
FILTER_IN_CHANNELS="A012BA2A9DG,B012BA2A9DG"The following configuration will restrict the bot from responding in only these channels "A012BA2A9DG" and "B012BA2A9DG".
FILTER_OUT_CHANNELS="A012BA2A9DG,B012BA2A9DG"The following configuration will disable communication with the bot in direct messages:
DISABLE_IM="true"To find the Channel ID for a particular Slack channel:
- Right click the channel
- Click
View channel details - Locate the Channel ID at the bottom of the window
Using the GriptapeCloudRulesetDriver allows the bot to pull in Rulesets for every event that comes through. For every message, the bot will reach out to Griptape Cloud and try to find Rulesets that are aliased with the following values:
- Slack App ID
- Slack Team ID
- Slack Channel ID
- Slack User ID
Simply create a Griptape Cloud Ruleset and set the alias field to any one of those values, and the bot will find and use them.
The bot will always respond in a Slack thread, creating a new one if needed. Outside of a DM, the bot will only respond if explicitly tagged with @bot_name. However, the bot is picking up other messages and storing them in a Griptape Cloud Thread, and will be able to understand previous context if tagged in a message later in a Slack thread.
Creating a "Kitchen Sink" Agent with Griptape is really easy, but sometimes the LLM can get confused if it is given too many instructions. The get_tools function with parameter dyanamic=True will prompt the LLM to decide which tools it should use to accomplish a given input, and those tools will be passed to the final "Kitchen Sink" agent that will respond to the user. It will use other context, such as conversation history, to make its decision.
This can be enabled by setting "enable_toolbox": "true" in the metadata of any Ruleset that gets pulled in.
Responses from the Griptape Agent can be streamed token-by-token for faster perceived response times. These tokens will be batched and sent as larger message chunks back to slack, updating the bot's initial response message over time.
This can be enabled by setting "stream": "true" in the metadata of any Ruleset that gets pulled in.