2

Send direct message

by
Published Nov 16, 2022

Sends direct message to user (you must know user email)

Script slack Verified

The script

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

3
const isEmail = (email: string): boolean =>
4
  /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
5

6
type Slack = {
7
  token: string;
8
};
9
export async function main(user_email: string, text: string, slack: Slack) {
10
  throwOnInvalidEmail(user_email);
11

12
  const client = new WebClient(slack.token);
13
  const userData = await getUserData(client, user_email);
14
  console.log(userData);
15
  const result = await sendDirectMessage(text, client, userData);
16

17
  return result;
18
}
19

20
interface UserData {
21
  id: string;
22
  email: string;
23
  real_name: string;
24
}
25

26
async function getUserData(
27
  client: WebClient,
28
  email: string,
29
): Promise<UserData> {
30
  try {
31
    const response = await client.users.lookupByEmail({ email: email });
32

33
    const { id, real_name } = response.user;
34
    return { id, email, real_name };
35
  } catch (err) {
36
    return handleApiResponse(email, err);
37
  }
38
}
39

40
async function sendDirectMessage(
41
  text: string,
42
  client: WebClient,
43
  userData: UserData,
44
) {
45
  const { ok, ts, channel } = await client.chat.postMessage({
46
    channel: userData.id,
47
    text,
48
  });
49
  return { ok, ts, channel, email: userData.email };
50
}
51

52
function throwOnInvalidEmail(email: string) {
53
  if (!isEmail(email)) {
54
    throw new Error("Expected user email");
55
  }
56
}
57

58
function handleApiResponse(email: string, error: Error) {
59
  return Promise.reject({ email, err_msg: error.message });
60
}
61

Other submissions
  • Submitted by mrl5 Deno
    Created 385 days ago
    1
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { isEmail } from "https://deno.land/x/[email protected]/mod.ts";
    3
    
    
    4
    type Slack = {
    5
      token: string;
    6
    };
    7
    export async function main(user_email: string, text: string, slack: Slack) {
    8
      throwOnInvalidEmail(user_email);
    9
    
    
    10
      const client = new WebClient(slack.token);
    11
      const userData = await getUserData(client, user_email);
    12
      console.log(userData);
    13
      const result = await sendDirectMessage(text, client, userData);
    14
    
    
    15
      return result;
    16
    }
    17
    
    
    18
    interface UserData {
    19
      id: string;
    20
      email: string;
    21
      real_name: string;
    22
    }
    23
    
    
    24
    async function getUserData(
    25
      client: WebClient,
    26
      email: string,
    27
    ): Promise<UserData> {
    28
      try {
    29
        const response = await client.users.lookupByEmail({ email: email });
    30
    
    
    31
        const { id, real_name } = response.user;
    32
        return { id, email, real_name };
    33
      } catch (err) {
    34
        return handleApiResponse(email, err);
    35
      }
    36
    }
    37
    
    
    38
    async function sendDirectMessage(
    39
      text: string,
    40
      client: WebClient,
    41
      userData: UserData,
    42
    ) {
    43
      const { ok, ts, channel } = await client.chat.postMessage({
    44
        channel: userData.id,
    45
        text,
    46
      });
    47
      return { ok, ts, channel, email: userData.email };
    48
    }
    49
    
    
    50
    function throwOnInvalidEmail(email: string) {
    51
      if (!isEmail(email)) {
    52
        throw new Error("Expected user email");
    53
      }
    54
    }
    55
    
    
    56
    function handleApiResponse(email: string, error: Error) {
    57
      return Promise.reject({ email, err_msg: error.message });
    58
    }
    59