Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small improvements to deno example #77

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ An echo bot in multiple languages to get you started.
| [C](./c) | `1.132.1` |
| [Go](./go) | `1.127.0` (jsonrpc, newer core might work too, last tested with `v1.131.4` ) |
| [Node.js](./nodejs_stdio_jsonrpc) | `1.139.3` |
| [Deno](./deno) | |
| [Deno](./deno) | `1.152.1` |
| [Python over cffi](./python_cffi) | `1.132.1` |
| [Python over jsonrpc](./python_jsonrpc) | `1.132.1` |
| [Rust](./rust) | `1.132.1` |
Expand Down
1 change: 1 addition & 0 deletions deno/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deltachat-data
6 changes: 3 additions & 3 deletions deno/deno.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"tasks": {
"start": "deno run --allow-env --allow-read --allow-run index.js"
"start": "deno run --allow-env --allow-read --allow-run index.ts"
},
"imports": {
"@deltachat/jsonrpc-client": "npm:@deltachat/jsonrpc-client@^1.151.3",
"@deltachat/stdio-rpc-server": "npm:@deltachat/stdio-rpc-server@^1.151.3"
"@deltachat/jsonrpc-client": "npm:@deltachat/jsonrpc-client@^1.152.1",
"@deltachat/stdio-rpc-server": "npm:@deltachat/stdio-rpc-server@^1.152.1"
}
}
56 changes: 28 additions & 28 deletions deno/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 18 additions & 19 deletions deno/index.js → deno/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { startDeltaChat } from "@deltachat/stdio-rpc-server";
import { C } from "@deltachat/jsonrpc-client";
import process from "node:process";

async function main() {
const dc = await startDeltaChat("deltachat-data");
Expand All @@ -12,17 +11,14 @@ async function main() {
// dc.on("ALL", console.debug.bind("[core]"));

// or only log what you want
dc.on(
"Info",
(accountId, { msg }) => console.info(accountId, "[core:info]", msg),
dc.on("Info", (accountId, { msg }) =>
console.info(accountId, "[core:info]", msg)
);
dc.on(
"Warning",
(accountId, { msg }) => console.warn(accountId, "[core:warn]", msg),
dc.on("Warning", (accountId, { msg }) =>
console.warn(accountId, "[core:warn]", msg)
);
dc.on(
"Error",
(accountId, { msg }) => console.error(accountId, "[core:error]", msg),
dc.on("Error", (accountId, { msg }) =>
console.error(accountId, "[core:error]", msg)
);

let firstAccount = (await dc.rpc.getAllAccounts())[0];
Expand All @@ -32,16 +28,19 @@ async function main() {
if (firstAccount.kind === "Unconfigured") {
console.info("account not configured, trying to login now...");
try {
if (!!process.env.ADDR && !!process.env.MAIL_PW) {
const addr = Deno.env.get("ADDR");
const mail_pw = Deno.env.get("MAIL_PW");
const chatmail_qr = Deno.env.get("CHATMAIL_QR");
if (addr && mail_pw) {
await dc.rpc.batchSetConfig(firstAccount.id, {
addr: process.env.ADDR,
mail_pw: process.env.MAIL_PW,
addr,
mail_pw,
});
} else if (!!process.env.CHATMAIL_QR) {
await dc.rpc.setConfigFromQr(firstAccount.id, process.env.CHATMAIL_QR);
} else if (chatmail_qr) {
await dc.rpc.setConfigFromQr(firstAccount.id, chatmail_qr);
} else {
throw new Error(
"Credentials missing, you need to set ADDR and MAIL_PW",
"Credentials missing, you need to set ADDR and MAIL_PW, or CHATMAIL_QR"
);
}
await dc.rpc.batchSetConfig(firstAccount.id, {
Expand All @@ -51,7 +50,7 @@ async function main() {
await dc.rpc.configure(firstAccount.id);
} catch (error) {
console.error("Could not log in to account:", error);
process.exit(1);
Deno.exit(1);
}
} else {
await dc.rpc.startIo(firstAccount.id);
Expand All @@ -67,7 +66,7 @@ async function main() {
await dc.rpc.miscSendTextMessage(
botAccountId,
chatId,
message.text || "",
message.text || ""
);
}
});
Expand All @@ -82,7 +81,7 @@ async function main() {
`Verify Bot contact (if you use chatmail this is nessesary to contact the bot from outside the chatmail instance that the bot uses):
copy this code and \"scan\" it with delta chat:

${verificationQRCode}`,
${verificationQRCode}`
);
console.info("".padEnd(40, "="));
}
Expand Down