deltachat / napi-jsonrpc

use jsonrpc over napi.rs in nodejs
3 stars 0 forks source link

Deltachat jsonrpc-napi bindings

Allows you to use Deltachat core from nodejs. You can use this library to create Bots and Clients for deltachat. This package aims to become the sucessor to deltachat-node, replacing it as a backend in deltachat desktop.

The main differences to deltachat-node are:

* there might be some exceptions to expose certain low-level functions for deltachat-desktop.

Usage

basic usage

npm i @deltachat/napi-jsonrpc
import { openDeltaChatInstance, T, C } from "@deltachat/napi-jsonrpc";

async function main() {
  const dc = await openDeltaChatInstance("./test-deltachat-tmp");
  // log all events to console
  dc.on("ALL", console.debug.bind("[core]"));

  // prepare account and login
  let firstAccount: T.Account | undefined = (await dc.rpc.getAllAccounts())[0];
  if (!firstAccount) {
    firstAccount = await dc.rpc.getAccountInfo(await dc.rpc.addAccount());
  }
  if (firstAccount.kind === "Unconfigured") {
    await dc.rpc.batchSetConfig(firstAccount.id, {
      addr: process.env.ADDR,
      mail_pw: process.env.MAIL_PW,
      bot: "1",
    });
    await dc.rpc.configure(firstAccount.id);
  }

  // the actual bot code, echos back text messages you sent to it
  const botAccountId = firstAccount.id;
  const emitter = dc.getContextEvents(botAccountId);
  emitter.on("IncomingMsg", async ({ chatId, msgId }) => {
    const chat = await dc.rpc.getBasicChatInfo(botAccountId, chatId);
    // only echo to DM chat
    if (chat.chatType === C.DC_CHAT_TYPE_SINGLE) {
      const message = await dc.rpc.messageGetMessage(botAccountId, msgId);
      await dc.rpc.miscSendTextMessage(
        botAccountId,
        chatId
        message.text || "",
      );
    }
  });
}

use from a different process (like renderer process in electron)

TODO, Look at the deltachat-desktop code once it switches to this package for reference n the meantime.

Build the package yourself

Preparations

# install dependencies
yarn

Build it

yarn make

Update deltachat-core

to update deltachat core you need to update 2 packages:

  1. adjust the version in Cargo.toml:
deltachat-jsonrpc = { git = "https://github.com/deltachat/deltachat-core-rust/", version = "1.97.0" }
  1. run this command with the new version:
yarn add @deltachat/jsonrpc-client@1.97.0

Publish new version:

The ci does this automatically, you just need to run.

# npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
npm version patch
git push --follow-tags