Using Slack Built-in functions

This feature is confidential
This feature has not been released publicly, and we politely request that you treat everything in this document as confidential.

All details outlined in this document are in beta. We may change the timeline, APIs, and functionality based on feedback from pilot testers like yourself, so you should be prepared to update your code accordingly.

Custom functions are standalone configurations that execute specific tasks on Slack. Built-in functions perform Slack-native functionality and can be used alongside your custom functions to create a complete workflow. This tutorial will focus on using built-in functions to create a new project channel from a message on Slack.

1Set up Slack CLI

Use the new Slack CLI to create and deploy Slack apps. With the Slack CLI, developers can create an app and generate some boilerplate code in seconds.

This section walks through setting up your environment for next gen platform development.

  • Install the Slack CLI

    We've built a command line interface to make the process of creating new apps, running them, and deploying them easier. Binaries are available for macOS and Windows.

    Deno is currently a pre-requisite for using the CLI — if you do not have Deno installed, please do so first.

    Additionally, the experience of writing an app will be greatly enhanced if your IDE includes support for Deno. Here's a plugin to add Deno capabilities to VS Code.

    To install the Slack CLI:

    1. Download the latest binary for your platform
    2. Decompress the downloaded file. On macOS, copy the slack binary to a destination in your path (we recommend /usr/local/bin). For Windows, copy the .exe file into any location accessible in your path.
    3. Test the app is properly installed by running slack version and verifying the version matches the one you downloaded.

    Now you can now use the Slack CLI to authenticate and create a new app.

  • Authenticate the Slack CLI

    Before you can create an app, you must authenticate.

    1. Run slack login — this will display some instruction text and a string to copy and paste in any channel in the test workspace.

          /hermesauthticket ABC123DEF...XYZ
      
    2. Copy the entire string, including the leading /, and paste it into any channel in your Slack workspace. (You are essentially calling a special slash command that will authenticate your app with Slack).

    3. Run slack auth info for details on your active workspace authorization.

    Now that you've authenticated, you can create a new app.

  • Create a new app

    Now you can create an app! cd to a directory where you have write access and want your app to live, most likely in your home. Then, run

    $ slack create [optional-project-name]
    

    This will create a new project called with a random name like priceless-lemur-123.

    If you want to name your project something specific, include an additional parameter

    $ slack create my-new-project
    

    Once the app has finished running, you'll have a new directory that contains a skeleton app.

    $ cd my-new-project
    

    The structure of these next-generation apps aims to favor convention over configuration. Directories are named logically and the files within those directories map to specific functionality with an app. For example, the functions directory contains a list of an app's functions, with a single file for each function an app defines.

2Understand how apps are structured and setup your application to support built-in functions.

This section walks you through the structure of your app and how the different pieces tie together.

