1
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 179 days ago Viewed 444 times
0
Submitted by nextcloud Bun
Verified 9 days ago
1
import * as wmill from "windmill-client";
2
import createClient, { type Middleware } from "openapi-fetch";
3

4
type Nextcloud = {
5
  baseUrl: string,
6
  password: string,
7
  username: string
8
};
9

10
export async function main(
11
  ncResource: Nextcloud,
12
  userId: string | null = null,
13
  notificationUserId: string,
14
  subject: string,
15
  message: string,
16
  useAppApiAuth: boolean = false,
17
) {
18

19
  const client = createClient<paths>({ baseUrl: ncResource.baseUrl });
20
  const authMiddleware: Middleware = {
21
    async onRequest({ request, options }) {
22
      // fetch token, if it doesn’t exist
23
      // add Authorization header to every request
24
      request.headers.set("Authorization", `Basic ${btoa((userId || ncResource.username) + ':' + ncResource.password)}`);
25
      if (useAppApiAuth) {
26
        request.headers.set("AA-VERSION", "2.3.0",);
27
        request.headers.set("EX-APP-ID", "flow",);
28
        request.headers.set("EX-APP-VERSION", "1.0.1",);
29
        request.headers.set("AUTHORIZATION-APP-API", btoa(
30
          `${userId || ncResource.username}:${ncResource.password}`,
31
        ));
32
      }
33
      return request;
34
    },
35
  };
36
  client.use(authMiddleware);
37

38
  const {
39
    data, // only present if 2XX response
40
    error, // only present if 4XX or 5XX response
41
  } = await client.POST("/ocs/v2.php/apps/notifications/api/{apiVersion3}/admin_notifications/{userId}", {
42
    params: {
43
      header: {
44
        "OCS-APIRequest": true,
45
      },
46
      query: {
47
        format: "json",
48
      },
49
      path: {
50
        apiVersion3: "v3",
51
        userId: notificationUserId,
52
      },
53

54
    },
55
    body: {
56
      subject: subject,
57
      message: message,
58
    },
59
  });
60
}
Other submissions