Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Latest commit

 

History

History
113 lines (85 loc) · 3.34 KB

README.MD

File metadata and controls

113 lines (85 loc) · 3.34 KB

not maintained use https://github.com/deltachat/deltachat-core-rust/tree/main/deltachat-rpc-server/npm-package instead

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:

  • Exclusively use the jsonrpc api*:
    • easier to use, develop and maintain because it has better (autogenerated) typescript bindings.
    • better errors, functions throw errors, not a check if the function returned 0.
    • everything is async, no blocking calls
  • easier to install, maintain and less errors in the bindings levels because it's using napi-rs as base which is written in rust instead of the manually written c bindings in deltachat-node

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

Usage

  • If you use typescript, then you need to use a typescript version newer than 4.7.
  • Nodejs version 14 is the lowest supported node version by this package, but we recommend using 16 or newer.
  • This is an ESM module.

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/[email protected]

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