Thanks to visit codestin.com
Credit goes to hub.windmill.dev

Send a notification to a user
Codestin Search App 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 531 days ago Picked 6 times
Submitted by nextcloud Bun
Verified 347 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
  subjectParameters: Array | null = null,
17
  messageParameters: Array | null = null,
18
  useAppApiAuth: boolean = false,
19
) {
20

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

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

56
    },
57
    body: {
58
      subject: subject,
59
      message: message,
60
      subjectParameters: subjectParameters,
61
      messageParameters: messageParameters
62
    },
63
  });
64
}
Other submissions