Trigger Example with Slack
You can launch triggers directly from Slack using slash commands, providing responses to the conversation from within the executions running through Valohai.
In this guide, we will be getting an image from a Slack conversation and passing it on to an execution that modifies it, then posting the result back to the Slack conversation.
While we will use a basic image manipulation script as the middle step as a visually obvious modification, you can easily plug in any Valohai execution that takes an input image and produces an output image in its place by changing the input names in the pipeline layout.
Slack API setup
You will need the message verification secret and Slack API token first.
Create a new Slack App.
When you are logged in to Slack chat in the app or web, navigate to the drop-down menu by your workspace name near the top left corner of the window. Under "Tools and Settings" you should find "Manage Apps." Click it to navigate to the Slack management portal.
On the Slack management portal, click the "Build" link on the top navigation bar. This will navigate you to the Slack developer portal.
Click on "Create new app"
Select "From app manifest".
Select the workspace to add the app to
Enter the app manifest (example manifest below). You should change the app name to your liking, or you can do it later.
Review the app's summary and finish creating the app. It should have these permissions:
channels:history,channels:join,channels:read,chat:write,commands,files:read,files:writeInstall the app to your Slack workspace.
Example app manifest:
The integration requires two authentication tokens from your Slack app to interact with Slack.
Signing Secret: From your app summary page (where you installed it), browse down to find the Signing Secret. Click "Show" and copy the token into your notes.
Bot User OAuth Token: From the app summary page, browse the sidebar to "OAuth and Permissions." Click it to navigate to the OAuth configuration page. Copy the Bot User OAuth Token into your notes.
Valohai project setup
Add the following steps and pipeline to your valohai.yaml configuration file, commit the changes, push them to Git and fetch the new commit to Valohai.
Create a file called slack_input.py.
Create a file called slack_output.py.
Create a file called deepfry.py.
Add these steps and pipeline definition to your valohai.yaml:
We can now define the webhook trigger. Go to Project settings -> open Triggers tab -> create New Trigger
For type, select Webhook
Add a Web Request Authentication Condition
For Auth Type, select "HMAC"
For Token Lookup Namespace, select "HTTP Header"
For Token Lookup Key, type
X-Slack-SignatureFor Secret Key, type the Slack Signing Secret from your notes
For Value Prefix, type
v0=For HMAC Basestring Format, enter
v0:{header:X-Slack-Request-Timestamp}:{body}For Algorithm, select "SHA256"
Add a Web Request Timestamp condition
For Token Lookup Namespace, select "HTTP Header"
For Token Lookup Key, type
X-Slack-Request-TimestampTolerance of 60s is ok.
Add a Rate Limit condition
A Period of 60 seconds with a quota of 2-3 is fine.
As the slash command can be triggered on Slack any time, a rate limit can help limit excess usage.
Exceeding the rate limit will cause the webhook endpoint to return a 400 status response, and too many of these during a short time will cause Slack to disable the command. You may need to enable it again in the Slack developer portal.
Add a Run Pipeline action
For Source Commit Reference, type the primary branch of your Valohai project in Git. This is usually either
mainormasterFor Pipeline name, type "Business Deepfrier 2024"
For Payload Input Name, type "slack-slash.slack-webhook-input"
Add a Web Text Response action
For Response Text, type "Deep frying..."
This text displayed by Slack as the immediate response to the Slack slash command before the pipeline boots up. Feel free to customize this to add your own touch
Save trigger
You are now on the Triggers list again. Select the ... menu of the trigger we just created, and select "Edit" to return to editing the trigger. Get the trigger URL and save it to your notes.
Navigate to your Valohai project's Environment Variables settings, and add the Bot User OAuth Token from your notes as SLACK_TOKEN. You should mark it as a secret.
Slack slash command
Return to the Slack app portal. Select your integration app and navigate to "Slash Commands" on the sidebar.
Click "Create new command."
For Command, type
/deepfryFor Request URL, enter the Valohai trigger URL from the previous step
The short description and usage hint are optional.
Save the slash command.
Invite your bot user to a Slack conversation (DM or channel) in your workspace: /invite @My Slack Integration
Last updated
Was this helpful?
