Send a notification to a user
One script reply has been approved by the moderators Verified

Send a notification to a Nextcloud user. Either you authenticate as an admin and can then send notifications to any user, or you can only send notifications to the user you authenticated as.

Created by marcel klehr12 603 days ago Picked 36 times
Submitted by nextcloud Bun
Verified 29 days ago
1
import createClient, { type Middleware } from "openapi-fetch";
2

3

4
export async function main(
5
  nextcloud: RT.Nextcloud,
6
  notificationUserId: string,
7
  subject: string,
8
  message: string,
9
  subjectParameters: Array | null = null,
10
  messageParameters: Array | null = null,
11
) {
12

13
  const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
14
  const authMiddleware: Middleware = {
15
    async onRequest({ request, options }) {
16
      // fetch token, if it doesn’t exist
17
      // add Authorization header to every request
18
      request.headers.set("Authorization", `Basic ${btoa(nextcloud.userId + ':' + nextcloud.token)}`);
19
      return request;
20
    },
21
  };
22
  client.use(authMiddleware);
23

24
  const data = await client.POST("/ocs/v2.php/apps/notifications/api/{apiVersion3}/admin_notifications/{userId}", {
25
    params: {
26
      header: {
27
        "OCS-APIRequest": true,
28
      },
29
      query: {
30
        format: "json",
31
      },
32
      path: {
33
        apiVersion3: "v3",
34
        userId: notificationUserId,
35
      },
36

37
    },
38
    body: {
39
      subject: subject,
40
      message: message,
41
      subjectParameters: subjectParameters,
42
      messageParameters: messageParameters
43
    },
44
  });
45
  return data;
46
}
Other submissions