slackapi / deno-slack-sdk

SDK for building Run on Slack apps using Deno
https://api.slack.com/automation
MIT License
166 stars 28 forks source link

Deno Slack SDK

deno.land version deno dependencies Samples Integration Type-checking

A Deno SDK to build Slack apps with the latest platform features. Read the quickstart guide and look at our code samples to learn how to build apps.

Versioning

Releases for this repository follow the SemVer versioning scheme. The SDK's contract is determined by the top-level exports from src/mod.ts and src/types.ts. Exports not included in these files are deemed internal and any modifications will not be treated as breaking changes. As such, internal exports should be treated as unstable and used at your own risk.

Setup

Make sure you have a development workspace where you have permission to install apps. Please note that the features in this project require that the workspace be part of a Slack paid plan.

Install the Slack CLI

You need to install and configure the Slack CLI. Step-by-step instructions can be found on our install & authorize page.

Creating an app

Create a blank project by executing the following command:

slack create my-app --template slack-samples/deno-blank-template

cd my-app/

The manifest.ts file contains the app's configuration. This file defines attributes like app name, description and functions.

Create a function

mkdir ./functions && touch ./functions/hello_world.ts
// Contents of ./functions/hello_world.ts
import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";

export const HelloWorldFunctionDef = DefineFunction({
  callback_id: "hello_world_function",
  title: "Hello World",
  source_file: "functions/hello_world.ts",
  input_parameters: {
    properties: {},
    required: [],
  },
  output_parameters: {
    properties: {
      message: {
        type: Schema.types.string,
        description: "Hello world message",
      },
    },
    required: ["message"],
  },
});

export default SlackFunction(
  HelloWorldFunctionDef,
  () => {
    return {
      outputs: { message: "Hello World!" },
    };
  },
);

DefineFunction is used to define a custom function and provide Slack with the information required to use it.

SlackFunction uses the definition returned by DefineFunction and your custom executable code to export a Slack-usable custom function.

Create a workflow

mkdir ./workflows && touch ./workflows/hello_world.ts
// Contents of ./workflows/hello_world.ts
import { DefineWorkflow, Schema } from "deno-slack-sdk/mod.ts";
import { HelloWorldFunctionDef } from "../functions/hello_world.ts";

const HelloWorldWorkflowDef = DefineWorkflow({
  callback_id: "hello_world_workflow",
  title: "Hello World Workflow",
  input_parameters: {
    properties: {
      channel: {
        type: Schema.slack.types.channel_id,
      },
    },
    required: ["channel"],
  },
});

const helloWorldStep = HelloWorldWorkflowDef.addStep(HelloWorldFunctionDef, {});

HelloWorldWorkflowDef.addStep(Schema.slack.functions.SendMessage, {
  channel_id: HelloWorldWorkflowDef.inputs.channel,
  message: helloWorldStep.outputs.message,
});

export default HelloWorldWorkflowDef;

DefineWorkflow is used to define a workflow and provide Slack with the information required to use it.

HelloWorldWorkflow.addStep is used to add a step to the workflow; here we add the HelloWorldFunction and then the SendMessage Slack Function that will post the message to a Slack channel.

Update the manifest

// Contents of manifest.ts
import { Manifest } from "deno-slack-sdk/mod.ts";
import HelloWorldWorkflow from "./workflows/hello_world.ts";

export default Manifest({
  name: "my-app",
  description: "A Hello World app",
  icon: "assets/default_new_app_icon.png",
  workflows: [HelloWorldWorkflow],
  outgoingDomains: [],
  botScopes: ["chat:write", "chat:write.public"],
});

Manifest is used to define your apps manifest and provides Slack with the information required to manage it.

Create a trigger

mkdir ./triggers && touch ./triggers/hello_world.ts
// Contents of ./triggers/hello_world.ts
import { Trigger } from "deno-slack-sdk/types.ts";
import { TriggerContextData, TriggerTypes } from "deno-slack-api/mod.ts";
import HelloWorldWorkflow from "../workflows/hello_world.ts";

const trigger: Trigger<typeof HelloWorldWorkflow.definition> = {
  type: TriggerTypes.Shortcut,
  name: "Reverse a string",
  description: "Starts the workflow to reverse a string",
  workflow: `#/workflows/${HelloWorldWorkflow.definition.callback_id}`,
  inputs: {
    channel: {
      value: TriggerContextData.Shortcut.channel_id,
    },
  },
};

export default trigger;

The Trigger object is used to define a trigger that will invoke the HelloWorldWorkflow. The Slack CLI will detect this file and prompt you for its creation.

Running an app

slack run

When prompted, create the triggers/hello_world.ts trigger. This will send your trigger configuration to Slack.

Post the Hello world shortcut trigger in a slack message and use it

Getting Help

This documentation has more information on basic and advanced concepts of the SDK.

Information on how to get started developing with Deno can be found in this documentation.

If you get stuck, we're here to help. The following are the best ways to get assistance working through your issue:

Contributing

Contributions are more than welcome. Please look at the contributing guidelines for more info!