0

Send Message to Channel

by
Published Jul 28, 2022

Post a message to a channel, as a thread reply, or as Block Kit blocks. Needs chat:write.

Script slack Verified

The script

Submitted by hugo989 Bun
Verified 7 days ago
1
import { WebClient } from "@slack/web-api"
2

3
/**
4
 * Send Message to Channel
5
 * Post a message to a channel, DM or group DM by ID. Pass `thread_ts` (the parent message's `ts`) to reply in a thread, and `blocks` to send a Block Kit layout — `text` is then the notification fallback. Returns the posted message's `ts`, needed to update it or react to it. Needs chat:write; a bot token posts to public channels it is not in only with chat:write.public.
6
 */
7
export async function main(
8
  text: string,
9
  channel: string,
10
  slack: RT.Slack,
11
  thread_ts?: string,
12
  reply_broadcast?: boolean,
13
  blocks?: { [key: string]: any }[]
14
) {
15
  const web = new WebClient(slack.token)
16

17
  return await web.chat.postMessage(
18
    dropEmpty({ channel, text, thread_ts, reply_broadcast, blocks }) as any
19
  )
20
}
21

22
function dropEmpty<T extends { [key: string]: any }>(params: T) {
23
  return Object.fromEntries(
24
    Object.entries(params).filter(
25
      ([, v]) => v !== undefined && v !== null && v !== ""
26
    )
27
  ) as T
28
}
29

Other submissions
  • Submitted by admin Deno
    Created 505 days ago
    1
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    type Slack = {
    4
      token: string;
    5
    };
    6
    export async function main(text: string, channel: string, slack: Slack) {
    7
      const web = new WebClient(slack.token);
    8
    
    
    9
      await web.chat.postMessage({
    10
        channel,
    11
        text,
    12
      });
    13
    }
    14
    
    
  • Submitted by wangjian.cd865 Deno
    Created 1243 days ago
    1
    // import * as wmill from 'https://deno.land/x/windmill/index.ts'
    2
    
    
    3
    export async function main(x: string) {
    4
        return x
    5
    }
  • Submitted by choonfeng.sng05571 Deno
    Created 1106 days ago
    1
    // import * as wmill from 'https://deno.land/x/windmill/index.ts'
    2
    
    
    3
    export async function main(x: string) {
    4
        return x
    5
    }