How can we manipulate strings in Slack workflows?
The primary use case is to extract URLs from previous workflow steps which inconveniently return them within a string.
For example: Here is the url! https://... Have fun!
I just want the https://...
part, so that I can format the message more nicely. Because links can be very long & ugly, and it would be better to display them as a simple link.
I ended up doing this by using custom functions/steps for workflow apps.
It's basically JavaScript/TypeScript code running in a secure Deno sandbox on Slack's own infrastructure, and it's pretty easy to setup once you wrap your head around it.
Here's the code for the function to extract a URL from some text:
import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
export const ExtractUrlDefinition = DefineFunction({
callback_id: "extract_url",
title: "Extract URL",
description: "Extracts a URL from some text",
source_file: "functions/extract_url.ts",
input_parameters: {
properties: {
text: {
type: Schema.types.string,
description: "The string in which to find & extract the URL",
},
},
required: ["text"],
},
output_parameters: {
properties: {
url: {
type: Schema.types.string,
description: "The extracted URL (if one was found)",
},
},
required: ["url"],
},
});
export default SlackFunction(
ExtractUrlDefinition,
({ inputs }) => {
const url = inputs.text.match(/http[^ ]+/)?.[0] || "";
return { outputs: { url } };
},
);