0

Reply to Channel Message

by
Published today

Reply in the thread of a channel message as the connected user, as plain text or HTML. Requires ChannelMessage.Send (a connection default).

Script ms_teams Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
export type DynSelect_team_id = string
4

5
// Dropdown of the teams the connected user has joined.
6
export async function team_id(auth: RT.MsTeams) {
7
  const response = await fetch(
8
    `https://graph.microsoft.com/v1.0/me/joinedTeams`,
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { value } = (await response.json()) as { value: any[] }
20
  return value.map((x) => ({ value: x.id, label: x.displayName }))
21
}
22

23
export type DynSelect_channel_id = string
24

25
// Dropdown of the chosen team's channels.
26
export async function channel_id(auth: RT.MsTeams, team_id: string) {
27
  if (team_id == null || team_id === "") return []
28
  const response = await fetch(
29
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels`,
30
    {
31
      headers: {
32
        Authorization: `Bearer ${auth.token}`,
33
        Accept: "application/json",
34
      },
35
    }
36
  )
37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40
  const { value } = (await response.json()) as { value: any[] }
41
  return value.map((x) => ({ value: x.id, label: x.displayName }))
42
}
43

44
/**
45
 * Reply to Channel Message
46
 * Reply in the thread of a channel message as the connected user, as plain text or HTML. Requires ChannelMessage.Send (a connection default).
47
 */
48
export async function main(
49
  auth: RT.MsTeams,
50
  team_id: DynSelect_team_id,
51
  channel_id: DynSelect_channel_id,
52
  message_id: string,
53
  message: string,
54
  content_type: "text" | "html" = "text"
55
) {
56
  const payload: { [key: string]: any } = {
57
    body: { contentType: content_type, content: message },
58
  }
59

60
  const response = await fetch(
61
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels/${encodeURIComponent(channel_id)}/messages/${encodeURIComponent(message_id)}/replies`,
62
    {
63
      method: "POST",
64
      headers: {
65
        Authorization: `Bearer ${auth.token}`,
66
        "Content-Type": "application/json",
67
        Accept: "application/json",
68
      },
69
      body: JSON.stringify(payload),
70
    }
71
  )
72

73
  if (!response.ok) {
74
    throw new Error(`${response.status} ${await response.text()}`)
75
  }
76

77
  return await response.json()
78
}
79