Example of responding to Slack commands and @mentions

This script responds to both `/windmill <text>` slash commands and @Windmill mentions in Slack.

Script slack Verified

by admin ยท 11/8/2022

The script

Submitted by hugo989 Bun
Verified 4 days ago
1
  import { WebClient } from '@slack/web-api';
2
  import * as wmill from 'windmill-client@1';
3

4
  type Slack = {
5
      token: string;
6
  };
7

8
  export async function main(
9
      response_url: string,
10
      text: string,
11
      channel_id?: string,
12
      command?: string
13
  ) {
14
      if (command === "@mention") {
15
          // @mention - use Slack API
16
          const slack = await wmill.getResource<Slack>('f/slack_bot/bot_token');
17
          const web = new WebClient(slack.token);
18
          await web.chat.postMessage({
19
              channel: channel_id,
20
              text: `Echo: ${text}`
21
          });
22
      } else {
23
          // Slash command - use response_url
24
          await fetch(response_url, {
25
              method: 'POST',
26
              body: JSON.stringify({ text: `Echo: ${text}` })
27
          });
28
      }
29
  }
Other submissions
  • Submitted by henri186 Deno
    Created 220 days ago
    1
      import { WebClient } from 'https://deno.land/x/[email protected]/mod.ts';
    2
      import * as wmill from 'https://deno.land/x/[email protected]/mod.ts';
    3
    
    
    4
      type Slack = {
    5
          token: string;
    6
      };
    7
    
    
    8
      export async function main(
    9
          response_url: string,
    10
          text: string,
    11
          channel_id?: string,
    12
          command?: string
    13
      ) {
    14
          if (command === "@mention") {
    15
              // @mention - use Slack API
    16
              const slack = await wmill.getResource<Slack>('f/slack_bot/bot_token');
    17
              const web = new WebClient(slack.token);
    18
              await web.chat.postMessage({
    19
                  channel: channel_id,
    20
                  text: `Echo: ${text}`
    21
              });
    22
          } else {
    23
              // Slash command - use response_url
    24
              await fetch(response_url, {
    25
                  method: 'POST',
    26
                  body: JSON.stringify({ text: `Echo: ${text}` })
    27
              });
    28
          }
    29
      }
  • Submitted by admin Deno
    Created 1307 days ago
    1
    export async function main(
    2
      response_url: string,
    3
      text: string,
    4
    ) {
    5
      const x = await fetch(response_url, {
    6
        method: 'POST',
    7
        body: JSON.stringify({ text: `ROGER ${text}` }),
    8
      });
    9
    }