Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

GBAS

This is an adapter for easily creating generic bots on the Slack Next-gen platform that respond to mentions, messages, and emojis.

Getting Started

First, prepare a Slack Next-gen platform app.

You can create a new one using the slack create command. For more information, please refer to the official Slack documentation.

Then navigate to the slack project directory, run the following:

deno run -Ar https://deno.land/x/gbas/init.ts
Actually command output example
┗╸❯❯❯ slack create sample
βš™οΈ  Creating a new Slack app in ~/sample

πŸ“¦ Installed project dependencies

✨ sample successfully created

🧭 Explore the documentation to learn more
   Read the README.md or peruse the docs over at api.slack.com/future
   Find available commands and usage info with slack help

πŸ“‹ Follow the steps below to begin development
   Change into your project directory with cd sample/
   Develop locally and see changes in real-time with slack run
   When you're ready to deploy for production use slack deploy

┗╸❯❯❯ cd sample

┗╸❯❯❯ deno run -Ar https://deno.land/x/gbas/init.ts
πŸŽ‰ Successfully created bot code.

You must edit bot/config.ts.

1. Change CHANNEL_IDS in bot/config.ts.
2. Add your first command by `deno run -Ar https://deno.land/x/gbas/command.ts`
3. Develop locally with `slack run`.

For more examples, please refer to the examples directory.

Usage

Implement each function as a command.

You can scaffold code by running the following:

deno run -Ar https://deno.land/x/gbas/command.ts

Mention Command

To create a command that responds to mentions, use the following implementation:

import { createMentionCommand } from "gbas/mod.ts";

export const echo = createMentionCommand({
  name: "echo",
  examples: ["echo <message> - echo applied message"],
  pattern: /^echo\s*(.+)$/i,
  execute: (c) => c.res.message(c.match[1]),
});

You can write a test for the command like this:

import { createMentionCommandTester } from "gbas/mod.ts";
import { assert, assertEquals } from "std/testing/asserts.ts";
import { echo } from "./echo.ts";

const { createContext, dispatch } = createMentionCommandTester(echo);

Deno.test("echo", async () => {
  const res = await dispatch(createContext("<@BOT> echo hello world"));
  assert(res.type === "message");
  assertEquals(res.text, "hello world");
});

Message Command

To create a command that responds to messages, use the following implementation:

import { createMessageCommand } from "gbas/mod.ts";

export const hello = createMessageCommand({
  name: "hello",
  examples: ["hello - reaction emoji"],
  pattern: /^hello$/i,
  execute: (c) => c.res.reaction("hugging_face"),
});

You can write a test for the command like this:

import { createMessageCommandTester } from "gbas/mod.ts";
import { assert, assertEquals } from "std/testing/asserts.ts";
import { hello } from "./hello.ts";

const { createContext, dispatch } = createMessageCommandTester(echo);

Deno.test("hello", async () => {
  const c = createContext("hello");
  const res = await dispatch(c);
  assert(res.type === "reaction");
  assertEquals(res.emoji, "hugging_face");
});

Reaction Command

To create a command that responds to reactions (emojis), use the following implementation:

import { createReactionCommand } from "gbas/mod.ts";

export const owl = createReactionCommand({
  name: "owl",
  examples: [":owl: - reply hoot"],
  emojis: ["owl"],
  execute: (c) => c.res.message("hoot!", { mentionUserIds: [c.event.userId] }),
});

You can write a test for the command like this:

import { createReactionCommandTester } from "gbas/mod.ts";
import { assert, assertEquals } from "std/testing/asserts.ts";
import { owl } from "./owl.ts";

const { createContext, dispatch } = createReactionCommandTester(owl);

Deno.test("owl", async () => {
  const c = createContext("owl");
  const res = await dispatch(c);
  assert(res.type === "message");
  assertEquals(res.text, "<@USER> hoot!");
});

Development

  • Test: deno task test