Plugin SDK

@whatszori/plugin-sdk makes writing plugins feel like writing any other React code:

  • TypeScript types for the whole plugin API, with typed events;
  • JSX and React hooks that run on the app's own React, so you don't think about ctx.React;
  • wz-plugin, a command that creates a project, builds it, and rebuilds it into WhatsZori's plugins folder on every save;
  • a JSON schema for manifest.json, so your editor checks it as you type.

NoteThe SDK isn't on npm yet. Until it is, clone the repository, run npm install and npm link in it, then npm link @whatszori/plugin-sdk in your plugin's folder.

A new plugin

npx @whatszori/plugin-sdk create my-plugin
cd my-plugin
npm install
npm run dev

npm run dev builds the plugin straight into WhatsZori's plugins folder and keeps watching. Turn the plugin on in Settings โ†’ Plugins. After each rebuild, turn it off and on again to load the new code.

The project looks like this:

my-plugin/
  manifest.json      id, name, version
  src/index.tsx      the plugin
  assets/            optional, copied next to the built script
  tsconfig.json
  package.json

Writing it

import { definePlugin } from '@whatszori/plugin-sdk';
import { useState } from 'react';

export default definePlugin((ctx) => {
  ctx.registerSlot('composer.actions', () => {
    const [n, setN] = useState(0);
    return (
      <ctx.ui.IconButton
        label={`Wave (${n})`}
        icon="๐Ÿ‘‹"
        onClick={() => {
          setN(n + 1);
          ctx.actions.insertIntoComposer('๐Ÿ‘‹');
        }}
      />
    );
  });

  ctx.onEvent('msg:deleted', ({ chatJid, deleted }) => {
    ctx.log.info(`${deleted.byName} deleted a message in ${chatJid}`);
  });
});

definePlugin takes your start(ctx) function, or an object with start, and becomes the module's default export. Before your code runs, it hands the app's React to the SDK. That's what makes JSX and import โ€ฆ from 'react' work.

Where to create components

The app's React isn't known until start() runs. So create components, contexts and memoised values inside start() or during render, not at the top level of the module:

// at the top of the module: fails, React isn't there yet
const Ctx = createContext(null);

export default definePlugin((ctx) => {
  // inside start: fine
  const Ctx = createContext(null);
});

If you get this wrong, the SDK throws an error that says exactly this.

Libraries

Libraries that import react work, and get the app's React too. react-dom isn't available: render through slots instead.

Commands

Command
wz-plugin create <folder> A new project. Options: --id, --name, --author.
wz-plugin dev Build into WhatsZori's plugins folder and rebuild on every save
wz-plugin install Build into the plugins folder once. --minify makes it smaller.
wz-plugin build Build into dist/<id>/, ready to zip and share. --out picks another folder, --minify makes it smaller.
wz-plugin validate [folder] Check manifest.json the same way the app does

The entry is src/index.tsx, or .ts, .jsx, .js. The output is a single ES module, plus the manifest and assets/.

The plugins folder is found the way the app finds it:

System Folder
Windows %AppData%\whatszori\plugins
Linux $XDG_CONFIG_HOME/whatszori/plugins, usually ~/.config/whatszori/plugins
macOS ~/Library/Application Support/whatszori/plugins

Set WHATSZORI_CONFIG_DIR to build into another profile, for example a test one.

Sharing a plugin

Run npm run build and zip the dist/<id> folder. To install it, users unpack it into their plugins folder, as described in the guide.