The new Slack platform and the features described below are in beta and under active development.

Slack API methods

While building your Run On Slack functions, you can access and make calls to the Slack API.

To do this, you will:

  1. Import the API library
  2. Add the token context property
  3. Instantiate a Slack API client object
  4. Make an API call

1. Import the API library

The first step in making API calls in your Run On Slack functions is to import the API library in the file where you are implementing your function:

import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
import { SlackAPI } from "deno-slack-api/mod.ts"; // Add this

2. Add the token context property

The next step is to add the token context property to your function handler:

import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
import { SlackAPI } from "deno-slack-api/mod.ts";

export const ExampleFunctionDefinition = DefineFunction({
  callback_id: "example_function_def",
  title: "Example function",
  source_file: "functions/example_function.ts",
});

export default SlackFunction(
  ExampleFunctionDefinition,
  ({ inputs, token }) => { // Add `token` here

  // ...

3. Instantiate a Slack API client

Next, instantiate the Slack API client by passing along the token context property to SlackAPI:

import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
import { SlackAPI } from "deno-slack-api/mod.ts"; // Add this

export const ExampleFunctionDefinition = DefineFunction({
  callback_id: "example_function_def",
  title: "Example function",
  source_file: "functions/example_function.ts",
});

export default SlackFunction(
  ExampleFunctionDefinition,
  ({ inputs, token }) => {
  
  const client = SlackAPI(token); // Add this
  
  // ...

4. Make an API call

Finally, make the API call:

import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
import { SlackAPI } from "deno-slack-api/mod.ts"; // Add this

export const ExampleFunctionDefinition = DefineFunction({
  callback_id: "example_function_def",
  title: "Example function",
  source_file: "functions/example_function.ts",
});

export default SlackFunction(
  ExampleFunctionDefinition,
  ({ inputs, token }) => {
  
  const client = SlackAPI(token);

  // Call an API method in one of two ways.

  // Option #1: use client.apiCall(<method name>)
  await client.apiCall('chat.postMessage', {
    channel: inputs.some_channel,
    text: `You said "${inputs.some_string}"`
  });

  // Option #2: use client.methodGroup.method
  await client.chat.postMessage({
    channel: inputs.some_channel,
    text: `You said "${inputs.some_string}"`
  });
  
  // ...

Most API endpoints require specific permission scopes. Add scopes to your app by listing them in the botScopes property of your manifest.

Have 2 minutes to provide some feedback?

We'd love to hear about your experience with the new Slack platform. Please complete our short survey so we can use your feedback to improve.