@deltachat/napi-jsonrpc
v0.101.1
Published
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.
Downloads
11
Readme
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 indeltachat-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 using16
or newer. - This is an ESM module.
basic usage
npm i @deltachat/napi-jsonrpc
import { openDeltaChatInstance, T } 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.type === "Unconfigured") {
await dc.rpc.batchSetConfig(firstAccount.id, {
addr: process.env.ADDR,
mail_pw: process.env.MAIL_PW,
});
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 === 100 /* DC_CHAT_TYPE_SINGLE = 100 */) {
const message = await dc.rpc.messageGetMessage(botAccountId, msgId);
await dc.rpc.miscSendTextMessage(
botAccountId,
message.text || "",
chatId
);
}
});
}
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:
- adjust the version in
Cargo.toml
:
deltachat-jsonrpc = { git = "https://github.com/deltachat/deltachat-core-rust/", version = "1.97.0" }
- 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