0

Search Messages

by
Published today

Search the chat and channel messages the connected user can see with a KQL query (e.g. `budget from:alice`), ranked by relevance. Requires Chat.Read (covered by the default Chat.ReadWrite); channel messages are only searched when ChannelMessage.Read.All is granted (admin consent, not a default).

Script ms_teams Verified

The script

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

3
/**
4
 * Search Messages
5
 * Search the chat and channel messages the connected user can see with a KQL query (e.g. `budget from:alice`), ranked by relevance. Requires Chat.Read (covered by the default Chat.ReadWrite); channel messages are only searched when ChannelMessage.Read.All is granted (admin consent, not a default).
6
 */
7
export async function main(
8
  auth: RT.MsTeams,
9
  query_string: string,
10
  from: number = 0,
11
  size: number = 25
12
) {
13
  const payload = {
14
    requests: [
15
      {
16
        entityTypes: ["chatMessage"],
17
        query: { queryString: query_string },
18
        from,
19
        size,
20
      },
21
    ],
22
  }
23

24
  const response = await fetch(
25
    `https://graph.microsoft.com/v1.0/search/query`,
26
    {
27
      method: "POST",
28
      headers: {
29
        Authorization: `Bearer ${auth.token}`,
30
        "Content-Type": "application/json",
31
        Accept: "application/json",
32
      },
33
      body: JSON.stringify(payload),
34
    }
35
  )
36

37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40

41
  return await response.json()
42
}
43