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
.
- 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.
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 || "",
);
}
});
}
TODO, Look at the deltachat-desktop code once it switches to this package for reference n the meantime.
# install dependencies
yarn
yarn make
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]
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