Nextcloud Talk: Send a message

Send a message to a talk room

Script nextcloud Verified

by marcel klehr12 · 7/23/2024

The script

Submitted by nextcloud Bun
Verified 86 days ago
1
import createClient, { type Middleware } from "openapi-fetch";
2

3
export async function main(
4
  nextcloud: RT.Nextcloud,
5
  talkRoomToken: string,
6
  message: string,
7
) {
8

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

20
  const data = await client.POST("/ocs/v2.php/apps/spreed/api/{apiVersion}/chat/{token}", {
21
    params: {
22
      header: {
23
        "OCS-APIRequest": true,
24
      },
25
      query: {
26
        format: "json",
27
      },
28
      path: {
29
        apiVersion: "v1",
30
        token: talkRoomToken,
31
      },
32

33
    },
34
    body: {
35
      message: message,
36
    },
37
  });
38
  return data;
39
}
Other submissions
  • Submitted by marcel klehr12 Deno
    Created 568 days ago
    1
    import * as wmill from "npm:windmill-client@1";
    2
    import * as tb from "https://raw.githubusercontent.com/marcelklehr/nextcloud-client-deno/6f973bfdcaed0d8b4f35b6cf5d0368d8bec73ed4/talk/index.ts";
    3
    
    
    4
    export async function main(
    5
      nextcloudResource: string,
    6
      userId: string | null = null,
    7
      talkRoomToken: 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.ChatApi(config);
    41
    
    
    42
      const res = await api.chatSendMessageRaw({
    43
        apiVersion: "v1",
    44
        message,
    45
        oCSAPIRequest: true,
    46
        token: talkRoomToken,
    47
      });
    48
      return await res.raw.json();
    49
    }
    50