0

Get Chat Message

by
Published today

Get a single message of a chat by id. Requires Chat.Read (covered by the default Chat.ReadWrite).

Script ms_teams Verified

The script

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

3
export type DynSelect_chat_id = string
4

5
// Dropdown of the connected user's chats, labelled by topic or member names.
6
export async function chat_id(auth: RT.MsTeams) {
7
  const response = await fetch(
8
    `https://graph.microsoft.com/v1.0/me/chats?$expand=members&$top=50`,
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) => ({
21
    value: x.id,
22
    label:
23
      x.topic ||
24
      (x.members ?? [])
25
        .map((m: any) => m.displayName)
26
        .filter(Boolean)
27
        .join(", ") ||
28
      x.chatType,
29
  }))
30
}
31

32
/**
33
 * Get Chat Message
34
 * Get a single message of a chat by id. Requires Chat.Read (covered by the default Chat.ReadWrite).
35
 */
36
export async function main(
37
  auth: RT.MsTeams,
38
  chat_id: DynSelect_chat_id,
39
  message_id: string
40
) {
41
  const url = new URL(
42
    `https://graph.microsoft.com/v1.0/chats/${encodeURIComponent(chat_id)}/messages/${encodeURIComponent(message_id)}`
43
  )
44

45
  const response = await fetch(url, {
46
    headers: {
47
      Authorization: `Bearer ${auth.token}`,
48
      Accept: "application/json",
49
    },
50
  })
51

52
  if (!response.ok) {
53
    throw new Error(`${response.status} ${await response.text()}`)
54
  }
55

56
  return await response.json()
57
}
58