We will then define a function invoked by a Slack trigger that creates a new channel, sets the topic for the channel and sends a welcome message.

  • Understand how the app is structured

    Your new project has a directory structure that should resemble the following:

    .slack/
      - apps.json
      - cli.json
    assets/
      - icon.png
    functions/
     - myfunction.ts
    tables/
     - mytable.ts
    triggers/
     - mytrigger.ts
    workflows/
     - myworkflow.ts
    import_map.json
    project.ts
    README.md
    

    Functions: Modular, reusable, atomic blocks of functionality that can be grouped together as workflows. A function defines inputs, performs some useful bit of work using those inputs, and responds with pre-defined outputs. You'll be able to quickly write custom functions, deploy them via the CLI, and building workflows using these functions. Check out our functions documentation to learn more.

    Workflows: A workflow is a group of functions, including built-in functions provided by Slack. Check out our workflows documentation to learn more.

    Triggers: Define how workflows are called, such as a message action, shortcut, or event, where it is able to be executed, and who can execute it. Check out our triggers documentation to learn more.

    Tables: Define tables and store data to power your application. Check out our tables documentation to learn more.

    Project.ts: Contains all of the metadata about a project, such as the name of project, description, and requested OAuth scopes. It also contains a list of defined functions, tables, triggers and workflows.

  • Adding a new workflow to your app

    Now that we have defined our NewProjectChannel, we need to connect it to Workflow.

    Each input parameter represents an input field in the Workflow modal. The end user will be expected to fill out the modal with a channel name, channel purpose/topic and select a user to add to the channel.

    1. Create a project_wf.ts file under the workflows/ directory of your project.
    2. Copy and paste the following code into it:
    import { DefineWorkflow, Schema } from "slack-cloud-sdk/mod.ts";
    import { NewProjectChannel } from "../functions/project_channel_func.ts";
    
    export const NewProjWorkflow = DefineWorkflow("new_channel", {
      title: "Create new project",
      description: "Create a new project channel from a message.",
      input_parameters: {
        projtitle: {
          type: Schema.types.string,
          description: "New channel name. All lowercase, no spaces.",
        },
        purpose: {
          type: Schema.types.string,
          description: "Write a short description about your project",
        },
        user: {
          type: Schema.slack.types.user_id,
          description: "Invite yourself or another user to the project channel",
        },
      },
    });
    
  • Adding Slack built-in functions to your app

    The core functionality of this workflow depends solely on built-in functions.

    The NewProjWorkflow workflow uses built-in functions to create a new channel, invite a user to the channel, update the channel topic and post a welcome message.

    The createChannelStep,updateTopicStep and inviteUserStep steps all call existing built-in functions that mimic some of Slack's native functionality. createChannelStep uses Schema.slack.functions.CreateChannel to create a channel. updateTopicStep uses Schema.slack.functions.UpdateChannelTopic to update the channel topic and inviteUserStep uses the Schema.slack.functions.InviteUserToChannel built-in function to invite a user to the channel. Lastly, we use the Schema.slack.functions.SendMessage built-in function to send a welcome message into the created channel.

    1. Copy and paste the following code into the project_wf.ts file:
    const createChannelStep = NewProjWorkflow.addStep(
      Schema.slack.functions.CreateChannel,
      {
        channel_name: NewProjWorkflow.inputs.projtitle,
        is_private: false,
      },
    );
    const updateTopicStep = NewProjWorkflow.addStep(
      Schema.slack.functions.UpdateChannelTopic,
      {
        channel_id: createChannelStep.outputs.channel_id,
        topic: NewProjWorkflow.inputs.purpose,
      },
    );
    const inviteUserStep = NewProjWorkflow.addStep(
      Schema.slack.functions.InviteUserToChannel,
      {
        channel_id: createChannelStep.outputs.channel_id,
        user_id: NewProjWorkflow.inputs.user,
      },
    );
    NewProjWorkflow.addStep(Schema.slack.functions.SendMessage, {
      channel_id: createChannelStep.outputs.channel_id,
      message:
        `Welcome <@${inviteUserStep.outputs.user_id}> :tada:\n You've started a new project: *#${NewProjWorkflow.inputs.projtitle}*. The topic of this channel is: *${updateTopicStep.outputs.topic}*`,
    });
    
    
    1. The workflow is now complete. Next, we need to import and reference the workflow we just created in the project metadata file, located at project.ts in the project root:
      1. Import your new workflow at the top of your project.ts file:

        import { NewProjWorkflow } from "./workflows/project_wf.ts";

      2. Make sure NewProjWorkflow is referenced in the workflows array of your Project definition, so the relevant section of your project.ts file looks like the following:

        workflows: [NewProjWorkflow],

  • Adding a new trigger to your app

    Now that we have defined our NewProjWorkflow, we need to connect it to Trigger. There are many different kinds of triggers, but for our purposes we will use a Message Shortcut trigger. This shortcut will be how users in your Slack workspace will interact with this app. End users will be able to trigger the workflow from any message on Slack.

    Let's create a trigger that leverages our NewProjWorkflow by taking its output and using a built-in function to create a new project channel that updates the channel topic with the text from the selected message.

    1. Create a new_channel_shortcut.ts file under the triggers/ directory of your project.
    2. Copy and paste the following code into it:
    import { DefineTrigger, TriggerTypes } from "slack-cloud-sdk/mod.ts";
    import { NewProjWorkflow } from "../workflows/project_wf.ts";
    
    export const NewProjectShortcut = DefineTrigger("new_project_shortcut", {
      type: TriggerTypes.MessageShortcut,
      name: "New Project channel",
      description: "Create a new channel for your project",
    })
      .runs(NewProjWorkflow)
      .withInputs((ctx) => ({
        purpose: ctx.data.message.text,
      }));
    
    1. Now we need to import and reference the trigger we just created in the project metadata file, located at project.ts in the project root:
      1. Import your new trigger at the top of your project.ts file:

        import { NewChannelShortcut } from "./triggers/new_channel_shortcut.ts";

      2. Make sure NewChannelShortcut is referenced in the triggers array of your Project definition, so the relevant section of your project.ts file looks like the following:

        triggers: [NewChannelShortcut],

3Review project triggers, workflows, and functions before a widespread deploy

Before deploying your app to a production workspace, let's confirm that your project.ts is configured appropriately.

  • Review project.ts

    Let's take a quick minute to review our project.ts file to confirm everything looks right.

    import { Project } from "slack-cloud-sdk/mod.ts";
    import { NewProjWorkflow } from "./workflows/project_wf.ts";
    import { NewChannelShortcut } from "./triggers/new_channel_shortcut.ts";
    
    Project({
      name: "Project Organizer",
      description: "A demo showing how to create a channel from a message trigger",
      icon: "assets/icon.png",
      runtime: "deno1.x",
      botScopes: ["commands", "chat:write", "chat:write.public"],
      functions: [],
      workflows: [NewProjWorkflow],
      triggers: [NewProjectShortcut],
      tables: [],
      outgoingDomains: [],
    });
    });
    
    

    You should now be able to run the app locally with slack run or deploy it to Slack's hosting infrastructure via slack deploy.

    This concludes the setting up the app!

Was this page helpful?