Send a notification to a user

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.

Script nextcloud Verified

by marcel klehr12 · 7/23/2024

The script

Submitted by nextcloud Bun
Verified 100 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
  • Submitted by marcel klehr12 Deno
    Created 582 days ago
    1
    import * as wmill from "npm:windmill-client@1";
    2
    import * as tb from "https://raw.githubusercontent.com/marcelklehr/nextcloud-client-deno/4432705a6ee46808e951b08cf3e9e3e1daece0f9/notifications-admin/index.ts";
    3
    
    
    4
    export async function main(
    5
      nextcloudResource: string,
    6
      userId: string|null = null,
    7
      notificationUserId: string,
    8
      message: string,
    9
      useAppApiAuth: boolean = false,
    10
    ) {
    11
      const ncResource = await wmill.getResource(
    12
        nextcloudResource,
    13
      );
    14
      const config = new tb.Configuration({
    15
        username: userId || ncResource.username,
    16
        password: ncResource.password,
    17
        basePath: ncResource.baseUrl,
    18
        middleware: [{
    19
          async pre(context) {
    20
            if (!context.url.includes("?")) {
    21
              context.url += "?";
    22
            } else {
    23
              context.url += "&";
    24
            }
    25
            context.url += "format=json";
    26
            return context;
    27
          },
    28
        }],
    29
        ...(useAppApiAuth && ({
    30
          headers: {
    31
            "AA-VERSION": "2.3.0",
    32
            "EX-APP-ID": "flow",
    33
            "EX-APP-VERSION": "1.0.0",
    34
            "AUTHORIZATION-APP-API": btoa(
    35
              `${userId || ncResource.username}:${ncResource.password}`,
    36
            ),
    37
          },
    38
        })),
    39
      });
    40
      const api = new tb.ApiApi(config);
    41
    
    
    42
      return await api.apiGenerateNotification({
    43
        apiVersion: "v2",
    44
        oCSAPIRequest: true,
    45
        shortMessage: message,
    46
        userId: notificationUserId,
    47
      });
    48
    }
    49